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
  }

  @property(Boolean)
  public debug: boolean = false

  @property(Laya.Image)
  public mask: Laya.Image

  @property(Laya.Box)
  public btn_clean: Laya.Box

  onAwake(): void {
    UIManager._instance = this
    config.DEBUG = this.debug
    this.mask.on(Laya.Event.CLICK, this, () => {
      console.log("on click mask >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
    })
    this.mask.visible = false

    this.btn_clean.on(Laya.Event.CLICK, this, (evt: Laya.Event) => {
      console.log("btn_clean=", evt)
      StorageManager.getInstance().cleanAll()
    })
    this.btn_clean.visible = this.debug
  }

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

  private topbar: TopBar
  private stage: Stage
  private dc_ui: DailyChallenge
  private dc_year: number = -1
  private dc_month: number = -1
  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 setCurrentDCMonth(year: number, month: number): void {
    this.dc_year = year
    this.dc_month = month
    console.log("setCurrentDCMonth >>>>>>>>>>>>>>>>>>>>", year, month)
  }

  public loadDCUI(): void {
    this.mask.visible = true
    Laya.loader.load(respath.dc_ui_res()).then((go) => {
      const prefab = go.create()
      this.dc_ui = this.getUIRoot().addChild(prefab).getComponent(DailyChallenge)
      if (this.dc_year <= 0 || this.dc_month <= 0) {
        const now = new Date()
        this.dc_year = now.getFullYear()
        this.dc_month = now.getMonth() + 1
      }
      this.dc_ui.loadWithMonth(this.dc_year, this.dc_month)
      this.mask.visible = false
    })
  }

  public loadDCWtihMonth(year: number, month: number): void {
    this.closeTrophyUI()
    if (this.dc_ui) {
      this.dc_ui.loadWithMonth(year, month)
    }
  }

  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 {
    this.mask.visible = true
    Laya.loader.load(respath.trophy_ui_res()).then((go) => {
      const prefab = go.create()
      this.trophyRoom = this.getUIRoot().addChild(prefab).getComponent(TrophyRoom)
      this.mask.visible = false
    })
  }

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

  public loadTrophyShowUI(record: TrophyRecord): void {
    this.mask.visible = true
    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)
      this.mask.visible = false
    })
  }

  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)
    })
  }
}
