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


ROWS = 100
COLS = 200
# GRID_LINE_COLOR = Color.CHARCOAL
# CENTER_GUIDE_COLOR = Color.DARK_GREY
# HOVER_GRIDBOX_COLOR = Color.YELLOW
# CROSSHAIRS_COLOR = Color.BURLY_WOOD
STATIC_HIGHLIGHTED_CELLS = {Vector2(1, 1): Color.PURPLE,
                            Vector2(5, 5): Color.YELLOW}
GRID_TOGGLE_POLICY = TogglePolicy(key='toggled', toggle_type=BooleanToggleable, default=False)

class GridGame_Basic(GameTemplate):

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

        self.grid = RectGrid(nRows=ROWS, nColumns=COLS)
        self.grid_handler = PyGridHandler(
            grid_provider=lambda: self.grid,
            screen=self.screen,
            get_game_area_rect_callback=lambda: self.game_area_rectangle,
            get_highlights_callback=lambda: STATIC_HIGHLIGHTED_CELLS,
            get_highlighted_rows=lambda: self.highlighted_rows(),
            get_highlighted_cols=lambda: self.highlighted_cols(),
            get_toggled_key_colors=lambda: {GRID_TOGGLE_POLICY.key: Color.RED}
        )

    def highlighted_rows(self):
        return {
            3: Color.ANTIQUE_WHITE
        }
    def highlighted_cols(self):
        return {
            3: Color.PEACH_PUFF
        }

    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_1, event_type=InputEventType.DOWN)], callback=lambda x: self.grid_handler.toggle_surface([GridSurfaceType.GRID_SURFACE_ID])),
                InputAction([InputEvent(PyKeys.K_2, event_type=InputEventType.DOWN)], callback=lambda x: self.grid_handler.toggle_surface([GridSurfaceType.HIGHLIGHTS_SURFACE_ID])),
                InputAction([InputEvent(PyMouse.LEFT, event_type=InputEventType.DOWN)], callback=lambda x: self.toggle_grid_at_mouse_pos()),
                InputAction([InputEvent(PyKeys.K_KP_PLUS, event_type=InputEventType.DOWN)], callback=lambda x: self.grid_handler.show_all()),
                InputAction([InputEvent(PyKeys.K_KP_MINUS, event_type=InputEventType.DOWN)], callback=lambda x: self.grid_handler.hide_all()),
        ])

    def toggle_grid_at_mouse_pos(self):
        mgpo = self.grid_handler.mouse_grid_pos()
        self.grid.act_at_loc(row=int(mgpo.x), column=int(mgpo.y), policies=[GRID_TOGGLE_POLICY])
        self.grid_handler.invalidate()

    def redraw(self):
        self.grid_handler.redraw()

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


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