from coopgame.gameTemplate import GameTemplate
from coopgame.pyZoneHandler import PyZoneHandler
import pygame
import coopgame.pygamehelpers as help
from coopstructs.toggles import BooleanToggleable
from coopstructs.vectors import Vector2

DUMMY_NEXT_ZONE = "DUMMY"

class ZoneDrawerGame(GameTemplate):

    def __init__(self):
        super().__init__()

        self.zone_handler = PyZoneHandler()
        self.draw_label_toggle = BooleanToggleable()

        self._zn_idx = -1

    def register_keys(self):
        # CREATE NEW ZONE
        self.register_action_to_keys((pygame.K_n,),
                                     lambda: self.handle_n(),
                                     react_while_holding=False)

        # TOGGLE ZONE LAYERS
        self.register_action_to_keys((pygame.K_UP,),
                                     lambda: self.zone_handler.layer_selector.visibility_toggle(),
                                     react_while_holding=False)

        # LABELS
        self.register_action_to_keys((pygame.K_l,),
                                     lambda: self.draw_label_toggle.toggle(),
                                     react_while_holding=False)

    def get_next_id(self):
        self._zn_idx += 1
        return str(self._zn_idx)

    def draw(self, frames:int, debug_mode: bool = False):
        self.zone_handler.draw_zones(self.screen,
                                     draw_zone_label=self.draw_label_toggle.value
                                     )

    def init_dummy_zone(self):
        if DUMMY_NEXT_ZONE in self.zone_handler.zm.zones:
            self.zone_handler.zm.delete_zone(DUMMY_NEXT_ZONE)
        self.zone_handler.init_new_zone(lambda:  DUMMY_NEXT_ZONE,
                                        initial_points=self.zone_handler.zm.zones[str(self._zn_idx)].boundary_points,
                                        set_active=False)

    def handle_n(self):
        self.zone_handler.init_new_zone(lambda: self.get_next_id())
        self.init_dummy_zone()

    def handle_left_mouse_down(self, pressed_keys):
        # set origin at mouse down point
        self.zone_handler.origin = help.mouse_pos_as_vector()


    def handle_left_mouse_up(self, pressed_keys):
        # make sure we have started a zone
        if self._zn_idx < 0: return

        # get mouse position
        mouse_pos = help.mouse_pos_as_vector()

        # determine if next point is to be placed between points based on location of mouse down (origin)
        bnds = self.zone_handler.zm.zones[str(self._zn_idx)].insersection_boundary_idxs_from_origin(
            mouse_pos, self.zone_handler.origin)

        at_idx = bnds[0][1] if bnds and len(bnds) > 0 else None
        print(bnds, at_idx)
        # add the new point
        self.zone_handler.add_to_active_zone(mouse_pos, at_idx=at_idx)

        # clear the origin
        self.zone_handler.origin = None

        # update dummy
        self.overwrite_dummy_zone_with_last_idx_zone(mouse_pos)

    def handle_right_mouse_up(self, pressed_keys):
        # get mouse position
        mouse_pos = help.mouse_pos_as_vector()

        self.zone_handler.remove_from_active_zone()
        self.overwrite_dummy_zone_with_last_idx_zone()

    def handle_hover_over(self, mouse_pos_as_vector: Vector2):
        if DUMMY_NEXT_ZONE in self.zone_handler.zm.zones \
                and self.zone_handler.origin \
                and mouse_pos_as_vector != self.zone_handler.origin:
            print(f"dragging")

            self.overwrite_dummy_zone_with_last_idx_zone(mouse_pos_as_vector)

    def overwrite_dummy_zone_with_last_idx_zone(self, mouse_pos_as_vector: Vector2 = None):
        bnds = self.zone_handler.zm.zones[str(self._zn_idx)].insersection_boundary_idxs_from_origin(
            mouse_pos_as_vector, self.zone_handler.origin)
        print(bnds)
        if mouse_pos_as_vector:
            self.zone_handler.zm.zones[DUMMY_NEXT_ZONE] = self.zone_handler.zm.zones[
                str(self._zn_idx)].with_additional_point(mouse_pos_as_vector,
                                                         at_idx=bnds[0][1] if bnds and len(bnds) > 0 else None)
            print(self.zone_handler.zm.zones[DUMMY_NEXT_ZONE])
            print(self.zone_handler.zm.zones[str(self._zn_idx)])
        else:
            self.zone_handler.zm.zones[DUMMY_NEXT_ZONE] = self.zone_handler.zm.zones[str(self._zn_idx)]



if __name__ == "__main__":

    game = ZoneDrawerGame()
    game.main()