import { Container, Loader, Texture, interaction } from "pixi.js"

import { Point } from "../Datatypes/MathTypes/point"
import { InfoSystem } from "../Systems/UI/InfoSystem"
import { Button } from "./Button"
import { AdventurerMenu } from "./AdventurerMenu"
import { canvasWidth, canvasHeight } from "../constants"

export class PlanningMenu extends Container {
  system: InfoSystem

  adventurers: AdventurerMenu

  constructor(system: InfoSystem) {
    super()

    this.system = system

    this.adventurers = new AdventurerMenu(system)
    this.adventurers.position = new Point(0, 0)
    this.adventurers.width = canvasWidth
    this.adventurers.height = canvasHeight - canvasWidth - 18
    this.addChild(this.adventurers)

    this.setupButtons()
    this.visible = false
  }

  setupButtons() {
    let buttons = Loader.shared.resources["AdventureButtons"].textures
    let buttonDef: {
      texture: Texture
      tooltip?: string
      action?: (event: interaction.InteractionEvent) => void
      context?: any
    }[] = [
      {
        texture: buttons["Clear"],
        tooltip: "Discard Adventure",
        action: this.system.abortPlans,
        context: this.system,
      },
      {
        texture: buttons["People"],
        tooltip: "Adventurers",
        action: () => (this.adventurers.visible = true),
        context: this,
      },
      { texture: buttons["Items"], tooltip: "Supplies" },
      { texture: buttons["Dir"], tooltip: "Travel Direction" },
      { texture: buttons["Go"], tooltip: "Start Adventure!" },
      { texture: null },
      { texture: null },
    ]

    let container = new Container()
    this.addChild(container)
    container.position = new Point(0, 126 - 18)

    for (let i = 0; i < buttonDef.length; i++) {
      let def = buttonDef[i]
      let button = new Button(def.texture)
      if (def.action) button.onClick(def.action, def.context)
      if (def.tooltip) button.setTooltip(def.tooltip)

      container.addChild(button)
      button.position = new Point(i * 18, 0)
    }
  }
}
