"""
Sidebar — server list with search, add/edit/delete buttons.
"""

import customtkinter as ctk
from core.i18n import t
from gui.widgets.status_badge import StatusBadge


class Sidebar(ctk.CTkFrame):
    def __init__(self, master, store, on_select=None):
        super().__init__(master, width=250, corner_radius=0)
        self.store = store
        self.on_select = on_select
        self._selected_alias: str | None = None
        self._server_frames: dict[str, ctk.CTkFrame] = {}
        self._badges: dict[str, StatusBadge] = {}

        self.pack_propagate(False)

        # Title
        self.title_label = ctk.CTkLabel(self, text=t("servers"), font=ctk.CTkFont(size=18, weight="bold"))
        self.title_label.pack(padx=15, pady=(15, 5))

        # Search
        self.search_var = ctk.StringVar()
        self.search_var.trace_add("write", lambda *_: self._refresh_list())
        self.search_entry = ctk.CTkEntry(self, placeholder_text=t("search"), textvariable=self.search_var)
        self.search_entry.pack(fill="x", padx=10, pady=(5, 10))

        # Server list
        self.list_frame = ctk.CTkScrollableFrame(self, fg_color="transparent")
        self.list_frame.pack(fill="both", expand=True, padx=5, pady=0)

        # Buttons
        btn_frame = ctk.CTkFrame(self, fg_color="transparent")
        btn_frame.pack(fill="x", padx=10, pady=10)
        self.add_btn = ctk.CTkButton(btn_frame, text=t("add"), width=70, height=30, command=self._on_add)
        self.add_btn.pack(side="left", padx=(0, 3))
        self.edit_btn = ctk.CTkButton(btn_frame, text=t("edit"), width=70, height=30, fg_color="#6b7280", command=self._on_edit)
        self.edit_btn.pack(side="left", padx=3)
        self.del_btn = ctk.CTkButton(btn_frame, text=t("delete"), width=70, height=30, fg_color="#ef4444", hover_color="#dc2626", command=self._on_delete)
        self.del_btn.pack(side="right", padx=(3, 0))

        # Callbacks for add/edit/delete — set by app.py
        self.add_callback = None
        self.edit_callback = None
        self.delete_callback = None

        # Subscribe to store changes
        self.store.subscribe(self._refresh_list)
        self._refresh_list()

    def update_language(self):
        self.title_label.configure(text=t("servers"))
        self.search_entry.configure(placeholder_text=t("search"))
        self.add_btn.configure(text=t("add"))
        self.edit_btn.configure(text=t("edit"))
        self.del_btn.configure(text=t("delete"))

    def _refresh_list(self):
        # Clear
        for widget in self.list_frame.winfo_children():
            widget.destroy()
        self._server_frames.clear()
        self._badges.clear()

        search = self.search_var.get().lower()
        servers = self.store.get_all()

        for server in servers:
            alias = server["alias"]
            ip = server["ip"]
            stype = server.get("type", "ssh")

            if search and search not in alias.lower() and search not in ip.lower():
                continue

            frame = ctk.CTkFrame(self.list_frame, cursor="hand2", height=45)
            frame.pack(fill="x", padx=2, pady=2)
            frame.pack_propagate(False)

            # Status badge
            badge = StatusBadge(frame, status=self.store.get_status(alias))
            badge.pack(side="left", padx=(10, 5), pady=10)
            self._badges[alias] = badge

            # Info
            info = ctk.CTkFrame(frame, fg_color="transparent")
            info.pack(side="left", fill="both", expand=True, padx=5)

            name_label = ctk.CTkLabel(info, text=alias, font=ctk.CTkFont(size=13, weight="bold"), anchor="w")
            name_label.pack(fill="x")
            detail = f"{ip}  [{stype}]"
            detail_label = ctk.CTkLabel(info, text=detail, font=ctk.CTkFont(size=10), text_color="#9ca3af", anchor="w")
            detail_label.pack(fill="x")

            # Click handlers
            for widget in [frame, info, name_label, detail_label, badge]:
                widget.bind("<Button-1>", lambda e, a=alias: self._select(a))

            self._server_frames[alias] = frame

        self._highlight_selected()

    def _select(self, alias: str):
        self._selected_alias = alias
        self._highlight_selected()
        if self.on_select:
            self.on_select(alias)

    def _highlight_selected(self):
        for alias, frame in self._server_frames.items():
            if alias == self._selected_alias:
                frame.configure(fg_color=("#3b82f6", "#1d4ed8"))
            else:
                frame.configure(fg_color=("gray85", "gray20"))

    def get_selected(self) -> str | None:
        return self._selected_alias

    def update_statuses(self):
        for alias, badge in self._badges.items():
            badge.set_status(self.store.get_status(alias))

    def _on_add(self):
        if self.add_callback:
            self.add_callback()

    def _on_edit(self):
        if self.edit_callback and self._selected_alias:
            self.edit_callback(self._selected_alias)

    def _on_delete(self):
        if self.delete_callback and self._selected_alias:
            self.delete_callback(self._selected_alias)
