



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 {

        var trophyCountOfCell = 3
        if (config.H_SCREEN)
            trophyCountOfCell = 5

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

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

        var years: Array<number> = new Array()
        var monthMap: Map<number, Array<any>> = new Map() 
        for (var i=0; i<infos.length; i++) {
            obj = infos[i]
            if (years.indexOf(obj.year) < 0) {
                years.push(obj.year)
            }
            if (monthMap.has(obj.year)== false) {
                monthMap.set(obj.year, new Array())
            }
            var list = monthMap.get(obj.year)
            list.push(obj)
        }
        console.log(">>>>>>>>>>>>years=", years)
        Laya.loader.load(respath.trophy_year_ui_res()).then((go)=>{
            for (var i=1; i<years.length; i++) {
                var obj_year = this.obj_items.getChildAt(i)
                if (!obj_year) {
                    var prefab = go.create()
                    this.obj_items.addChild(prefab)
                }
            }
            
            Laya.loader.load(respath.trophy_cell_ui_res()).then((go)=>{
                var itemsHeight = 0
                for (var i=0; i<years.length; i++) {
                    var list = monthMap.get(years[i])
                    var cellCount = Math.ceil(list.length/trophyCountOfCell)
                    var yearHeight = 120 + cellCount * 444
                    itemsHeight += yearHeight

                    var obj_year = this.obj_items.getChildAt(i) as Laya.Box
                    obj_year.height = yearHeight
                    var label = obj_year.getChildByName("year").getChildByName("Label") as Laya.Label
                    label.text = years[i].toString()
                    
                    var index = -1
                    for (var j=1; j<cellCount+1; j++) {
                        var obj_cell = obj_year.getChildAt(j)
                        if (!obj_cell) {
                            var prefab = go.create()
                            obj_cell = obj_year.addChild(prefab)
                        }
                        var hbox = obj_cell.getChildByName("HBox")
                        for (var k=0; k<hbox.numChildren; k++) {
                            var 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)
    }
}