import type { TrophyRecord } from "../../types/global"
import { config } from "../../constants/config"
import { MONTH_LIST } from "../../models/DCManager"
import { ResourceManager } from "../../models/ResourceManager"
import { Utility_EaseOut_2 } from "../../utils/utility"
import { UISequenceAnimation } from "../common/UISequenceAnimation"

const { regClass, property } = Laya

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

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

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

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

  private mask: Laya.Image
  private center: Laya.Box

  onAwake(): void {
    this.mask = this.owner.getChildByName("Image") as Laya.Image
    this.mask.alpha = 0
    Laya.Tween.to(this.mask, { alpha: 0.8 }, 600)

    this.center = this.owner.getChildByName("center") as Laya.Box

    const light = this.center.getChildByName("light") as Laya.Image
    light.scaleX = 0
    light.scaleY = 0
    Laya.Tween.to(light, { scaleX: 1, scaleY: 1 }, 200, Utility_EaseOut_2, null, 200)

    const trophy = this.center.getChildByName("trophy") as Laya.Image
    trophy.scaleX = 0
    trophy.scaleY = 0
    Laya.Tween.to(trophy, { scaleX: 1, scaleY: 1 }, 200, Utility_EaseOut_2, null, 200)

    const blossom = trophy.getChildByName("blossom").getComponent(UISequenceAnimation)
    Laya.timer.once(400, this, () => {
      blossom.onSetRunning(true)
    })

    const label_titlle = this.center.getChildByName("label_titlle") as Laya.Label
    label_titlle.alpha = 0
    Laya.Tween.to(label_titlle, { alpha: 1 }, 600, Laya.Ease.linearNone, null, 600)

    const btn_close = this.center.getChildByName("btn_close") as Laya.Box
    btn_close.alpha = 0
    Laya.Tween.to(btn_close, { alpha: 1 }, 600, Laya.Ease.linearNone, null, 800)
  }

  public onSetShow(record: TrophyRecord): void {
    for (let i = 0; i < MONTH_LIST.length; i++) {
      const obj = MONTH_LIST[i]
      if (obj.year === record.year && obj.month === record.month) {
        ResourceManager.getInstance().loadTexture(obj.icon_res, this.icon)
        break
      }
    }

    this.label_date.text = `${config.MONTH_ABBRS[record.month - 1]} ${record.day}, ${record.year}`

    this.btn_close.on(Laya.Event.CLICK, this, () => {
      this.owner.destroy()
    })
  }
}
