import pyxel

from .settings import (
    TOOL_BUCKET,
    TOOL_CIRC,
    TOOL_CIRCB,
    TOOL_PENCIL,
    TOOL_RECT,
    TOOL_RECTB,
    TOOL_SELECT,
)
from .widgets import Widget


class EditorBase(Widget):
    # Variables:
    #   help_message_var
    #
    # Events:
    #   undo (data)
    #   redo (data)
    #   drop (filename)

    _TOOL_HELP = {
        TOOL_SELECT: "SELECT:S",
        TOOL_PENCIL: "PENCIL:P",
        TOOL_RECTB: "RECTANGLE:R",
        TOOL_RECT: "FILLED-RECT:SHIFT+R",
        TOOL_CIRCB: "CIRCLE:C",
        TOOL_CIRC: "FILLED-CIRC:SHIFT+C",
        TOOL_BUCKET: "BUCKET:B",
    }

    def __init__(self, parent):
        super().__init__(parent, 0, 0, 0, 0, is_visible=False)
        self._history_list = []
        self._history_index = 0
        self.copy_var("help_message_var", parent)

    # Public methods

    @property
    def can_undo(self):
        return self._history_index > 0

    @property
    def can_redo(self):
        return self._history_index < len(self._history_list)

    def undo(self):
        if not self.can_undo:
            return

        self._history_index -= 1
        self.trigger_event("undo", self._history_list[self._history_index])

    def redo(self):
        if not self.can_redo:
            return

        self.trigger_event("redo", self._history_list[self._history_index])
        self._history_index += 1

    def add_history(self, data):
        self._history_list = self._history_list[: self._history_index]
        self._history_list.append(data)
        self._history_index += 1

    def reset_history(self):
        self._history_list = []
        self._history_index = 0

    def add_number_picker_help(self, number_picker):
        number_picker.dec_button.add_event_listener(
            "mouse_hover", self.__on_number_picker_dec_mouse_hover
        )
        number_picker.inc_button.add_event_listener(
            "mouse_hover", self.__on_number_picker_inc_mouse_hover
        )

    def check_tool_button_shortcuts(self):
        if any(pyxel.btn(k) for k in (pyxel.KEY_CTRL, pyxel.KEY_ALT, pyxel.KEY_GUI)):
            return

        if pyxel.btnp(pyxel.KEY_S):
            self.tool_var = TOOL_SELECT
        elif pyxel.btnp(pyxel.KEY_P):
            self.tool_var = TOOL_PENCIL
        elif pyxel.btnp(pyxel.KEY_R):
            self.tool_var = TOOL_RECT if pyxel.btn(pyxel.KEY_SHIFT) else TOOL_RECTB
        elif pyxel.btnp(pyxel.KEY_C):
            self.tool_var = TOOL_CIRC if pyxel.btn(pyxel.KEY_SHIFT) else TOOL_CIRCB
        elif pyxel.btnp(pyxel.KEY_B):
            self.tool_var = TOOL_BUCKET

    def add_tool_button_help(self, tool_button):
        tool_button.add_event_listener("mouse_hover", self.__on_tool_button_mouse_hover)

    # Event handlers

    def __on_number_picker_dec_mouse_hover(self, _x, _y):
        self.help_message_var = "-10:SHIFT+CLICK"

    def __on_number_picker_inc_mouse_hover(self, _x, _y):
        self.help_message_var = "+10:SHIFT+CLICK"

    def __on_tool_button_mouse_hover(self, x, y):
        value = self._tool_button.check_value(x, y)
        self.help_message_var = self._TOOL_HELP.get(value, "")
