# Python imports
import sys, os, signal
from dataclasses import dataclass

# Lib imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib

# Application imports
from shellfm.windows.controller import WindowController
from plugins.plugins_controller import PluginsController


@dataclass(slots=True)
class State:
    wid: int  = None
    tid: int  = None
    tab: type = None
    icon_grid: gi.overrides.Gtk.IconView  = None
    store: gi.overrides.Gtk.ListStore     = None
    selected_files: [] = None
    to_copy_files:  [] = None
    to_cut_files:   [] = None
    warning_alert: type = None


class Controller_Data:
    """ Controller_Data contains most of the state of the app at ay given time. It also has some support methods. """
    __slots__ = "settings", "builder", "logger", "keybindings", "trashman", "fm_controller", "window", "window1", "window2", "window3", "window4"

    def setup_controller_data(self) -> None:
        self.builder             = settings.get_builder()
        self.keybindings         = settings.get_keybindings()

        self.fm_controller       = WindowController()
        self.plugins             = PluginsController()
        self.fm_controller_data  = self.fm_controller.get_state_from_file()

        self.window             = settings.get_main_window()
        self.window1            = self.builder.get_object("window_1")
        self.window2            = self.builder.get_object("window_2")
        self.window3            = self.builder.get_object("window_3")
        self.window4            = self.builder.get_object("window_4")
        self.message_popup_widget = self.builder.get_object("message_popup_widget")
        self.message_text_view  = self.builder.get_object("message_text_view")
        self.message_buffer     = self.builder.get_object("message_buffer")
        self.arc_command_buffer = self.builder.get_object("arc_command_buffer")

        self.exists_file_rename_bttn = self.builder.get_object("exists_file_rename_bttn")
        self.warning_alert      = self.builder.get_object("warning_alert")
        self.new_file_menu      = self.builder.get_object("new_file_menu")
        self.edit_file_menu     = self.builder.get_object("edit_file_menu")
        self.file_exists_dialog = self.builder.get_object("file_exists_dialog")
        self.exists_file_label  = self.builder.get_object("exists_file_label")
        self.exists_file_field  = self.builder.get_object("exists_file_field")
        self.path_menu          = self.builder.get_object("path_menu")
        self.path_entry         = self.builder.get_object("path_entry")

        self.bottom_size_label       = self.builder.get_object("bottom_size_label")
        self.bottom_file_count_label = self.builder.get_object("bottom_file_count_label")
        self.bottom_path_label       = self.builder.get_object("bottom_path_label")

        self.trash_files_path        = f"{GLib.get_user_data_dir()}/Trash/files"
        self.trash_info_path         = f"{GLib.get_user_data_dir()}/Trash/info"
        self.icon_theme              = settings.get_icon_theme()

        self.notebooks          = [self.window1, self.window2, self.window3, self.window4]
        self.selected_files     = []
        self.to_copy_files      = []
        self.to_cut_files       = []
        self.soft_update_lock   = {}
        self.dnd_left_primed    = 0

        self.single_click_open  = False
        self.is_pane1_hidden    = False
        self.is_pane2_hidden    = False
        self.is_pane3_hidden    = False
        self.is_pane4_hidden    = False

        self.override_drop_dest = None

        self.cancel_creation    = False
        self.skip_edit          = False
        self.cancel_edit        = False
        self.ctrl_down          = False
        self.shift_down         = False
        self.alt_down           = False

        self.success_color      = settings.get_success_color()
        self.warning_color      = settings.get_warning_color()
        self.error_color        = settings.get_error_color()

        # sys.excepthook = self.custom_except_hook
        self.window.connect("delete-event", self.tear_down)
        GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, self.tear_down)

        self.window.show()
        if settings.is_debug():
            self.window.set_interactive_debugging(True)


    def get_current_state(self) -> State:
        '''
        Returns the state info most useful for any given context and action intent.

                Parameters:
                        a (obj): self

                Returns:
                        state (obj): State
        '''
        state                = State()
        state.wid, state.tid = self.fm_controller.get_active_wid_and_tid()
        state.tab            = self.get_fm_window(state.wid).get_tab_by_id(state.tid)
        state.icon_grid      = self.builder.get_object(f"{state.wid}|{state.tid}|icon_grid")
        state.store          = state.icon_grid.get_model()
        state.warning_alert  = self.warning_alert

        selected_files       = state.icon_grid.get_selected_items()
        if selected_files:
            state.selected_files = self.format_to_uris(state.store, state.wid, state.tid, selected_files, True)

        # if self.to_copy_files:
        #     state.to_copy_files  = self.format_to_uris(state.store, state.wid, state.tid, self.to_copy_files, True)
        #
        # if self.to_cut_files:
        #     state.to_cut_files   = self.format_to_uris(state.store, state.wid, state.tid, self.to_cut_files, True)

        event_system.emit("update_state_info_plugins", state)
        return state


    def clear_console(self) -> None:
        ''' Clears the terminal screen. '''
        os.system('cls' if os.name == 'nt' else 'clear')

    def call_method(self, _method_name: str, data: type = None) -> type:
        '''
        Calls a method from scope of class.

                Parameters:
                        a (obj): self
                        b (str): method name to be called
                        c (*): Data (if any) to be passed to the method.
                                Note: It must be structured according to the given methods requirements.

                Returns:
                        Return data is that which the calling method gives.
        '''
        method_name = str(_method_name)
        method      = getattr(self, method_name, lambda data: f"No valid key passed...\nkey={method_name}\nargs={data}")
        return method(data) if data else method()

    def has_method(self, obj, name) -> type:
        ''' Checks if a given method exists. '''
        return callable(getattr(obj, name, None))

    def clear_children(self, widget: type) -> None:
        ''' Clear children of a gtk widget. '''
        for child in widget.get_children():
            widget.remove(child)
