from PyQt6.QtWidgets import QWidget, QApplication
from zapzap.resources.CSRButtonThemeProvider import CSRButtonThemeProvider
from zapzap.resources.TrayIcon import TrayIcon
from zapzap.services.SettingsManager import SettingsManager
from zapzap.services.SysTrayManager import SysTrayManager
from zapzap.services.ThemeManager import ThemeManager
from zapzap.views.ui_page_appearance import Ui_PageAppearance


class PageAppearance(QWidget, Ui_PageAppearance):
    """Classe para gerenciar a página de configurações de aparência."""

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)

        self._load_settings()
        self._configure_signals()

    def _load_settings(self):
        """Carrega as configurações iniciais da interface."""
        # Configurações gerais
        self.browser_sidebar.setChecked(
            SettingsManager.get("system/sidebar", True))
        self.mainwindow_menu.setChecked(
            SettingsManager.get("system/menubar", True))
        self.scaleComboBox.setCurrentText(
            f"{SettingsManager.get('system/scale', 100)} %")
        self.tray_groupBox.setChecked(
            SettingsManager.get("system/tray_icon", True))

        self.notificationCounter.setChecked(SettingsManager.get(
            "system/notificationCounter", False))

        self.csr_groupBox.setChecked(SettingsManager.get("system/csr", False))
        self.csr_theme_comboBox.clear()
        self.csr_theme_comboBox.addItems(CSRButtonThemeProvider.available_theme_names())
        csr_theme = str(SettingsManager.get("system/csr_button_theme", "default")).lower()
        theme_index = self.csr_theme_comboBox.findText(csr_theme)
        self.csr_theme_comboBox.setCurrentIndex(theme_index if theme_index >= 0 else 0)
        self.csr_show_minimize_checkBox.setChecked(SettingsManager.get("system/csr_show_minimize_button", True))
        self.csr_show_maximize_checkBox.setChecked(SettingsManager.get("system/csr_show_maximize_button", True))
        csr_direction = str(SettingsManager.get("system/csr_buttons_direction", "right")).strip().lower()
        self.csr_direction_comboBox.setCurrentIndex(1 if csr_direction == "left" else 0)

        # Configurações de tema
        theme_mode = SettingsManager.get(
            "system/theme", ThemeManager.Type.Auto.value)
        self._set_selected_radio(theme_mode, {
            ThemeManager.Type.Auto.value: self.theme_auto_radioButton,
            ThemeManager.Type.Light.value: self.theme_light_radioButton,
            ThemeManager.Type.Dark.value: self.theme_dark_radioButton,
        })

        # Configuração de colunas do grid
        self._setup_grid_options()
        self.gridColsComboBox.setCurrentText(
            str(SettingsManager.get("system/grid_cols", 2)))

    def _setup_grid_options(self):
        """Adiciona opção de colunas do grid à página de aparência."""
        from PyQt6.QtWidgets import QComboBox, QLabel, QHBoxLayout, QWidget
        from gettext import gettext as _
        
        self.grid_widget = QWidget(parent=self.frame)
        self.grid_layout = QHBoxLayout(self.grid_widget)
        self.grid_layout.setContentsMargins(0, 0, 0, 0)
        
        self.grid_label = QLabel(_("Grid Columns:"), parent=self.grid_widget)
        self.gridColsComboBox = QComboBox(parent=self.grid_widget)
        self.gridColsComboBox.addItems(["2", "3", "4"])
        
        self.grid_layout.addWidget(self.grid_label)
        self.grid_layout.addWidget(self.gridColsComboBox)
        self.grid_layout.addStretch()
        
        # Inserir no layout principal (abaixo da escala)
        self.verticalLayout_2.insertWidget(6, self.grid_widget)
        self.gridColsComboBox.currentTextChanged.connect(self._handle_grid_cols)

    def _handle_grid_cols(self, text):
        """Salva a quantidade de colunas do grid."""
        SettingsManager.set("system/grid_cols", int(text))

        # Configurações de bandeja
        tray_mode = SettingsManager.get(
            "system/tray_theme", TrayIcon.Type.Default.value)
        self._set_selected_radio(tray_mode, {
            TrayIcon.Type.Default.value: self.tray_default_radioButton,
            TrayIcon.Type.SLight.value: self.tray_slight_radioButton,
            TrayIcon.Type.SDark.value: self.tray_sdark_radioButton,
        })

        # Configurações de tema
        theme_mode = SettingsManager.get(
            "system/theme", ThemeManager.Type.Auto.value)
        self._set_selected_radio(theme_mode, {
            ThemeManager.Type.Auto.value: self.theme_auto_radioButton,
            ThemeManager.Type.Light.value: self.theme_light_radioButton,
            ThemeManager.Type.Dark.value: self.theme_dark_radioButton,
        })

    def _configure_signals(self):
        """Conecta os sinais dos widgets aos respectivos manipuladores."""
        # Sinais gerais
        self.browser_sidebar.clicked.connect(self._handle_sidebar)
        self.mainwindow_menu.clicked.connect(self._handle_menubar)
        self.scaleComboBox.currentTextChanged.connect(self._handle_scale)
        self.tray_groupBox.toggled.connect(SysTrayManager.set_state)
        self.notificationCounter.clicked.connect(
            self._handle_remove_notification_indicator)

        # Sinais de radio buttons
        self.tray_default_radioButton.toggled.connect(self._handle_tray_mode)
        self.tray_slight_radioButton.toggled.connect(self._handle_tray_mode)
        self.tray_sdark_radioButton.toggled.connect(self._handle_tray_mode)
        self.theme_auto_radioButton.toggled.connect(self._handle_theme_mode)
        self.theme_light_radioButton.toggled.connect(self._handle_theme_mode)
        self.theme_dark_radioButton.toggled.connect(self._handle_theme_mode)
        self.csr_groupBox.toggled.connect(self._handle_csr_enabled)
        self.csr_theme_comboBox.currentTextChanged.connect(self._handle_csr_theme)
        self.csr_show_minimize_checkBox.toggled.connect(self._handle_csr_show_minimize)
        self.csr_show_maximize_checkBox.toggled.connect(self._handle_csr_show_maximize)
        self.csr_direction_comboBox.currentTextChanged.connect(self._handle_csr_direction)

    @staticmethod
    def _set_selected_radio(selected_value, radio_map):
        """Seleciona o botão de rádio correspondente ao valor dado."""
        radio_button = radio_map.get(selected_value)
        if radio_button:
            radio_button.setChecked(True)

    def _handle_sidebar(self):
        """Salva e aplica a configuração da barra lateral."""
        QApplication.instance().getWindow().set_sidebar_visible(
            self.browser_sidebar.isChecked()
        )

    def _handle_menubar(self):
        """Salva e aplica a configuração da barra de menu."""
        SettingsManager.set("system/menubar", self.mainwindow_menu.isChecked())
        QApplication.instance().getWindow().settings_menubar()

    def _handle_scale(self, text):
        """Salva a escala escolhida."""
        scale_value = ''.join(filter(str.isdigit, text))
        SettingsManager.set("system/scale", scale_value)

    def _handle_tray_mode(self):
        """Salva e aplica a configuração do tema da bandeja."""
        tray_mode_map = {
            self.tray_default_radioButton: TrayIcon.Type.Default,
            self.tray_slight_radioButton: TrayIcon.Type.SLight,
            self.tray_sdark_radioButton: TrayIcon.Type.SDark,
        }
        selected_tray_mode = self._get_selected_radio(tray_mode_map)
        if selected_tray_mode:
            SysTrayManager.set_theme(selected_tray_mode)

    def _handle_theme_mode(self):
        """Salva e aplica a configuração do tema visual."""
        theme_mode_map = {
            self.theme_auto_radioButton: ThemeManager.Type.Auto,
            self.theme_light_radioButton: ThemeManager.Type.Light,
            self.theme_dark_radioButton: ThemeManager.Type.Dark,
        }
        selected_theme_mode = self._get_selected_radio(theme_mode_map)
        if selected_theme_mode:
            ThemeManager.set_theme(selected_theme_mode)

    @staticmethod
    def _get_selected_radio(radio_map):
        """Retorna o valor associado ao botão de rádio selecionado."""
        for radio_button, value in radio_map.items():
            if radio_button.isChecked():
                return value
        return None

    def _handle_remove_notification_indicator(self):
        SettingsManager.set("system/notificationCounter",
                            self.notificationCounter.isChecked())
        SysTrayManager.refresh()

    def _refresh_csr_buttons(self):
        window = QApplication.instance().getWindow()
        if getattr(window, "is_csr_wrapper", False):
            window.refresh_csr_button_preferences()

    def _handle_csr_enabled(self, enabled):
        SettingsManager.set("system/csr", enabled)

    def _handle_csr_theme(self, theme_name):
        SettingsManager.set("system/csr_button_theme", theme_name)
        self._refresh_csr_buttons()

    def _handle_csr_show_minimize(self, enabled):
        SettingsManager.set("system/csr_show_minimize_button", enabled)
        self._refresh_csr_buttons()

    def _handle_csr_show_maximize(self, enabled):
        SettingsManager.set("system/csr_show_maximize_button", enabled)
        self._refresh_csr_buttons()

    def _handle_csr_direction(self, direction_label):
        direction = "left" if direction_label.strip().lower() == "left" else "right"
        SettingsManager.set("system/csr_buttons_direction", direction)
        self._refresh_csr_buttons()
