from coopgame.gameTemplate import GameTemplate
import coopgame.pygamehelpers as help
from cooptools.colors import Color
from coopgame.grids_graphs.pygridhandler import PyGridHandler
from cooptools.toggles import BooleanToggleable
from coopgame.handleKeyPressedArgs import InputState, InputStateHandler, InputAction, InputEvent, InputEventType, basic_key_actions, CallbackPackage
from coopgame.pygame_k_constant_names import PyKeys, PyMouse
from cooptools.sectors.grids import RectGrid
from cooptools.dictPolicies import TogglePolicy
from coopstructs.geometry.vectors.vectorN import Vector2
from coopgame.grids_graphs.pygridhandler import GridSurfaceType
from coopgame.surfaceManager import SurfaceManager, SurfaceRegistryArgs
from cooptools.coopEnum import CoopEnum, auto
import pygame

BACK_COLOR = Color.YELLOW


class SurfaceLayer(CoopEnum):
    BACK = auto()
    BG0 = auto()
    BG1 = auto()
    FG0 = auto()
    FG1 = auto()
    CLOUDS = auto()


class SurfaceManager_Basic(GameTemplate):

    def __init__(self):
        super().__init__(input_state_handler=self.define_input_handler())
        self._surface_manager = SurfaceManager()

        self._surface_manager.register_surface_ids(
            surface_draw_callbacks=[
                SurfaceRegistryArgs(id=SurfaceLayer.BACK.name, callback=self._get_surface_back),
                SurfaceRegistryArgs(id=SurfaceLayer.BG0.name, callback=self._get_surface_bg0)
            ]
        )


    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_r, event_type=InputEventType.DOWN)], callback=lambda x: self.redraw()),
                InputAction([InputEvent(PyKeys.K_0, event_type=InputEventType.DOWN)], callback=lambda x: self._surface_manager.toggle_visible([SurfaceLayer.BACK.name])),
                InputAction([InputEvent(PyKeys.K_1, event_type=InputEventType.DOWN)], callback=lambda x: self._surface_manager.toggle_visible([SurfaceLayer.BG0.name])),
                InputAction([InputEvent(PyKeys.K_2, event_type=InputEventType.DOWN)], callback=lambda x: self._surface_manager.toggle_visible([SurfaceLayer.BG1.name])),
                InputAction([InputEvent(PyKeys.K_KP_PLUS, event_type=InputEventType.DOWN)], callback=lambda x: self._surface_manager.show_all()),
                InputAction([InputEvent(PyKeys.K_KP_MINUS, event_type=InputEventType.DOWN)], callback=lambda x: self._surface_manager.hide_all()),
        ])


    def _get_surface_back(self):
        surf = help.init_surface(self.screen.get_size())
        surf.fill(color=BACK_COLOR.value)
        return surf

    def _get_surface_bg0(self):
        bg = pygame.image.load(r"C:\Users\tburns\PycharmProjects\coopgame\coopgame\assets\backgrounds\desert_spikes_dawn.png")
        return bg

    def draw(self, frames: int, debug_mode: bool = False):
        self._surface_manager.render(self.screen)

    def redraw(self):
        self._surface_manager.redraw()


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