import type { Stage } from "./Stage"

const { regClass } = Laya

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

  private _stage: Stage

  private offset: Laya.Box
  private bg_sprite: Laya.Sprite
  private bg_filter: Laya.GlowFilter
  private show_sprite: Laya.Sprite
  private mask_sprite: Laya.Sprite
  private label_show: Laya.Label
  private label_left: Laya.Label

  private show: number
  private left: number
  private available: boolean = true

  public onInit(handler: any): void {
    this._stage = handler
    this.owner.on(Laya.Event.CLICK, this, () => {
      if (this.available === false)
        return
      this._stage.onApplyCandy(this.show, false)
    })

    this.owner.on(Laya.Event.MOUSE_DOWN, this, () => {
      if (this.available === false)
        return
      Laya.Tween.to(this.bg_sprite, { alpha: 0 }, 200)
      Laya.Tween.to(this.mask_sprite, { alpha: 0.1 }, 200)
      Laya.Tween.to(this.offset, { scaleX: 0.96, scaleY: 0.96 }, 200)
    })
    this.owner.on(Laya.Event.MOUSE_UP, this, () => {
      if (this.available === false)
        return
      Laya.Tween.to(this.bg_sprite, { alpha: 0.12 }, 200)
      Laya.Tween.to(this.mask_sprite, { alpha: 0 }, 200)
      Laya.Tween.to(this.offset, { scaleX: 1, scaleY: 1 }, 200)
    })
    this.owner.on(Laya.Event.MOUSE_OVER, this, () => {
      if (this.available === false)
        return
      Laya.Render.canvas.style.cursor = "pointer"
      Laya.Tween.to(this.bg_sprite, { alpha: 0.16 }, 200)
      Laya.Tween.to(this.bg_filter, { offY: 4, blur: 8 }, 200)
      Laya.Tween.to(this.mask_sprite, { alpha: 0.05 }, 200)
    })
    this.owner.on(Laya.Event.MOUSE_OUT, this, () => {
      if (this.available === false)
        return
      Laya.Render.canvas.style.cursor = ""
      Laya.Tween.to(this.bg_sprite, { alpha: 0.12 }, 200)
      Laya.Tween.to(this.bg_filter, { offY: 2, blur: 4 }, 200)
      Laya.Tween.to(this.mask_sprite, { alpha: 0 }, 200)
    })
    this.offset = this.owner.getChildByName("offset") as Laya.Box
    this.bg_sprite = this.owner.getChildByName("offset").getChildByName("shadow") as Laya.Sprite
    this.bg_filter = this.bg_sprite.filters[0] as Laya.GlowFilter
    this.show_sprite = this.owner.getChildByName("offset").getChildByName("Sprite") as Laya.Sprite
    this.mask_sprite = this.owner.getChildByName("offset").getChildByName("mask") as Laya.Sprite
    this.label_show = this.owner.getChildByName("offset").getChildByName("label") as Laya.Label
    this.label_left = this.owner.getChildByName("offset").getChildByName("label_left") as Laya.Label
  }

  public setAvailableStatus(available: boolean) {
    this.available = available
    if (available) {
      Laya.Tween.to(this.bg_sprite, { alpha: 0.12 }, 200)
      Laya.Tween.to(this.label_show, { alpha: 1 }, 200)
      Laya.Tween.to(this.label_left, { alpha: 1 }, 200)
    } else {
      Laya.Tween.to(this.bg_sprite, { alpha: 0 }, 200)
      Laya.Tween.to(this.label_show, { alpha: 0.4 }, 200)
      Laya.Tween.to(this.label_left, { alpha: 0.4 }, 200)
    }
  }

  public setNoteStatus(note_open: boolean) {
    if (note_open) {
      this.label_show.color = "#2d3138"
    } else {
      this.label_show.color = "#2559c0"
    }
  }

  public setCandy(show: number, left: number): void {
    this.offset.scaleX = 1
    this.offset.scaleY = 1
    this.bg_sprite.alpha = 0.12
    this.bg_filter.offY = 2
    this.bg_filter.blur = 4
    this.mask_sprite.alpha = 0
    this.label_show.alpha = 1
    this.label_left.alpha = 1

    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 = ""
    }
  }
}
