import { Container, Graphics, Text } from 'pixi.js';
import {
    World,
    IsometricTileMap,
    isoToScreen,
    screenToIsoExact,
    DEFAULT_ISO,
    type Scene,
    type Camera
} from '@rpg/engine';
import { Game } from '../Game';
import { buildMeadowsMap } from '../data/map';
import { NPCS, type NpcDef } from '../data/npcs';
import { PlayerController } from '../systems/PlayerController';
import { DialogueSystem } from '../systems/DialogueSystem';
import { MenuScene, type SaveData } from './MenuScene';

/**
 * Локация «Выжженные луга»: карта, герой, NPC, диалоги, автосейв по Esc.
 */
export class LocationScene implements Scene {
    private world = new World();
    private map: IsometricTileMap;
    private player: PlayerController;
    private dialogue: DialogueSystem;
    private npcs: { def: NpcDef; view: Container }[] = [];
    private hint: Text;
    private mapLayer = new Container();
    private camera: Camera;

    constructor(
        private game: Game,
        save: SaveData | null
    ) {
        this.camera = game.camera;

        const data = buildMeadowsMap();
        this.map = new IsometricTileMap(data, new Map(), DEFAULT_ISO);
        this.mapLayer.addChild(this.map.view);
        this.game.renderer.worldRoot.addChild(this.mapLayer);

        const startTile = save?.pos ?? { x: 14, y: 14 };
        this.player = new PlayerController(this.world, this.map, startTile);
        this.mapLayer.addChild(this.player.view);

        for (const def of NPCS) {
            const view = this.makeNpcView(def);
            this.mapLayer.addChild(view);
            this.npcs.push({ def, view });
        }

        this.dialogue = new DialogueSystem(this.game.renderer.uiRoot, {
            width: 480,
            height: 270,
            margin: 8
        });
        this.dialogue.onDialogueFinished = (id) => this.onDialogueFinished(id);
        if (save?.flags) {
            Object.assign(this.dialogue.flags, save.flags);
        }

        // Камера: следим за героем, границы — по экранному размеру карты.
        const size = this.map.screenSize;
        this.camera.bounds = { width: size.width, height: size.height };
        const tile = this.player.currentTile();
        const c = this.tileCenter(tile.x, tile.y);
        this.camera.follow(c.x, c.y);

        this.hint = new Text({
            text: 'клик — идти · клик по NPC — говорить · Esc — меню',
            style: { fontFamily: 'monospace', fontSize: 8, fill: 0x999988 }
        });
        this.hint.position.set(6, 4);
        this.game.renderer.uiRoot.addChild(this.hint);
    }

    enter(): void {}

    exit(): void {
        this.mapLayer.destroy({ children: true });
        this.hint.destroy();
    }

    update(dt: number): void {
        const input = this.game.engine.input;
        if (this.dialogue.active) {
            // Во время диалога клик/пробел только листают реплики.
            if (input.getPointer().justPressed || input.isActionJustPressed('advance')) {
                this.dialogue.advance();
            }
        } else {
            if (input.isActionJustPressed('menu')) {
                this.saveAndExit();
                return;
            }
            const pointer = input.getPointer();
            if (pointer.justPressed) {
                this.handleWorldClick(pointer.x, pointer.y);
            }
        }

        this.player.update(dt);

        // Камера за героем.
        const tile = this.player.currentTile();
        const c = this.tileCenter(tile.x, tile.y);
        this.camera.follow(c.x, c.y);
    }

    render(): void {}

    /** Экранные координаты центра ромба тайла (с Origin карты). */
    private tileCenter(tx: number, ty: number): { x: number; y: number } {
        const p = isoToScreen(tx, ty, DEFAULT_ISO);
        return { x: p.x, y: p.y + DEFAULT_ISO.tileH / 2 };
    }

    private handleWorldClick(px: number, py: number): void {
        // Из координат экрана в мировые (учёт позиции камеры).
        const worldX = px - this.game.renderer.worldRoot.position.x;
        const worldY = py - this.game.renderer.worldRoot.position.y;

        const clicked = screenToIsoExact(
            worldX,
            worldY,
            this.map.data.width,
            this.map.data.height,
            DEFAULT_ISO
        );

        // Клик по тайлу NPC — диалог.
        if (clicked) {
            const npc = this.npcs.find(
                (n) => n.def.tile.x === clicked.x && n.def.tile.y === clicked.y
            );
            if (npc) {
                this.talkTo(npc.def);
                return;
            }
        }
        this.player.onWorldClick(worldX, worldY);
    }

    private talkTo(def: NpcDef): void {
        const met = !!this.dialogue.flags[def.flagKey];
        if (!met) {
            this.dialogue.flags[def.flagKey] = true;
        }
        this.dialogue.start(met ? def.dialogueRepeat : def.dialogueFirst);
    }

    private onDialogueFinished(_id: string): void {
        // Триггеры сюжета вешаются на id завершённого диалога (позже).
    }

    private makeNpcView(def: NpcDef): Container {
        const view = new Container();
        const body = new Graphics();
        const p = this.tileCenter(def.tile.x, def.tile.y);
        body.rect(-3, -9, 6, 9).fill(def.colors.cloak);
        body.rect(-3, -12, 6, 3).fill(def.colors.hat);
        body.rect(-1, -11, 2, 1).fill(0x1c1c22);
        view.position.set(p.x, p.y);
        view.addChild(body);

        // Маркер «с ним можно говорить»: восклицательный штрих.
        const marker = new Graphics();
        marker.rect(-1, -17, 2, 4).fill(0xf0d878);
        marker.rect(-1, -11, 2, 2).fill(0xf0d878);
        view.addChild(marker);
        return view;
    }

    private saveAndExit(): void {
        const pos = this.player.currentTile();
        this.game.saves.save('autosave', {
            pos,
            flags: this.dialogue.flags
        } satisfies SaveData);
        void this.game.scenes.replace(new MenuScene(this.game));
    }
}