import { config } from "../constants/config"
import { respath } from "../constants/respath"
import { G_getMonthInfo } from "../models/DCManager"
import { ResourceManager } from "../models/ResourceManager"
import { StorageManager } from "../models/StorageManager"
import { UIManager } from "../models/UIManager"
import { Utility_ConvertSecondToString } from "../utils/utility"
import { Difficulty } from "./Difficulty"

const { regClass, property } = Laya

@regClass()
export class Home extends Laya.Script {
  declare owner: Laya.Box

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

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

  @property(Laya.Label)
  public label_dc_title: Laya.Label

  @property(Laya.Label)
  public label_dc_date: Laya.Label

  @property(Laya.Sprite)
  public btn_play: Laya.Sprite

  @property(Laya.Sprite)
  public btn_continue: Laya.Sprite

  @property(Laya.Sprite)
  public btn_more: Laya.Sprite

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

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

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

  onStart(): void {
    const user = StorageManager.getInstance().getUser()
    const now = new Date()
    const nowYear = now.getFullYear()
    const nowMonth = now.getMonth() + 1
    const nowDay = now.getDate()
    const find = G_getMonthInfo(nowYear, nowMonth)
    if (find) {
      ResourceManager.getInstance().loadTexture(find.icon_res, this.icon_dc)
      if (user.get_trophyRecord(nowYear, nowMonth)) {
        ResourceManager.getInstance().loadTexture(respath.home_top_dc_bg_finish, this.bg_dc)
        this.label_dc_title.color = "#ffffff"
        this.label_dc_date.color = "#ffffff"
      } else {
        ResourceManager.getInstance().loadTexture(respath.home_top_dc_bg_normal, this.bg_dc)
        this.label_dc_title.color = "#2d3138"
        this.label_dc_date.color = "#2d3138"
      }
    } else {
      ResourceManager.getInstance().loadTexture(respath.home_top_dc_bg_normal, this.bg_dc)
      this.label_dc_title.color = "#2d3138"
      this.label_dc_date.color = "#2d3138"
    }

    const stageID = `${nowYear}-${nowMonth}-${nowDay}`
    this.label_dc_date.text = `${config.MONTH_ABBRS[nowMonth - 1]} ${nowDay}`

    const progress = user.get_done(stageID)
    if (progress <= 0) {
      this.btn_play.visible = true
      this.btn_continue.visible = false
      this.btn_more.visible = false
      this.btn_play.on(Laya.Event.CLICK, this, () => { // Play
        const stageName = `${config.MONTH_ABBRS[nowMonth - 1]} ${nowDay}`
        StorageManager.getInstance().newStage(stageID, config.STAGE_TYPE.DC, stageName)
        UIManager.getInstance().loadStageUI(stageID)
        this.owner.destroy()
      })
    } else if (progress >= 1) {
      this.btn_play.visible = false
      this.btn_continue.visible = false
      this.btn_more.visible = true
      this.btn_more.on(Laya.Event.CLICK, this, () => { // 更多
        UIManager.getInstance().loadDCUI()
        this.owner.destroy()
      })
    } else {
      this.btn_play.visible = false
      this.btn_continue.visible = true
      this.btn_more.visible = false
      this.btn_continue.on(Laya.Event.CLICK, this, () => { // 继续
        UIManager.getInstance().loadStageUI(stageID)
        this.owner.destroy()
      })
    }

    this.btn_new_blue.on(Laya.Event.CLICK, this, this.onClickNew)
    this.btn_new_white.on(Laya.Event.CLICK, this, this.onClickNew)
    this.btn_go.on(Laya.Event.CLICK, this, () => {
      UIManager.getInstance().loadStageUI(config.DEFAULT_STAGE_ID)
      this.owner.destroy()
    })

    if (user.get_done(config.DEFAULT_STAGE_ID) >= 1) {
      console.log("当前主线关卡完成")
      this.btn_new_blue.visible = true
      this.btn_new_white.visible = false
      this.btn_go.visible = false
    } else {
      const doStage = StorageManager.getInstance().getStage(config.DEFAULT_STAGE_ID)
      if (doStage) {
        console.log("当前主线关卡进行中 mistake=", doStage.get_mistake())
        if (doStage.get_mistake() >= config.MISTAKE_MAX) {
          this.btn_new_blue.visible = true
          this.btn_new_white.visible = false
          this.btn_go.visible = false
        } else {
          this.btn_new_blue.visible = false
          this.btn_new_white.visible = true
          this.btn_go.visible = true

          const label_time = this.btn_go.getChildByName("time").getChildByName("label_time") as Laya.Label
          const duration = doStage.get_duration()
          const timeStr = Utility_ConvertSecondToString(duration)
          label_time.text = `${timeStr} - ${doStage.get_difficulty()}`
        }
      } else {
        console.log("当前主线关卡未开始")
        this.btn_new_blue.visible = true
        this.btn_new_white.visible = false
        this.btn_go.visible = false
      }
    }
  }

  public onClickNew(_evt: Laya.Event): void {
    Laya.loader.load(respath.difficulty_ui_res).then((go) => {
      const prefab = go.create()
      const d = UIManager.getInstance().getUIRoot().addChild(prefab).getComponent(Difficulty)
      d.onInit((value: string) => {
        console.log("选择难度", value)
        StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, value, value)
        UIManager.getInstance().loadStageUI(config.DEFAULT_STAGE_ID)
        this.owner.destroy()
      })
    })
  }
}
