import { Animation, Component, Label, Node, Tween, Vec2, Vec3, _decorator, find, tween } from 'cc'

import CannonManager from '../../script/game/manager/CannonManager'
import FishManager from '../../script/game/manager/FishManager'

import FishSetting from './FishSetting'
import WsManager from '../../script/game/manager/WsManager'

const { ccclass, property } = _decorator

@ccclass('FishUI')
export default class FishUI extends Component {
  public static instance: FishUI = null
  @property({ type: Label })
  private dzScore: Label | null = null

  @property({ type: Label })
  private jfScore: Label | null = null

  @property({ type: Animation })
  private clickEffect: Animation | null = null

  @property({ type: Node })
  private waveEffect: Node | null = null

  // 豆子
  public dz_score: number = 0
  // 积分
  public jf_score: number = 0
  private _vec3Cache: Vec3

  onLoad() {
    FishUI.instance = this
    this._vec3Cache = new Vec3()
    this.clickEffect.node.active = false
    this.waveEffect.active = false
    this.dzScore = this.node
      .getChildByName('userInfo')
      .getChildByName('info')
      .getChildByName('dz_fillet')
      .getChildByName('dz_text')
      .getComponent(Label) as Label
    this.jfScore = this.node
      .getChildByName('userInfo')
      .getChildByName('info')
      .getChildByName('jf_fillet')
      .getChildByName('jf_text')
      .getComponent(Label) as Label
  }

  start() {
    // this.refreshScore()
  }

  public playClickEffect(p: Vec2) {
    this._vec3Cache.x = p.x
    this._vec3Cache.y = p.y
    this._vec3Cache.z = 0
    this.clickEffect.node.setPosition(this._vec3Cache)
    this.clickEffect.node.active = true
    this.clickEffect.play()
  }

  public playWaveEffect() {
    this.waveEffect.active = true
    this.waveEffect.setPosition(1292.703, 0)

    FishManager.instance.playFishMap()
    tween(this.waveEffect)
      .to(2, { position: new Vec3(-1319.969, 0, 0) })
      .call(() => {
        this.waveEffect.active = false
        FishManager.instance.startFishMap()
      })
      .start()
  }

  private onClickPre() {
    if (CannonManager.instance.cannonType > 1) {
      CannonManager.instance.cannonType--
      CannonManager.instance.refreshCannon()
    }
  }

  private onClickNext() {
    if (CannonManager.instance.cannonType < 7) {
      CannonManager.instance.cannonType++
      CannonManager.instance.refreshCannon()
    }
  }

  public refreshScore(res: any) {
    this.dz_score = res.pulse
    this.jf_score = res.integral
    this.dzScore.string = `${this.dz_score}`
    this.jfScore.string = `${this.jf_score}`
  }

  private onClickSetting() {
    FishSetting.show()
  }

  onDestroy() {
    FishUI.instance = null
    this.unscheduleAllCallbacks()
    Tween.stopAllByTarget(this.node)
    // this.node.stopAllActions();
  }
}
