import { config } from "../constants/config";
import { CommonData } from "./common/CommonData";

const { regClass, property } = Laya;

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


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


    //组件被激活后执行，此时所有节点和组件均已创建完毕，此方法只执行一次
    onAwake(): void {
        var offset = this.owner.getChildByName("offset") as Laya.Box
        var bg = offset.getChildByName("bg") as Laya.Sprite
        bg.graphics.clear()
        bg.graphics.drawRoundRect(0, 0, offset.width, offset.height, 20, 20, 0, 0, "#f5f7fb")

        Laya.Tween.to(offset, {y: 705}, 500)

        var mask = this.owner.getChildByName("mask")
        mask.on(Laya.Event.CLICK, this, ()=>{
            Laya.Tween.to(offset, {y: 2400}, 500, null, Laya.Handler.create(this, () => {
                this.owner.destroy()
            }))
        })
    }

    public onInit(callback: any): void {
        for (var i=0; i<config.DIFFICULTY_LIST.length; i++) {
            var obj = this.obj_items.getChildAt(i)
            var Label = obj.getChildByName("Label") as Laya.Label
            Label.text = config.DIFFICULTY_LIST[i]
            var data = obj.getComponent(CommonData)
            data.strValue = config.DIFFICULTY_LIST[i]
            obj.on(Laya.Event.CLICK, this, (evt: Laya.Event)=>{
                var data = evt.target.getComponent(CommonData)
                if (callback) {
                    callback(data.strValue)
                }
                this.owner.destroy()
            })
        }
    }
}