from coopgame.gameTemplate import GameTemplate
import coopgame.pygamehelpers as help
from cooptools.colors import Color
from coopgame.handleKeyPressedArgs import InputState, InputStateHandler, InputAction, InputEvent, InputEventType, basic_key_actions, CallbackPackage
from coopgame.pygame_k_constant_names import PyKeys, PyMouse
from cooptools.transform import Transform
from typing import Tuple

SCROLL_SPEED = 5
ZOOM_SCALE = 0.75

class TransformGame_Basic(GameTemplate):

    def __init__(self):
        self._camera = Transform(scale=[100, 100, 100])
        super().__init__(input_state_handler=self.define_input_handler())

    def define_input_handler(self):
        return InputStateHandler(
            quit_callback_package=CallbackPackage(down=lambda x: self.quit()),
            debug_callback_package=CallbackPackage(down=lambda x: self.debug_mode_toggle.toggle()),
            fullscreen_callback_package=CallbackPackage(down=lambda x: self.toggle_fullscreen()),
            key_actions=[
                InputAction([InputEvent(PyKeys.K_w, event_type=InputEventType.HELD)], callback=lambda x: self._camera.Translation.update(delta_vector=(0, SCROLL_SPEED, 0))),
                InputAction([InputEvent(PyKeys.K_a, event_type=InputEventType.HELD)], callback=lambda x: self._camera.Translation.update(delta_vector=(-SCROLL_SPEED, 0, 0))),
                InputAction([InputEvent(PyKeys.K_s, event_type=InputEventType.HELD)], callback=lambda x: self._camera.Translation.update(delta_vector=(0, -SCROLL_SPEED, 0))),
                InputAction([InputEvent(PyKeys.K_d, event_type=InputEventType.HELD)], callback=lambda x: self._camera.Translation.update(delta_vector=(SCROLL_SPEED, 0, 0))),
                InputAction([InputEvent(PyKeys.K_UP, event_type=InputEventType.HELD)], callback=lambda x: self._camera.Translation.update(delta_vector=(0, SCROLL_SPEED, 0))),
                InputAction([InputEvent(PyKeys.K_LEFT, event_type=InputEventType.HELD)], callback=lambda x: self._camera.Translation.update(delta_vector=(-SCROLL_SPEED, 0, 0))),
                InputAction([InputEvent(PyKeys.K_DOWN, event_type=InputEventType.HELD)], callback=lambda x: self._camera.Translation.update(delta_vector=(0, -SCROLL_SPEED, 0))),
                InputAction([InputEvent(PyKeys.K_RIGHT, event_type=InputEventType.HELD)], callback=lambda x: self._camera.Translation.update(delta_vector=(SCROLL_SPEED, 0, 0))),
                InputAction([InputEvent(PyMouse.SCROLLUP, event_type=InputEventType.DOWN)], callback=lambda x: self._camera.Scale.update(scalar=1/ZOOM_SCALE)),
                InputAction([InputEvent(PyMouse.SCROLLDOWN, event_type=InputEventType.DOWN)], callback=lambda x: self._camera.Scale.update(scalar=ZOOM_SCALE)),
        ])

    def draw(self, frames: int, debug_mode: bool = False):
        help.draw_polygon(self.screen,
                          points=[
                              (0, 1, 0),
                              (1, 0, 0),
                              (0.75, -1, 0),
                              (-0.75, -1, 0),
                              (-1, 0, 0)
                          ],
                          color=Color.YELLOW,
                          draw_transform_matrix=self._camera.transform_matrix(
                              inversions=['Y']
                          )
                          )

if __name__ == "__main__":
    g = TransformGame_Basic()
    g.main()