import sys
import winsound
import threading
from PyQt6.QtWidgets import (
    QApplication, QMainWindow, QWidget,
    QVBoxLayout, QLabel, QPushButton, QHBoxLayout
)
from PyQt6.QtCore import QTimer, QElapsedTimer, Qt

# --- 파라미터 ---
INTERVAL_A_MS   = 7_500
INTERVAL_B_MS   = 60_000
FREQ_A_HZ       = 1_000
FREQ_B_HZ       =   600
DUR_A_MS        =   300
DUR_B_MS        =   500
UPDATE_TICK_MS  =   100   # UI 갱신 주기
# ----------------

def beep_nonblocking(freq, dur):
    threading.Thread(target=lambda: winsound.Beep(freq, dur), daemon=True).start()

class BeepWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Beeper Deluxe")
        self.setFixedSize(1920, 1080)

        # 절대 시간 타이머
        self.elapsed = QElapsedTimer()
        self.update_timer = QTimer(self)
        self.update_timer.timeout.connect(self._update_and_beep)

        # UI
        self.status_lbl = QLabel("정지", alignment=Qt.AlignmentFlag.AlignCenter)
        self.timerA_lbl = QLabel("숨돌방지: 0.0s", alignment=Qt.AlignmentFlag.AlignCenter)
        self.timerB_lbl = QLabel("파운틴: 0.0s", alignment=Qt.AlignmentFlag.AlignCenter)
        self.start_btn  = QPushButton("시작")
        self.stop_btn   = QPushButton("중지")
        self.stop_btn.setEnabled(False)

        vbox = QVBoxLayout()
        vbox.addWidget(self.status_lbl)
        vbox.addWidget(self.timerA_lbl)
        vbox.addWidget(self.timerB_lbl)
        hbox = QHBoxLayout()
        hbox.addWidget(self.start_btn)
        hbox.addWidget(self.stop_btn)
        vbox.addLayout(hbox)

        container = QWidget()
        container.setLayout(vbox)
        self.setCentralWidget(container)

        # 버튼 연결
        self.start_btn.clicked.connect(self.start_beeping)
        self.stop_btn.clicked.connect(self.stop_beeping)

        # 상태값
        self.is_running = False
        self.next_a = INTERVAL_A_MS
        self.next_b = INTERVAL_B_MS

    def _update_and_beep(self):
        now = self.elapsed.elapsed()

        if now >= self.next_a:
            beep_nonblocking(FREQ_A_HZ, DUR_A_MS)
            self.next_a += INTERVAL_A_MS

        if now >= self.next_b:
            beep_nonblocking(FREQ_B_HZ, DUR_B_MS)
            self.next_b += INTERVAL_B_MS

        rem_a = max(0, self.next_a - now) / 1000
        rem_b = max(0, self.next_b - now) / 1000
        self.timerA_lbl.setText(f"숨돌방지: {rem_a:.1f}s")
        self.timerB_lbl.setText(f"파운틴: {rem_b:.1f}s")

    def start_beeping(self):
        if not self.is_running:
            self.is_running = True
            self.status_lbl.setText("실행 중")
            self.start_btn.setEnabled(False)
            self.stop_btn.setEnabled(True)

            # 타이머 초기화
            self.next_a = INTERVAL_A_MS
            self.next_b = INTERVAL_B_MS
            self.elapsed.restart()

            # 즉시 삑
            beep_nonblocking(FREQ_A_HZ, DUR_A_MS)
            beep_nonblocking(FREQ_B_HZ, DUR_B_MS)

            # 업데이트 시작
            self.update_timer.start(UPDATE_TICK_MS)

    def stop_beeping(self):
        if self.is_running:
            self.is_running = False
            self.update_timer.stop()
            self.status_lbl.setText("정지")
            self.start_btn.setEnabled(True)
            self.stop_btn.setEnabled(False)
            # 초기화 표시
            self.timerA_lbl.setText("숨돌방지: 0.0s")
            self.timerB_lbl.setText("파운틴: 0.0s")

def main():
    app = QApplication(sys.argv)
    win = BeepWindow()
    win.show()
    sys.exit(app.exec())

if __name__ == "__main__":
    main() 이코드 어때?