import { config } from "../../constants/config"
import { respath } from "../../constants/respath"
import { ResourceManager } from "../../models/ResourceManager"
import { StorageManager } from "../../models/StorageManager"
import { UIManager } from "../../models/UIManager"

const { regClass, property } = Laya

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

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

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

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

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

  public onInit(info: any): void {
    const user = StorageManager.getInstance().getUser()
    const starCount = user.get_doneCountByDate(info.year, info.month)
    ResourceManager.getInstance().loadTexture(info.icon_res, this.icon)
    this.label_count.text = `${starCount}/${info.dayCount}`
    this.label_month.text = config.MONTH_ABBRS[info.month - 1]
    if (user.get_trophyRecord(info.year, info.month)) {
      ResourceManager.getInstance().loadTexture(respath.trophy_bg_light_gold, this.bg_light)
      this.icon.gray = false
      this.label_month.color = "#2d3138"
    } else {
      ResourceManager.getInstance().loadTexture(respath.trophy_bg_light_grey, this.bg_light)
      this.icon.gray = true
      this.label_month.color = "#a7abb3"
    }

    this.icon.on(Laya.Event.CLICK, this, () => {
      const find = user.get_trophyRecord(info.year, info.month)
      if (find) {
        UIManager.getInstance().loadTrophyShowUI(find)
      } else {
        UIManager.getInstance().loadDCWtihMonth(info.year, info.month)
      }
    })
  }
}
