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 {
    const offset = this.owner.getChildByName("offset") as Laya.Box
    const 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)

    const 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 (let i = 0; i < config.DIFFICULTY_LIST.length; i++) {
      const obj = this.obj_items.getChildAt(i)
      const Label = obj.getChildByName("Label") as Laya.Label
      Label.text = config.DIFFICULTY_LIST[i]
      const data = obj.getComponent(CommonData)
      data.strValue = config.DIFFICULTY_LIST[i]
      obj.on(Laya.Event.CLICK, this, (evt: Laya.Event) => {
        const data = evt.target.getComponent(CommonData)
        if (callback) {
          callback(data.strValue)
        }
        this.owner.destroy()
      })
    }
  }
}
