import sys
import os

try:
    from PyQt5.QtWidgets import (
        QApplication, QWidget, QVBoxLayout, QLabel, QHBoxLayout, QPushButton, QSpinBox, QComboBox, QLineEdit, QTabWidget
    )
    from PyQt5.QtCore import QTimer, Qt
    from PyQt5.QtGui import QFont
except ModuleNotFoundError:
    print("Error: PyQt5 is not installed. Please install it using 'pip install PyQt5'.")
    sys.exit(1)

# macOS 시스템 사운드 목록
SYSTEM_SOUNDS = [
    "Glass.aiff", "Tink.aiff", "Hero.aiff", "Morse.aiff", "Ping.aiff", "Pop.aiff",
    "Purr.aiff", "Submarine.aiff", "Blow.aiff", "Bottle.aiff", "Frog.aiff", "Funk.aiff", "Sosumi.aiff"
]

class TimerApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        
        # 타이머 기능
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_timer)
        self.remaining_time = 1500  # 기본 25분 설정
        self.is_running = False
    
    def initUI(self):
        self.setWindowTitle("macOS Timer App")
        self.setStyleSheet("background-color: #1E1E1E; color: white;")
        
        main_layout = QVBoxLayout()
        
        # 상단 탭
        self.tabs = QTabWidget()
        self.tabs.addTab(QWidget(), "World Clock")
        self.tabs.addTab(QWidget(), "Alarms")
        self.tabs.addTab(QWidget(), "Stopwatch")
        self.tabs.addTab(QWidget(), "Timers")
        self.tabs.setCurrentIndex(3)
        self.tabs.setStyleSheet("QTabBar::tab { background: #3A3A3A; color: #888888; padding: 10px; } "
                                "QTabBar::tab:selected { background: #2C2C2C; color: white; }")
        main_layout.addWidget(self.tabs)
        
        # 타이머 디스플레이
        self.timer_label = QLabel("25:00")
        self.timer_label.setFont(QFont("Arial", 48, QFont.Bold))
        self.timer_label.setAlignment(Qt.AlignCenter)
        main_layout.addWidget(self.timer_label)
        
        # 시간 설정 UI
        time_layout = QHBoxLayout()
        
        self.hour_spin = QSpinBox()
        self.hour_spin.setRange(0, 23)
        self.hour_spin.setStyleSheet("background-color: #2C2C2C; color: white;")
        time_layout.addWidget(self.hour_spin)
        time_layout.addWidget(QLabel("hr"))
        
        self.min_spin = QSpinBox()
        self.min_spin.setRange(0, 59)
        self.min_spin.setValue(25)
        self.min_spin.setStyleSheet("background-color: #2C2C2C; color: white;")
        time_layout.addWidget(self.min_spin)
        time_layout.addWidget(QLabel("min"))
        
        self.sec_spin = QSpinBox()
        self.sec_spin.setRange(0, 59)
        self.sec_spin.setStyleSheet("background-color: #2C2C2C; color: white;")
        time_layout.addWidget(self.sec_spin)
        time_layout.addWidget(QLabel("sec"))
        
        main_layout.addLayout(time_layout)
        
        # 타이머 이름 입력
        self.timer_name = QLineEdit()
        self.timer_name.setPlaceholderText("Timer Name")
        self.timer_name.setStyleSheet("background-color: #2C2C2C; color: white; padding: 5px;")
        main_layout.addWidget(self.timer_name)
        
        # 사운드 선택
        self.sound_combo = QComboBox()
        self.sound_combo.addItems(SYSTEM_SOUNDS)
        self.sound_combo.setStyleSheet("background-color: #2C2C2C; color: white; padding: 5px;")
        main_layout.addWidget(self.sound_combo)
        
        # 버튼 레이아웃
        btn_layout = QHBoxLayout()
        
        self.start_pause_btn = QPushButton("Start")
        self.start_pause_btn.setStyleSheet("background-color: #34C759; color: white; padding: 10px; border-radius: 5px;")
        self.start_pause_btn.clicked.connect(self.start_pause_timer)
        btn_layout.addWidget(self.start_pause_btn)
        
        self.cancel_btn = QPushButton("Cancel")
        self.cancel_btn.setStyleSheet("background-color: #3A3A3A; color: white; padding: 10px; border-radius: 5px;")
        self.cancel_btn.clicked.connect(self.cancel_timer)
        btn_layout.addWidget(self.cancel_btn)
        
        main_layout.addLayout(btn_layout)
        
        self.setLayout(main_layout)
        self.setFixedSize(400, 300)
    
    def start_pause_timer(self):
        if self.is_running:
            self.timer.stop()
            self.start_pause_btn.setText("Start")
        else:
            if self.remaining_time == 0:
                hours = self.hour_spin.value()
                minutes = self.min_spin.value()
                seconds = self.sec_spin.value()
                self.remaining_time = (hours * 3600) + (minutes * 60) + seconds
            self.timer.start(1000)
            self.start_pause_btn.setText("Pause")
        self.is_running = not self.is_running
    
    def cancel_timer(self):
        self.timer.stop()
        self.remaining_time = 1500  # 기본 25분 설정
        self.update_display()
        self.start_pause_btn.setText("Start")
        self.is_running = False
    
    def update_timer(self):
        if self.remaining_time > 0:
            self.remaining_time -= 1
            self.update_display()
        else:
            self.timer.stop()
            self.is_running = False
            self.start_pause_btn.setText("Start")
            self.play_sound()
    
    def update_display(self):
        minutes = self.remaining_time // 60
        seconds = self.remaining_time % 60
        self.timer_label.setText(f"{minutes:02}:{seconds:02}")
    
    def play_sound(self):
        sound = self.sound_combo.currentText()
        os.system(f"afplay /System/Library/Sounds/{sound}")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = TimerApp()
    window.show()
    sys.exit(app.exec_())
