import type { TrophyRecord } from "../types/global"
import type { DOStage } from "./DOStage"
import { config } from "../constants/config"
import { respath } from "../constants/respath"
import { DailyChallenge } from "../views/dc/DailyChallenge"
import { TrophyClaim } from "../views/dc/TrophyClaim"
import { TrophyRoom } from "../views/dc/TrophyRoom"
import { TrophyShow } from "../views/dc/TrophyShow"
import { GameDone } from "../views/GameDone"
import { GameOver } from "../views/GameOver"
import { GamePause } from "../views/GamePause"
import { Stage } from "../views/Stage"
import { TopBar } from "../views/TopBar"
import { StorageManager } from "./StorageManager"

const { regClass, property } = Laya

@regClass()
export class UIManager extends Laya.Script {
  declare owner: Laya.Scene

  private static _instance: UIManager
  public static getInstance(): UIManager {
    return UIManager._instance
  }

  onAwake(): void {
    UIManager._instance = this
  }

  private UIRoot: Laya.Panel
  public getUIRoot(): Laya.Node {
    if (!this.UIRoot) {
      this.UIRoot = this.owner.getChildByName("UIRoot") as Laya.Panel
      if (config.H_SCREEN) {
        this.UIRoot.width = config.DESIGN_HEIGHT
        this.UIRoot.height = config.DESIGN_WIDTH
      } else {
        this.UIRoot.width = config.DESIGN_WIDTH
        this.UIRoot.height = config.DESIGN_HEIGHT
      }
    }
    return this.UIRoot
  }

  private topbar: TopBar
  private stage: Stage
  private dc_ui: DailyChallenge
  private trophyRoom: TrophyRoom

  onStart(): void {
    if (config.H_SCREEN) {
      this.loadTopBarUI()
    } else {
      this.loadHomeUI()
    }
  }

  public loadHomeUI(): void {
    Laya.loader.load(respath.home_ui_res).then((go) => {
      const prefab = go.create()
      this.getUIRoot().addChild(prefab)
    })
  }

  public loadTopBarUI(): void {
    Laya.loader.load(respath.topbar_ui_res).then((go) => {
      const prefab = go.create()
      this.topbar = this.getUIRoot().addChild(prefab).getComponent(TopBar)
    })
  }

  public setTopbarTo(isClassic: boolean): void {
    if (this.topbar) {
      this.topbar.onClickTab(isClassic)
    }
  }

  public loadDCUI(): void {
    Laya.loader.load(respath.dc_ui_res()).then((go) => {
      const prefab = go.create()
      this.dc_ui = this.getUIRoot().addChild(prefab).getComponent(DailyChallenge)
      this.dc_ui.loadWithMonth()
    })
  }

  public closeDCUI(): void {
    if (this.dc_ui) {
      this.dc_ui.owner.destroy()
      this.dc_ui = null
    }
  }

  public loadTrophyClaimUI(record: TrophyRecord): void {
    Laya.loader.load(respath.trophy_claim_ui_res()).then((go) => {
      const prefab = go.create()
      const obj = this.getUIRoot().addChild(prefab).getComponent(TrophyClaim)
      obj.onSetShow(record)
    })
  }

  public loadTrophyUI(): void {
    Laya.loader.load(respath.trophy_ui_res()).then((go) => {
      const prefab = go.create()
      this.trophyRoom = this.getUIRoot().addChild(prefab).getComponent(TrophyRoom)
    })
  }

  public closeTrophyUI(): void {
    if (this.trophyRoom) {
      this.trophyRoom.owner.destroy()
      this.trophyRoom = null
    }
  }

  public loadTrophyShowUI(record: TrophyRecord): void {
    Laya.loader.load(respath.trophy_show_ui_res()).then((go) => {
      const prefab = go.create()
      const obj = this.getUIRoot().addChild(prefab).getComponent(TrophyShow)
      obj.onSetShow(record)
    })
  }

  public loadStageUI(stageID: string): void {
    if (this.stage) {
      this.stage.onLoadStage(StorageManager.getInstance().loadStage(stageID))
    } else {
      Laya.loader.load(respath.stage_ui_res()).then((go) => {
        const prefab = go.create()
        this.stage = this.getUIRoot().addChild(prefab).getComponent(Stage)
        this.stage.onLoadStage(StorageManager.getInstance().loadStage(stageID))
      })
    }
  }

  public closeStageUI(): void {
    if (this.stage) {
      this.stage.owner.destroy()
      this.stage = null
    }
  }

  public loadGamePauseUI(doStage: DOStage): void {
    Laya.loader.load(respath.gamepause_ui_res()).then((go) => {
      const prefab = go.create()
      const ui = this.getUIRoot().addChild(prefab).getComponent(GamePause)
      ui.onSetStageInfo(doStage)
    })
  }

  public loadGameOverUI(doStage: DOStage): void {
    Laya.loader.load(respath.gameover_ui_res()).then((go) => {
      const prefab = go.create()
      const ui = this.getUIRoot().addChild(prefab).getComponent(GameOver)
      ui.onSetStageInfo(doStage)
    })
  }

  public loadGameDoneUI(isClassic: boolean, doStage: DOStage): void {
    Laya.loader.load(respath.gamedone_ui_res(isClassic)).then((go) => {
      const prefab = go.create()
      const ui = this.getUIRoot().addChild(prefab).getComponent(GameDone)
      ui.onSetStageInfo(doStage)
    })
  }
}
