import type { DOBlock } from "../models/DOBlock"
import { config } from "../constants/config"
import { G_ShowScoreTips } from "../views/common/GUITips"

const { regClass, property } = Laya

@regClass()
export class Block extends Laya.Script {
  declare owner: Laya.Sprite

  @property(Number)
  public XIndex: number = 0

  @property(Number)
  public YIndex: number = 0

  @property(Number)
  public XGroup: number = 0

  @property(Number)
  public YGroup: number = 0

  private tips_bgs: Array<Laya.Sprite> = []
  private label_show: Laya.Label
  private tips_labels: Array<Laya.Label> = []

  private selected: boolean = false

  private data: DOBlock

  public onInit(XIndex: number, YIndex: number, XGroup: number, YGroup: number, label_obj: Laya.Box, handler: any, func: any): void {
    this.XIndex = XIndex
    this.YIndex = YIndex
    this.XGroup = XGroup
    this.YGroup = YGroup

    let VBox = this.owner.getChildByName("VBox")
    for (var i = 0; i < VBox.numChildren; i++) {
      var hbox = VBox.getChildAt(i)
      for (var j = 0; j < hbox.numChildren; j++) {
        const sp = hbox.getChildAt(j) as Laya.Sprite
        this.tips_bgs.push(sp)
      }
    }
    this.label_show = label_obj.getChildByName("label_show") as Laya.Label
    // this.label_show.text = XGroup + "" + YGroup
    VBox = label_obj.getChildByName("VBox")
    for (var i = 0; i < VBox.numChildren; i++) {
      var hbox = VBox.getChildAt(i)
      for (var j = 0; j < hbox.numChildren; j++) {
        const Label = hbox.getChildAt(j) as Laya.Label
        Label.text = ""
        this.tips_labels.push(Label)
      }
    }
    this.owner.on(Laya.Event.CLICK, handler, func)
  }

  public setSelected(selected: boolean): void {
    this.selected = selected
    this.updateBlock()
  }

  public setBlockColor(color: string, alpha: number = 1, animated: boolean = false, completedColor: string = "", completedAlpha: number = 1): void {
    let blockSize = 116
    if (config.H_SCREEN)
      blockSize = 96
    this.owner.graphics.clear()
    this.owner.graphics.drawRect(0, 0, blockSize, blockSize, color)
    if (animated) {
      this.owner.alpha = 0
      Laya.Tween.to(this.owner, { alpha }, 100, Laya.Ease.linearIn)
      Laya.Tween.to(this.owner, { alpha: 0 }, 150, Laya.Ease.linearIn, null, 100)
      Laya.Tween.to(this.owner, { alpha }, 100, Laya.Ease.linearIn, Laya.Handler.create(this, () => {
        this.owner.graphics.clear()
        this.owner.graphics.drawRect(0, 0, blockSize, blockSize, completedColor)
        this.owner.alpha = completedAlpha
      }), 250)
    } else {
      this.owner.alpha = alpha
    }
  }

  public setFadeAnimation(): void {
    Laya.timer.once(200, this, () => {
      this.label_show.color = "#ff0000"
      Laya.timer.once(100, this, () => {
        this.label_show.color = "#2d3138"
        Laya.timer.once(200, this, () => {
          this.label_show.color = "#ff0000"
          Laya.timer.once(100, this, () => {
            this.updateBlock()
          })
        })
      })
    })
  }

  public setTipsBgColor(visible: boolean, noteNumber: number = 0, color: string = "", alpha: number = 1): void {
    let blockSize = 38
    if (config.H_SCREEN)
      blockSize = 30
    if (visible) {
      var obj = this.tips_bgs[noteNumber - 1]
      obj.visible = true
      obj.graphics.clear()
      obj.graphics.drawRect(0, 0, blockSize, blockSize, color)
      obj.alpha = alpha
    } else {
      for (let i = 0; i < this.tips_bgs.length; i++) {
        var obj = this.tips_bgs[i]
        obj.visible = false
      }
    }
  }

  public setBlock(data: DOBlock): void {
    this.data = data
    this.updateBlock()
  }

  public getData(): DOBlock {
    return this.data
  }

  public setShowNumber(showNumber: number, score: number): boolean {
    this.data.set_show(showNumber)
    if (this.data.get_checked()) {
      this.data.set_score(score)
      if (score > 0)
        G_ShowScoreTips(score.toString(), this.label_show.parent)
    } else {
      if (this.data.get_score() !== 0)
        this.data.set_score(0)
    }
    this.data.clean_notes()
    this.updateBlock()
    return this.data.get_checked()
  }

  public addNoteNumber(noteNumber: number): void {
    this.data.set_show(0)
    const note_numbers = this.data.get_notes()
    const find = note_numbers.indexOf(noteNumber)

    if (find >= 0) {
      this.data.remove_note(noteNumber)
    } else {
      this.data.add_note(noteNumber)
    }
    this.updateBlock()
  }

  public setNotes(notes: Array<number>): void {
    this.data.set_show(0)
    this.data.clean_notes()
    for (let i = 0; i < notes.length; i++) {
      this.data.add_note(notes[i])
    }
    this.updateBlock()
  }

  updateBlock(): void {
    // 设置文字颜色
    if (this.selected) {
      this.label_show.color = "#ffffff"
      for (var i = 0; i < this.tips_labels.length; i++) {
        this.tips_labels[i].color = "#ffffff"
      }
    } else {
      if (this.data.get_show() === this.data.get_correct()) {
        if (this.data.get_preset()) {
          this.label_show.color = "#2d3138"
        } else {
          this.label_show.color = "#2559c0"
        }
      } else {
        this.label_show.color = "#cb3256"
      }
      for (var i = 0; i < this.tips_labels.length; i++) {
        this.tips_labels[i].color = "#2d3138"
      }
    }

    for (var i = 0; i < this.tips_labels.length; i++) {
      this.tips_labels[i].text = ""
    }
    if (this.data.get_show() > 0) {
      this.label_show.text = this.data.get_show().toString()
    } else {
      this.label_show.text = ""
      const note_numbers = this.data.get_notes()
      for (var i = 0; i < note_numbers.length; i++) {
        const value = note_numbers[i]
        const label = this.tips_labels[value - 1]
        label.text = value.toString()
      }
    }
  }
}
