#!/usr/bin/env python3
"""
View for the settings view.
"""

import gettext

import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk  #pylint: disable=wrong-import-position

from flatpak_handler import FlatPakInfo

_ = gettext.translation('deb-software', fallback=True).gettext

class SettingsView:
    """
    View for the settings view.
    """
    def __init__(self, model):
        self.model = model
        self.flatpak_info = FlatPakInfo()

    def create_repositories_row(self) -> tuple[Gtk.ListBoxRow, list[Gtk.CheckButton]]:
        """
        Create a row for the repositories.
        
        Returns:
            Gtk.ListBoxRow: A GTK listboxrow for the repositories.
        """
        repos = self.model.get_repositories()
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        box.add_css_class("repo_box")

        label = Gtk.Label(label=_("Repositories"))
        label.add_css_class("settings_title")
        label.set_halign(Gtk.Align.START)
        box.append(label)

        checkbuttons = []
        for uri, enabled in repos:
            check = Gtk.CheckButton(label=uri)
            check.set_active(enabled)
            check.uri = uri
            checkbuttons.append(check)
            box.append(check)

        row = Gtk.ListBoxRow()
        row.add_css_class("listbox-row")
        row.set_child(box)
        return row, checkbuttons

    def create_interval_row(self) -> tuple[Gtk.ListBoxRow, Gtk.ComboBoxText]:
        """
        Create a row for the Flatpak check interval.
        
        Returns:
            Gtk.ListBoxRow: A GTK listboxrow for the Flatpak check interval.
        """
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        box.add_css_class("check_interval_box")

        label = Gtk.Label(label=_("Update Flatpak check interval"))
        label.add_css_class("settings_title")
        label.set_halign(Gtk.Align.START)
        box.append(label)

        combo = Gtk.ComboBoxText()
        combo.set_halign(Gtk.Align.START)
        combo.append_text(_("Never"))
        combo.append_text(_("Hourly"))
        combo.append_text(_("Daily"))
        combo.append_text(_("Weekly"))
        combo.interval_values = [0, 3600, 86400, 604800]

        interval = self.model.get_check_interval()
        combo.set_active(
            combo.interval_values.index(interval)
            if interval in combo.interval_values
            else 0
        )

        box.append(combo)

        row = Gtk.ListBoxRow()
        row.add_css_class("listbox-row")
        row.set_child(box)
        return row, combo

    def create_update_indicator_row(self) -> tuple[Gtk.ListBoxRow, Gtk.CheckButton]:
        """
        Create a row for the update checker.
        
        Returns:
            Gtk.ListBoxRow: A GTK listboxrow for the update checker.
        """
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        box.add_css_class("update_indicator_box")

        label = Gtk.Label(label=_("Update checker"))
        label.add_css_class("settings_title")
        label.set_halign(Gtk.Align.START)
        box.append(label)

        check = Gtk.CheckButton(label=_("Enable the update checker"))
        check.set_active(self.model.is_update_indicator_active())
        box.append(check)

        row = Gtk.ListBoxRow()
        row.add_css_class("listbox-row")
        row.set_child(box)
        return row, check

    def create_install_flatpak_row(self) -> tuple[Gtk.ListBoxRow, Gtk.CheckButton]:
        """
        Create a row for installing Flatpaks.

        Returns:
            Gtk.ListBoxRow: A GTK listboxrow for installing Flatpaks.
        """
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        box.add_css_class("update_indicator_box")

        label = Gtk.Label(label=_("Backends"))
        label.add_css_class("settings_title")
        label.set_halign(Gtk.Align.START)
        box.append(label)

        check = Gtk.CheckButton(label=_("Enable Flatpak"))
        check.set_active(self.flatpak_info.is_flatpak_enabled())
        box.append(check)

        row = Gtk.ListBoxRow()
        row.add_css_class("listbox-row")
        row.set_child(box)
        return row, check
