"""
Info tab — display server details, edit button.
"""

import customtkinter as ctk
from core.i18n import t


class InfoTab(ctk.CTkFrame):
    # Map field keys to i18n keys
    _FIELD_KEYS = ["alias", "ip", "port", "user", "type", "notes", "status"]
    _FIELD_I18N = {
        "alias": "info_alias",
        "ip": "info_ip",
        "port": "info_port",
        "user": "info_user",
        "type": "info_type",
        "notes": "info_notes",
        "status": "info_status",
    }

    def __init__(self, master, store, edit_callback=None):
        super().__init__(master, fg_color="transparent")
        self.store = store
        self.edit_callback = edit_callback
        self._current_alias: str | None = None

        # Header
        self.header = ctk.CTkLabel(self, text=t("no_server_selected_info"), font=ctk.CTkFont(size=20, weight="bold"))
        self.header.pack(padx=20, pady=(20, 10))

        # Info card
        self.card = ctk.CTkFrame(self)
        self.card.pack(fill="x", padx=20, pady=10)

        self._fields: dict[str, ctk.CTkLabel] = {}
        self._field_labels: dict[str, ctk.CTkLabel] = {}
        for key in self._FIELD_KEYS:
            row = ctk.CTkFrame(self.card, fg_color="transparent")
            row.pack(fill="x", padx=15, pady=4)
            label = ctk.CTkLabel(row, text=t(self._FIELD_I18N[key]), width=80, anchor="w",
                        font=ctk.CTkFont(size=12), text_color="#9ca3af")
            label.pack(side="left")
            val = ctk.CTkLabel(row, text="-", anchor="w", font=ctk.CTkFont(size=13))
            val.pack(side="left", fill="x", expand=True)
            self._field_labels[key] = label
            self._fields[key] = val

        # Edit button
        self.edit_btn = ctk.CTkButton(self, text=t("edit_server_btn"), command=self._on_edit)
        self.edit_btn.pack(pady=15)

    def set_server(self, alias: str | None):
        self._current_alias = alias
        self.refresh()

    def refresh(self):
        if not self._current_alias:
            self.header.configure(text=t("no_server_selected_info"))
            for v in self._fields.values():
                v.configure(text="-")
            return

        server = self.store.get_server(self._current_alias)
        if not server:
            return

        self.header.configure(text=server["alias"])
        self._fields["alias"].configure(text=server.get("alias", "-"))
        self._fields["ip"].configure(text=server.get("ip", "-"))
        self._fields["port"].configure(text=str(server.get("port", 22)))
        self._fields["user"].configure(text=server.get("user", "root"))
        self._fields["type"].configure(text=server.get("type", "ssh").upper())
        self._fields["notes"].configure(text=server.get("notes", "-") or "-")

        status = self.store.get_status(self._current_alias)
        color = {"online": "#22c55e", "offline": "#ef4444"}.get(status, "#9ca3af")
        self._fields["status"].configure(text=status.upper(), text_color=color)

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