# SPDX-FileCopyrightText: 2024 Luca Weiss
# SPDX-License-Identifier: GPL-3.0-only

from gi.repository import Adw
from gi.repository import Gio
from gi.repository import Gtk
from . import esim
from . import pages
from . import widgets
import os


@Gtk.Template(resource_path="/eu/lucaweiss/lpa-gtk/mainwindow.ui")
class MainWindow(Adw.ApplicationWindow):
    __gtype_name__ = "LpaGtkMainWindow"

    profile_add_button = Gtk.Template.Child()
    sim_slot_group = Gtk.Template.Child()
    nav_view = Gtk.Template.Child()
    nav_home = Gtk.Template.Child()
    nav_profile = Gtk.Template.Child()
    nav_download = Gtk.Template.Child()
    nav_chipinfo = Gtk.Template.Child()
    list_page = Gtk.Template.Child()
    detail_page = Gtk.Template.Child()
    download_page = Gtk.Template.Child()
    chipinfo_page = Gtk.Template.Child()

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        backends = esim.ESimBackend.get_operational_backends()

        if not backends:
            self.list_page.set_no_backend()
            self.sim_slot_group.hide()
            self.profile_add_button.hide()
            return

        # Hide the slot selector if only one slot is available, we only need it for two slots
        if len(backends) == 1:
            self.sim_slot_group.hide()

        available_slots = []
        for backend in backends:
            slot = backend.get_slot()
            print(f"Available slot: {slot}")
            available_slots.append(slot)

        self.active_slot = backends[0].get_slot()
        self.list_page.set_active_slot(self.active_slot)
        self.download_page.set_active_slot(self.active_slot)
        self.detail_page.set_active_slot(self.active_slot)
        self.chipinfo_page.set_active_slot(self.active_slot)
        print(f"Initial active slot: {self.active_slot}")

        self.esim_manager = esim.ESimManager(backends)

        self.list_page.init_notification_task(available_slots, self.esim_manager)

        self.profile_add_button.connect("clicked", lambda *_: self.nav_view.push(self.nav_download))
        self.sim_slot_group.connect("notify::active", self.sim_slot_changed)

        self.nav_home.connect("showing", lambda *_: self.list_page.update())
        self.nav_chipinfo.connect("showing", lambda *_: self.chipinfo_page.update())
        self.nav_download.connect("hidden", lambda *_: self.download_page.reset())

        self.detail_page.connect("profile-deleted", lambda *_: self.nav_view.pop())

        self.list_page.connect("profile-selected", self.profile_selected)

        self._add_action("chipinfo", None, lambda *_: self.nav_view.push(self.nav_chipinfo))

        self.list_page.set_manager(self.esim_manager)
        self.download_page.set_manager(self.esim_manager)
        self.detail_page.set_manager(self.esim_manager)
        self.chipinfo_page.set_manager(self.esim_manager)

        self.list_page.update()

    def _add_action(self, name, parameter_type, activate_cbk):
        action = Gio.SimpleAction.new(name, parameter_type)
        action.connect("activate", activate_cbk)
        self.add_action(action)
        return action

    def sim_slot_changed(self, toggle_group, _position):
        self.active_slot = int(toggle_group.get_active_name())
        self.list_page.set_active_slot(self.active_slot)
        self.download_page.set_active_slot(self.active_slot)
        self.detail_page.set_active_slot(self.active_slot)
        print(f"Selected SIM slot: {self.active_slot}")
        self.list_page.update()

    def profile_selected(self, _page, profile: esim.ESimProfile):
        self.detail_page.set_profile(profile)
        self.nav_view.push(self.nav_profile)
