"""All states the are used in-game"""

from __future__ import annotations

import attrs
import tcod.console
import tcod.constants
import tcod.event
from tcod.event import KeySym
from tcod.map import Map

import g
from game.components import Action, Gold, Graphic, Position
from game.constants import DIRECTION_KEYS, ACTION_KEYS
from game.screens import Push, Screen, ScreenResult
from game.tags import IsItem, IsPlayer, IsActor
from game.constants import WALL_CHAR
from game.screens import menu_screens
from game.world_tools import world_pos_to_map_pos, map_pos_to_world_pos

@attrs.define()
class MainScreen(Screen):
    """Primary in-game state."""

    def on_event(self, event: tcod.event.Event) -> ScreenResult:
        """Handle events for the in-game state."""
        (player,) = g.world.Q.all_of(tags=[IsPlayer])
        map: Map = g.world[None].components[Map]
        match event:
            case tcod.event.KeyDown(sym=sym) if sym in ACTION_KEYS:
                for entity in g.world.Q.all_of(components=[Action, Position]):
                    player_pos = player.components[Position]
                    if (player_pos - entity.components[Position]).length() < 2:
                        entity.components[Action](entity)
                        cam_map = world_pos_to_map_pos(player_pos)
                        map.compute_fov(cam_map.x, cam_map.y, 100)

            case tcod.event.Quit():
                raise SystemExit()
            case tcod.event.KeyDown(sym=sym) if sym in DIRECTION_KEYS:
                pos = player.components[Position]
                new_pos = pos + DIRECTION_KEYS[sym]
                map_pos = world_pos_to_map_pos(new_pos)
                if a := g.world.Q.all_of(components=[Action], tags=[new_pos]):
                    for aa in a:
                        aa.components[Action](aa)
                    map_pos = world_pos_to_map_pos(pos)
                    map.compute_fov(map_pos.x, map_pos.y, 100)
                    return None
                if not map.walkable[map_pos.y, map_pos.x]:
                    return None
                player.components[Position] = new_pos
                cam_map = world_pos_to_map_pos(new_pos)
                map.compute_fov(cam_map.x, cam_map.y, 100)

                # Auto pickup gold
                for gold in g.world.Q.all_of(components=[Gold], tags=[player.components[Position], IsItem]):
                    player.components[Gold] += gold.components[Gold]
                    text = f"Picked up {gold.components[Gold]}g, total: {player.components[Gold]}g"
                    g.world[None].components[("Text", str)] = text
                    gold.clear()
                return None
            case tcod.event.KeyDown(sym=KeySym.ESCAPE):
                return Push(menu_screens.MainMenu())
            case _:
                return None

    def on_draw(self, console: tcod.console.Console) -> None:
        """Draw the standard screen."""
        centers = [a.components[Position] for a in g.world.Q.all_of(tags=[IsPlayer])]
        camera_pos = sum(centers, start=Position(0,0))
        camera_pos = camera_pos.mod(len(centers))
        h = console.height//2
        w = console.width//2

        map: Map = g.world[None].components[Map]
        # TODO: eigentlich wäre andersrum rendern schöner
        # also nicht alle objekte zu rendern und dabei rauszufinden ob sie auf dem screen sind,
        # sondern über alle screen zellen laufen, über prüfen ob es im fov ist und dann gucken ob es dort ein objekt
        # gibt und es entsprechend rendern
        def draw(e_pos, e_graph):
            screen_pos = e_pos - camera_pos
            map_pos = world_pos_to_map_pos(e_pos)
            if (-w <= screen_pos.x < w\
                and -h <= screen_pos.y < h):
                if map.fov[map_pos.y, map_pos.x]:
                    graphic = e_graph
                else:
                    graphic = Graphic(0x2591, (50, 50, 50))
                if graphic.ch != 0:
                    console.rgb[["ch", "fg"]][screen_pos.y + h, screen_pos.x + w] = graphic.ch, graphic.fg

        # Draw walls
        for (y, row) in enumerate(map.walkable):
            for (x, val) in enumerate(row):
                pos = map_pos_to_world_pos(Position(x,y))
                draw(pos, Graphic(0) if val else Graphic(WALL_CHAR))
        # draw all entities that are not actors
        for entity in g.world.Q.all_of(components=[Position, Graphic]).none_of(tags=[IsActor]):
            draw(entity.components[Position], entity.components[Graphic])
        # draw all actors
        for actor in g.world.Q.all_of(components=[Position, Graphic], tags=[IsActor]).none_of(tags=[IsPlayer]):
            draw(actor.components[Position], entity.components[Graphic])
        # draw the player
        for player in g.world.Q.all_of(tags=[IsPlayer]):
            draw(player.components[Position], player.components[Graphic])
        if text := g.world[None].components.get(("Text", str)):
            console.print(x=0, y=console.height - 1, string=text, fg=(255, 255, 255), bg=(0, 0, 0))
