from coopgame.gameTemplate import GameTemplate
from coopgame import handleKeyPressedArgs as hkpa
from coopgame import pygamehelpers as pgh
from coopgame.label_drawing import label_drawing_utils as ldu
from coopgame.pointdrawing import point_draw_utils as pdu
from cooptools.colors import Color
from coopstructs.geometry import Rectangle
from cooptools.coopEnum import CardinalPosition
from cooptools.toggles import IntegerRangeToggleable
import math

SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 750

class RectDraw(GameTemplate):
    def __init__(self):

        self.rot_toggle = IntegerRangeToggleable(min=0, max=100, step_size=1, starting_value=0)

        input_state_handler = hkpa.InputStateHandler(key_actions=self.default_key_actions() + [
            hkpa.InputAction([hkpa.InputEvent(hkpa.PyKeys.K_LEFT,
                                              event_type=hkpa.InputEventType.HELD)],
                             callback=lambda x: self.rot_toggle.toggle(loop=True)),
            hkpa.InputAction([hkpa.InputEvent(hkpa.PyKeys.K_RIGHT,
                                              event_type=hkpa.InputEventType.HELD)],
                             callback=lambda x: self.rot_toggle.toggle(loop=True, reverse=True))
        ])



        super().__init__(input_state_handler=input_state_handler,
                         screen_width=SCREEN_WIDTH,
                         screen_height=SCREEN_HEIGHT)

    def draw_waypoints(self):
        points = {}
        for id, bg in self._model.BattleGroups.items():
            points = {**points,
                **{x.end_pos: pdu.DrawPointArgs(
                    color=Color.ORANGE,
                    radius=3
                )
                for x in bg.waypoint_follower.waypoints}
            }

            if len(bg.waypoint_follower.waypoints) > 0:
                pgh.draw_arrow(
                    self.screen,
                    start=bg.Transform.Translation.Vector,
                    end=bg.waypoint_follower.waypoints[0].end_pos,
                    color=Color.GRAY,
                    leader_line=True
                )

        pdu.draw_points(
            points=points,
            surface=self.screen
        )

    def _get_rot(self):
        return self.rot_toggle.value / 100 * 2 * math.pi

    def draw(self, frames: int, debug_mode: bool = False):
        rect = Rectangle.from_tuple((
            SCREEN_WIDTH // 2,
            SCREEN_HEIGHT // 2,
            50,
            25),
            anchor_cardinality=CardinalPosition.CENTER,
            rotation_rads=self._get_rot()
        )
        print(rect.RotationRads)
        pgh.draw_box(
            surface=self.screen,
            rect=rect,
            color=Color.BLUE,
            outline_color=Color.RED,
            anchor_draw_args=pgh.DrawPointArgs(
                color=Color.ORANGE,
            ),
            corner_draw_args=pgh.DrawPointArgs(
                color=Color.YELLOW,
                radius=3
            )
        )


if __name__ =="__main__":
    import logging
    from cooptools.loggingHelpers import BASE_LOG_FORMAT
    logging.basicConfig(level=logging.INFO, format=BASE_LOG_FORMAT)
    logging.getLogger('cooptools.physics.kinematic').setLevel(logging.WARN)
    logging.getLogger('cooptools.sectors.sectorTree.sectorTree_SectorComparer').setLevel(logging.WARN)
    game = RectDraw()
    game.main()

