const { regClass, property } = Laya

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

  private show_sprite: Laya.Sprite
  private label_show: Laya.Label
  private label_left: Laya.Label

  private show: number
  private left: number

  public onInit(label_obj: Laya.Box, handler: any, func: any): void {
    this.owner.on(Laya.Event.CLICK, handler, func)
    this.show_sprite = this.owner.getChildByName("Sprite") as Laya.Sprite
    this.label_show = label_obj.getChildByName("label") as Laya.Label
    this.label_left = label_obj.getChildByName("label_left") as Laya.Label
  }

  public setNoteStatus(note_open: boolean) {
    if (note_open) {
      this.show_sprite.visible = false
      this.label_show.color = "#7b818c"
    } else {
      this.show_sprite.visible = true
      this.label_show.color = "#2559c0"
    }
  }

  public setCandy(show: number, left: number): void {
    this.show = show
    this.left = left
    this.updateCandy()
  }

  public get_show(): number {
    return this.show
  }

  updateCandy(): void {
    if (this.show > 0) {
      this.label_show.text = this.show.toString()
    } else {
      this.label_show.text = ""
    }

    if (this.left > 0) {
      this.label_left.text = this.left.toString()
      this.owner.visible = true
    } else {
      this.label_left.text = ""
      this.owner.visible = false
      this.label_show.text = ""
    }
  }
}
