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 { ResourceManager } from "./ResourceManager"
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

    // const lottie = Lottie.loadAnimation({
    //   container: document.getElementById("layaContainer") as Element,
    //   renderer: 'svg',
    //   loop: true,
    //   autoplay: true,
    //   path: 'effect/lottie_trophy_light/lottie_trophy_light.json'
    // })
  }

  private UIRoot: Laya.Panel
  public getUIRoot(): Laya.Node {
    if (!this.UIRoot) {
      this.UIRoot = this.owner.getChildByName("UIRoot") as Laya.Panel
    }
    return this.UIRoot
  }

  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 {
    const resources: Array<{ url: string, type?: any, lazy?: boolean }> = []
    resources.push({ url: respath.font_Bold, type: Laya.Loader.TTF })
    resources.push({ url: respath.font_Medium, type: Laya.Loader.TTF })
    resources.push({ url: respath.font_Regular, type: Laya.Loader.TTF })
    resources.push({ url: respath.font_SemiBold, type: Laya.Loader.TTF })
    Laya.loader.load(resources, Laya.Handler.create(this, (obj: object) => { // 第二个参数：成功回调
      console.log("预加载字体结束>>>>", obj)
      if (config.H_SCREEN) {
        this.loadTopBarUI()
      } else {
        this.loadHomeUI()
      }
    }), Laya.Handler.create(this, (resProg: number) => { // 第三个参数：进度回调
      console.log("预加载资源进度>>>>", resProg)
    }))
  }

  public loadHomeUI(): void {
    ResourceManager.getInstance().loadPrefab(respath.home_ui_res, (go: any) => {
      const prefab = go.create()
      this.getUIRoot().addChild(prefab)
      ResourceManager.getInstance().startSchedule()
    })
  }

  public loadTopBarUI(): void {
    ResourceManager.getInstance().loadPrefab(respath.topbar_ui_res, (go: any) => {
      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
    ResourceManager.getInstance().loadPrefab(respath.dc_ui_res(), (go: any) => {
      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 {
    ResourceManager.getInstance().loadPrefab(respath.trophy_claim_ui_res(), (go: any) => {
      const prefab = go.create()
      const obj = this.getUIRoot().addChild(prefab).getComponent(TrophyClaim)
      obj.onSetShow(record)
    })
  }

  public loadTrophyUI(): void {
    this.mask.visible = true
    ResourceManager.getInstance().loadPrefab(respath.trophy_ui_res(), (go: any) => {
      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
    ResourceManager.getInstance().loadPrefab(respath.trophy_show_ui_res(), (go: any) => {
      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 {
      ResourceManager.getInstance().loadPrefab(respath.stage_ui_res(), (go: any) => {
        const prefab = go.create()
        this.stage = this.getUIRoot().addChild(prefab).getComponent(Stage)
        this.stage.onLoadStage(StorageManager.getInstance().loadStage(stageID))
        ResourceManager.getInstance().startSchedule()
      })
    }
  }

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

  public loadGamePauseUI(doStage: DOStage): void {
    this.mask.visible = true
    ResourceManager.getInstance().loadPrefab(respath.gamepause_ui_res(), (go: any) => {
      const prefab = go.create()
      const ui = this.getUIRoot().addChild(prefab).getComponent(GamePause)
      ui.onSetStageInfo(doStage)
      this.mask.visible = false
    })
  }

  public loadGameOverUI(doStage: DOStage): void {
    this.mask.visible = true
    ResourceManager.getInstance().loadPrefab(respath.gameover_ui_res(), (go: any) => {
      const prefab = go.create()
      const ui = this.getUIRoot().addChild(prefab).getComponent(GameOver)
      ui.onSetStageInfo(doStage)
      this.mask.visible = false
    })
  }

  public loadGameDoneUI(isClassic: boolean, doStage: DOStage): void {
    this.mask.visible = true
    ResourceManager.getInstance().loadPrefab(respath.gamedone_ui_res(isClassic), (go: any) => {
      const prefab = go.create()
      const ui = this.getUIRoot().addChild(prefab).getComponent(GameDone)
      ui.onSetStageInfo(doStage)
      this.mask.visible = false
    })
  }
}
