import { config } from "../../constants/config"
import { respath } from "../../constants/respath"
import { MONTH_LIST } from "../../models/DCManager"
import { EVENT_TYPES, EventManager } from "../../models/EventManager"
import { UIManager } from "../../models/UIManager"
import { TrophyUnit } from "./TrophyUnit"

const { regClass, property } = Laya

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

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

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

  // 第一次执行update之前执行，只会执行一次
  onStart(): void {
    let trophyCountOfCell = 3
    if (config.H_SCREEN)
      trophyCountOfCell = 5

    this.btn_back.on(Laya.Event.CLICK, this, () => {
      UIManager.getInstance().closeTrophyUI()
    })

    const infos: Array<any> = []
    const now = new Date()
    const nowYear = now.getFullYear()
    const nowMonth = now.getMonth() + 1
    for (let i = 0; i < MONTH_LIST.length; i++) {
      const obj = MONTH_LIST[i]
      infos.push(obj)
      if (obj.year === nowYear && obj.month === nowMonth) {
        break
      }
    }
    infos.reverse()

    const years: Array<number> = []
    const monthMap: Map<number, Array<any>> = new Map()
    for (let i = 0; i < infos.length; i++) {
      const obj = infos[i]
      const find = years.indexOf(obj.year)
      if (find < 0) {
        years.push(obj.year)
      }
      if (monthMap.has(obj.year) === false) {
        monthMap.set(obj.year, [])
      }
      const list = monthMap.get(obj.year)
      list.push(obj)
    }
    console.log(">>>>>>>>>>>>years=", years)
    Laya.loader.load(respath.trophy_year_ui_res()).then((go) => {
      for (let i = 1; i < years.length; i++) {
        const obj_year = this.obj_items.getChildAt(i)
        if (!obj_year) {
          const prefab = go.create()
          this.obj_items.addChild(prefab)
        }
      }

      Laya.loader.load(respath.trophy_cell_ui_res()).then((go) => {
        let itemsHeight = 0
        for (let i = 0; i < years.length; i++) {
          const list = monthMap.get(years[i])
          const cellCount = Math.ceil(list.length / trophyCountOfCell)
          const yearHeight = 120 + cellCount * 444
          itemsHeight += yearHeight

          const obj_year = this.obj_items.getChildAt(i) as Laya.Box
          obj_year.height = yearHeight
          const label = obj_year.getChildByName("year").getChildByName("Label") as Laya.Label
          label.text = years[i].toString()

          let index = -1
          for (let j = 1; j < cellCount + 1; j++) {
            let obj_cell = obj_year.getChildAt(j)
            if (!obj_cell) {
              const prefab = go.create()
              obj_cell = obj_year.addChild(prefab)
            }
            const hbox = obj_cell.getChildByName("HBox")
            for (let k = 0; k < hbox.numChildren; k++) {
              const unit = hbox.getChildAt(k).getComponent(TrophyUnit)
              index++
              if (index < list.length) {
                unit.owner.visible = true
                unit.onInit(list[index])
              } else {
                unit.owner.visible = false
              }
            }
          }
        }
        this.obj_items.height = itemsHeight
      })
    })

    EventManager.getInstance().DispatchEvent(EVENT_TYPES.NOT_SET_TROPHY_BUTTON, false)
  }

  onDestroy(): void {
    EventManager.getInstance().DispatchEvent(EVENT_TYPES.NOT_SET_TROPHY_BUTTON, true)
  }
}
