"""
출근시간 수동 입력 다이얼로그
"""
from PyQt5.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QLabel,
                             QPushButton, QTimeEdit, QMessageBox)
from PyQt5.QtCore import QTime, Qt
from datetime import datetime
from core.i18n import tr
from ui.styles import apply_dark_titlebar


class ClockInDialog(QDialog):
    """출근시간 입력 다이얼로그"""

    def __init__(self, parent=None, default_time=None):
        super().__init__(parent)
        self.selected_time = None
        self.init_ui(default_time)
        apply_dark_titlebar(self)

    def init_ui(self, default_time):
        """UI 초기화"""
        self.setWindowTitle(tr('window.clock_in_dialog'))
        self.setModal(True)
        self.setFixedSize(340, 200)

        layout = QVBoxLayout()
        layout.setSpacing(8)
        layout.setContentsMargins(12, 10, 12, 10)

        # 안내 문구
        info_label = QLabel(tr('dlg.clock_in.prompt'))
        info_label.setObjectName("field_label")
        info_label.setAlignment(Qt.AlignCenter)
        layout.addWidget(info_label)

        # 시간 입력
        time_layout = QHBoxLayout()
        time_label = QLabel(tr('dlg.clock_in.label'))
        time_label.setObjectName("field_label")

        self.time_edit = QTimeEdit()
        self.time_edit.setDisplayFormat("HH:mm:ss")
        self.time_edit.setMinimumHeight(35)

        # 기본값 설정
        if default_time:
            qtime = QTime(default_time.hour, default_time.minute, default_time.second)
        else:
            # 현재 시간으로 기본값 설정
            now = datetime.now()
            qtime = QTime(now.hour, now.minute, now.second)

        self.time_edit.setTime(qtime)

        time_layout.addWidget(time_label)
        time_layout.addWidget(self.time_edit)
        layout.addLayout(time_layout)

        # 빠른 선택 버튼
        quick_layout = QHBoxLayout()
        quick_label = QLabel(tr('dlg.clock_in.quick'))
        quick_label.setObjectName("field_label")

        btn_8am = QPushButton("08:00")
        btn_9am = QPushButton("09:00")
        btn_10am = QPushButton("10:00")
        btn_now = QPushButton(tr('dlg.clock_in.btn_now'))

        for btn in [btn_8am, btn_9am, btn_10am, btn_now]:
            btn.setMinimumHeight(30)

        btn_8am.clicked.connect(lambda: self.time_edit.setTime(QTime(8, 0, 0)))
        btn_9am.clicked.connect(lambda: self.time_edit.setTime(QTime(9, 0, 0)))
        btn_10am.clicked.connect(lambda: self.time_edit.setTime(QTime(10, 0, 0)))
        btn_now.clicked.connect(lambda: self.time_edit.setTime(QTime.currentTime()))

        quick_layout.addWidget(quick_label)
        quick_layout.addWidget(btn_8am)
        quick_layout.addWidget(btn_9am)
        quick_layout.addWidget(btn_10am)
        quick_layout.addWidget(btn_now)
        layout.addLayout(quick_layout)

        layout.addStretch()

        # 버튼
        button_layout = QHBoxLayout()

        ok_button = QPushButton(tr('btn.confirm'))
        ok_button.setObjectName("btn_primary")
        ok_button.setMinimumHeight(40)
        ok_button.clicked.connect(self.accept)

        cancel_button = QPushButton(tr('btn.cancel'))
        cancel_button.setMinimumHeight(40)
        cancel_button.clicked.connect(self.reject)

        button_layout.addWidget(cancel_button)
        button_layout.addWidget(ok_button)
        layout.addLayout(button_layout)

        self.setLayout(layout)

    def accept(self):
        """확인 버튼 클릭"""
        qtime = self.time_edit.time()

        # QTime을 datetime으로 변환
        today = datetime.now().date()
        self.selected_time = datetime.combine(
            today,
            datetime.min.time().replace(
                hour=qtime.hour(),
                minute=qtime.minute(),
                second=qtime.second()
            )
        )

        super().accept()

    def get_time(self):
        """선택된 시간 반환"""
        return self.selected_time


# 테스트 코드
if __name__ == "__main__":
    from PyQt5.QtWidgets import QApplication
    import sys

    app = QApplication(sys.argv)

    dialog = ClockInDialog()
    if dialog.exec_() == QDialog.Accepted:
        selected_time = dialog.get_time()
        print(tr('clock_in_dialog.selected', time=selected_time.strftime('%H:%M:%S')))
    else:
        print(tr('clock_in_dialog.cancelled'))

    sys.exit()
