# SPDX-FileCopyrightText: 2022 Chris Heywood
#
# SPDX-License-Identifier: GPL-3.0-or-later

from gettext import gettext as _

from gi.repository import Adw, Gio, GObject, Gtk

from iotas.category_header_bar import CategoryHeaderBar
from iotas.category_treeview_list_store import CategoryTreeViewListStore
from iotas.note import Note
from iotas.ui_utils import show_error_dialog


@Gtk.Template(resource_path="/org/gnome/World/Iotas/ui/selection_header_bar.ui")
class SelectionHeaderBar(Adw.Bin):
    __gtype_name__ = "SelectionHeaderBar"

    __gsignals__ = {
        "set-favourite": (GObject.SignalFlags.RUN_FIRST, None, ()),
        "clear-favourite": (GObject.SignalFlags.RUN_FIRST, None, ()),
        "export": (GObject.SignalFlags.RUN_FIRST, None, ()),
        "delete": (GObject.SignalFlags.RUN_FIRST, None, ()),
        "categories-changed": (GObject.SignalFlags.RUN_FIRST, None, ()),
        "abort": (GObject.SignalFlags.RUN_FIRST, None, ()),
        "select-all": (GObject.SignalFlags.RUN_FIRST, None, ()),
    }

    _stack: Gtk.Stack = Gtk.Template.Child()
    _main_header_bar: Adw.HeaderBar = Gtk.Template.Child()
    _category_header_bar: CategoryHeaderBar = Gtk.Template.Child()
    _count_label: Gtk.Label = Gtk.Template.Child()

    def __init__(self) -> None:
        super().__init__()
        self.__active = False
        self.__selected: list[Note] = []
        self._category_header_bar.connect(
            "categories-changed", lambda _o: self.emit("categories-changed")
        )
        self._category_header_bar.connect("abort", lambda _o: self.emit("abort"))
        self.__setup_actions()

    def setup(self, model: CategoryTreeViewListStore) -> None:
        """Perform initial setup.

        :param CategoryTreeViewListStore model: Categories model
        """
        self._category_header_bar.setup(model)

    def activate(self) -> None:
        """Activate header bar."""
        self._stack.set_visible_child(self._main_header_bar)
        self.active = True
        self.__selected = []
        self.__refresh()

    def set_note_selected(self, note: Note, select: bool) -> None:
        """Add or remove note from selection.

        :param Note note: Note to select
        :param bool select: Whether to add or remote from selection
        """
        if select:
            if note not in self.__selected:
                self.__selected.append(note)
        else:
            if note in self.__selected:
                self.__selected.remove(note)
        self.__refresh()

    def get_selected(self) -> list[Note]:
        """Get selected notes.

        :return: Selected notes
        :rtype: list[Note]
        """
        return list(self.__selected)

    def get_categories_changeset(self) -> list[tuple[Note, str]]:
        """Get categories changeset.

        :return: Selected of category-changed notes and their previous categories
        :rtype: list[tuple[Note, str]]
        """
        return self._category_header_bar.take_changeset()

    def edit_category_for_selection(self) -> None:
        """Edit the category for the selected notes."""
        if not self.__selected:
            return

        msg = _("Unable to change category on read-only note")
        if self.__error_for_readonly_in_selection(msg):
            return

        self._stack.set_visible_child(self._category_header_bar)
        self._category_header_bar.activate(self.__selected)

    def toggle_favourite_on_selection(self) -> None:
        """Toggle favourites for the selected notes.

        If a collection of notes with mixed favourite states are selected the destination state
        is the less common one in those selected.
        """
        if not self.__selected:
            return

        set = len([x for x in self.__selected if x.favourite])
        unset = len(self.__selected) - set
        if set >= unset:
            self.emit("clear-favourite")
        else:
            self.emit("set-favourite")

    def export_selection(self) -> None:
        """Export selection."""
        if not self.__selected:
            return

        self.emit("export")

    def handle_delete_keyboard_shortcut(self) -> None:
        """Handle pressed delete keyboard shortcut."""
        if self._stack.get_visible_child() == self._category_header_bar:
            self._category_header_bar.clear_and_apply()
        else:
            self.__delete_selection()

    @GObject.Property(type=bool, default=False)
    def active(self) -> bool:
        return self.__active

    @active.setter
    def set_active(self, value: bool) -> None:
        self.__active = value

    def __setup_actions(self) -> None:
        action_group = Gio.SimpleActionGroup.new()

        action = Gio.SimpleAction.new("categorise")
        action.connect("activate", lambda _a, _p: self.edit_category_for_selection())
        action_group.add_action(action)

        action = Gio.SimpleAction.new("favourite")
        action.connect("activate", lambda _a, _p: self.toggle_favourite_on_selection())
        action_group.add_action(action)

        action = Gio.SimpleAction.new("export")
        action.connect("activate", lambda _a, _p: self.export_selection())
        action_group.add_action(action)

        action = Gio.SimpleAction.new("delete")
        action.connect("activate", lambda _a, _p: self.__delete_selection())
        action_group.add_action(action)

        self.insert_action_group("selection", action_group)
        self.__action_group = action_group

    def __delete_selection(self) -> None:
        """Delete the selected notes."""
        if not self.__selected:
            return
        self.emit("delete")

    def __refresh(self) -> None:
        # Translators: Description, {} is a number
        self._count_label.set_text(_("{} Selected").format(len(self.__selected)))

        if self._stack.get_visible_child() == self._category_header_bar:
            self._category_header_bar.replace_notes(self.__selected)

        for action in self.__action_group.list_actions():
            action = self.__action_group.lookup(action)
            action.set_property("enabled", self.__selected)

    def __error_for_readonly_in_selection(self, message: str) -> bool:
        have_readonly = False
        for note in self.__selected:
            if note.read_only:
                have_readonly = True
                break
        if have_readonly:
            # Translators: Description, alert
            show_error_dialog(self, _("Unable to change category on read-only note"))
        return have_readonly
