import customtkinter as ctk
from view.theme import theme_manager

class NotificationDialog(ctk.CTkToplevel):
    def __init__(self, controller, title, msg, voc_id=None):
        super().__init__(controller.root)
        self.controller = controller
        self.voc_id = voc_id

        self.title(title)
        self.attributes("-topmost", True)  # 항상 위
        self.resizable(False, False)
        
        # UI 구성
        self.width = 460
        self.height = 400
        self._center_window()

        # 프레임
        self.frame = ctk.CTkFrame(self)
        self.frame.pack(fill="both", expand=True, padx=20, pady=20)

        # 제목 (부서 정보 등)
        self.lbl_title = ctk.CTkLabel(
            self.frame, text=title, 
            font=theme_manager.get_font(14, "bold"),
            text_color=theme_manager.MAIN_COLOR if hasattr(theme_manager, 'MAIN_COLOR') else "#1F6AA5"
        )
        self.lbl_title.pack(pady=(0, 10))

        # 메시지 (VOC 제목)
        self.lbl_msg = ctk.CTkLabel(
            self.frame, text=msg, 
            font=theme_manager.get_font(12),
            wraplength=350
        )
        self.lbl_msg.pack(pady=(0, 20), expand=True)

        # 버튼 영역
        self.btn_frame = ctk.CTkFrame(self.frame, fg_color="transparent")
        self.btn_frame.pack(fill="x", side="bottom")

        self.btn_close = ctk.CTkButton(
            self.btn_frame, text="닫기", width=100, 
            fg_color="gray", command=self.destroy
        )
        self.btn_close.pack(side="right", padx=5)

        self.btn_list = ctk.CTkButton(
            self.btn_frame, text="전체내역 보기", width=120,
            command=self._on_open_list
        )
        self.btn_list.pack(side="right", padx=5)

        self.btn_view = ctk.CTkButton(
            self.btn_frame, text="상세보기", width=100,
            command=self._on_view
        )
        self.btn_view.pack(side="right", padx=5)

        # 단건 알림이 아닐 경우 상세보기 비활성화
        if not self.voc_id:
            self.btn_view.configure(state="disabled")

        # 포커스 강제
        self.lift()
        self.focus_force()

    def _on_view(self):
        if self.voc_id:
            self.controller.open_list_view(focus_id=self.voc_id)
        self.destroy()

    def _on_open_list(self):
        self.controller.open_list_view()
        self.destroy()

    def _center_window(self):
        self.update_idletasks()
        x = (self.winfo_screenwidth() // 2) - (self.width // 2)
        y = (self.winfo_screenheight() // 2) - (self.height // 2)
        self.geometry(f"{self.width}x{self.height}+{x}+{y}")
