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

from gettext import gettext as _
from gi.repository import GObject, Gtk


@Gtk.Template(resource_path="/org/gnome/World/Iotas/ui/editor_search_entry.ui")
class EditorSearchEntry(Gtk.Box):
    __gtype_name__ = "EditorSearchEntry"
    __gsignals__ = {
        "activate": (GObject.SignalFlags.RUN_FIRST, None, ()),
    }

    text: Gtk.Text = Gtk.Template.Child()
    _info: Gtk.Label = Gtk.Template.Child()

    def __init__(self) -> None:
        self.__occurrence_count = 0
        self.__occurrence_position = 0
        self.text.connect("notify::text", lambda _o, _v: self.__on_text_changed())
        super().__init__()

    def set_occurrence_count(self, count: int) -> None:
        """Set the number of search term occurrences.

        :param int count: New value
        """
        self.__occurrence_count = count
        if count or self.text.get_text() == "":
            self.remove_css_class("error")
        else:
            self.add_css_class("error")
        self.__update_position()

    def set_occurrence_position(self, position: int) -> None:
        """Set the current search term occurrence.

        :param int position: New value
        """
        self.__occurrence_position = position
        self.__update_position()

    def select_all_and_focus(self) -> None:
        """Select and focus all text."""
        if self.text.get_text() != "":
            self.text.select_region(0, -1)
        self.text.grab_focus()

    def show_working(self) -> None:
        """Show feedback that search is running.

        Rarely necessary but can occur if searching immediately after opening large notes.
        """
        self._info.set_label("…")
        self.remove_css_class("error")

    def get_text(self) -> str:
        """Fetch the text from the entry.

        :return: Unaccented form
        :rtype: str
        """
        return self.text.get_text()

    def set_text(self, text: str) -> None:
        """Set the text on the entry.

        :param str text: New value
        """
        return self.text.set_text(text)

    @Gtk.Template.Callback()
    def _on_activate(self, _obj: GObject.Object) -> None:
        self.emit("activate")

    def __on_text_changed(self) -> None:
        if self.get_text() == "":
            self.__occurrence_count = 0
            self.__occurrence_position = 0
            self._info.set_label("")
        else:
            self._info.set_label("…")

    def __update_position(self) -> None:
        if self.__occurrence_count == 0:
            occurrence_str = ""
        else:
            # Translators: Description, {0} the current position in {1} a number of search results
            occurrence_str = _("{0} of {1}").format(
                self.__occurrence_position, self.__occurrence_count
            )
        self._info.set_label(occurrence_str)
