import Phaser from "phaser";
import { type CombatantActor, type Stats, type UIUpdatePayload } from "../core/types";
import { HudComponent } from "./components/HudComponent";
import { MenuComponent } from "./components/MenuComponent";
import { InventoryOverlay } from "./components/InventoryOverlay";
import { CharacterOverlay } from "./components/CharacterOverlay";
import { DeathOverlay } from "./components/DeathOverlay";
import { PersistentButtonsComponent } from "./components/PersistentButtonsComponent";
import { QuickSlotComponent } from "./components/QuickSlotComponent";

export default class GameUI extends Phaser.Scene {
  private hud: HudComponent;
  private menu: MenuComponent;
  private inventory: InventoryOverlay;
  private character: CharacterOverlay;
  private death: DeathOverlay;
  private persistentButtons: PersistentButtonsComponent;
  private quickSlots: QuickSlotComponent;

  constructor() {
    super({ key: "GameUI" });
    this.hud = new HudComponent(this);
    this.menu = new MenuComponent(this);
    this.inventory = new InventoryOverlay(this);
    this.character = new CharacterOverlay(this);
    this.death = new DeathOverlay(this);
    this.persistentButtons = new PersistentButtonsComponent(this);
    this.quickSlots = new QuickSlotComponent(this);
  }


  create() {
    this.hud.create();
    this.menu.create();
    this.inventory.create();
    this.character.create();
    this.death.create();
    this.persistentButtons.create();
    this.quickSlots.create();

    const gameScene = this.scene.get("GameScene");

    
    // Listen for updates from GameScene
    gameScene.events.on("update-ui", (payload: UIUpdatePayload) => {
      this.updateUI(payload);
    });

    gameScene.events.on("toggle-menu", () => {
        this.menu.toggle();
        this.emitMenuStates();
    });
    
    gameScene.events.on("toggle-inventory", () => {
        const open = this.inventory.toggle();
        if (open) {
            this.menu.setVisible(false);
            this.character.setVisible(false);
        }
        this.emitMenuStates();
    });

    gameScene.events.on("toggle-character", () => {
        const open = this.character.toggle();
        if (open) {
            this.menu.setVisible(false);
            this.inventory.setVisible(false);
        }
        this.emitMenuStates();
    });

    gameScene.events.on("close-menu", () => {
      this.menu.setVisible(false);
      this.inventory.setVisible(false);
      this.character.setVisible(false);
      this.emitMenuStates();
    });

    gameScene.events.on("restart-game", () => {
        this.death.hide();
    });

    // Request initial state update
    gameScene.events.emit("request-ui-update");
  }

  private emitMenuStates() {
    const gameScene = this.scene.get("GameScene");
    gameScene.events.emit("menu-toggled", this.menu.isOpen);
    gameScene.events.emit("inventory-toggled", this.inventory.isOpen);
    gameScene.events.emit("character-toggled", this.character.isOpen);
  }


  showDeathScreen(data: { floor: number; gold: number; stats: Stats }) {
    this.death.show(data);
  }

  private updateUI(payload: UIUpdatePayload) {
    const { world, playerId, floorIndex, uiState } = payload;
    const player = world.actors.get(playerId) as CombatantActor;
    if (!player) return;

    this.hud.update(player.stats, floorIndex);
    this.inventory.update(player);
    this.character.update(player);
    this.quickSlots.update(player, uiState.targetingItemId);
  }
}
