import { Animation, CCFloat, CCInteger, Component, Vec2, Vec3, _decorator, log } from 'cc'

import { Logger } from '../../script/engine/utils/Logger'
import MathUtils from '../../script/engine/utils/MathUtils'
import TimeHelper from '../../script/game/utils/TimeHelper'

import FishBase from './FishBase'

const { ccclass, property } = _decorator

@ccclass('FishMover')
export default class FishMover extends Component {
  // 鱼类型
  @property({ type: CCInteger })
  public fishType: number = 1

  // 鱼移动速度
  @property({ type: CCFloat })
  public speed: number = 3

  // 下个位置移动点
  private targetMoveIndex: number = 0
  // 鱼移动位置
  public movePList: Array<Vec2> = []
  // 贝萨尔曲线
  public bezierPList: Array<Vec2> = []
  public isMoving: boolean = false
  private minSpeed: number = 0.1
  private moveCount: number = 1
  private totalTimes: number = 60 * 2
  private _vec3Cahce: Vec3 = new Vec3()

  public startMove() {
    this.targetMoveIndex = 0
    this.isMoving = true
    // this.node.getComponent(Animation).play()//v3 当前帧 不能播放
    TimeHelper.exeNextFrame(this.node, () => this.node.getComponent(Animation).play())
  }

  update(dt) {
    // this.moveFish();
    this.checkMoveBezier()
  }

  // 检测是否到达目标点
  private checkMoveBezier() {
    if (this.isMoving && this.getComponent(FishBase).isDead === 2) {
      this.moveCount++
      if (this.moveCount >= this.totalTimes) this.moveCount = this.totalTimes
      this.moveBezier()
    }
  }

  // 贝塞尔曲线移动
  public moveBezier() {
    if (this.bezierPList.length > this.targetMoveIndex + 2) {
      const p0: Vec2 = this.bezierPList[this.targetMoveIndex]
      const p1: Vec2 = this.bezierPList[this.targetMoveIndex + 1]
      const p2: Vec2 = this.bezierPList[this.targetMoveIndex + 2]
      const t: number = this.moveCount / this.totalTimes
      const mx: number = (1 - t) ** 2 * p0.x + 2 * t * (1 - t) * p1.x + t ** 2 * p2.x
      const my: number = (1 - t) ** 2 * p0.y + 2 * t * (1 - t) * p1.y + t ** 2 * p2.y
      this.node.getPosition(this._vec3Cahce)
      const rad: number = MathUtils.p2pRad(
        new Vec2(this._vec3Cahce.x, this._vec3Cahce.y),
        new Vec2(mx, my),
      )
      const rot111: number = MathUtils.radiansToDegrees(rad)
      const rot: number = MathUtils.rotation2Fish(rot111)
      if (this.fishType === 7 || this.fishType === 27 || this.fishType === 29) {
        if (rot > 90 || rot < -90) {
          this.node.getScale(this._vec3Cahce)
          if (this._vec3Cahce.x > 0) {
            this._vec3Cahce.x = -1 * this._vec3Cahce.x
            this.node.setScale(this._vec3Cahce)
          }
        }
      } else if (
        this.fishType === 9 ||
        this.fishType === 10 ||
        this.fishType === 21 ||
        this.fishType === 28
      ) {
      } else {
        this.node.angle = -rot
      }
      // Logger.log("moveBezier====", rad, rot111, this.fishType, rot)
      const moveTimes: number = Math.round(this.speed / this.minSpeed)
      for (let i = 0; i < moveTimes; i++) {
        const speedX: number = this.minSpeed * Math.cos(rad)
        const speedY: number = this.minSpeed * Math.sin(rad)
        this.node.getPosition(this._vec3Cahce)
        this._vec3Cahce.x += speedX
        this._vec3Cahce.y += speedY
        this.node.setPosition(this._vec3Cahce)
        if (MathUtils.distance(this._vec3Cahce.x, this._vec3Cahce.y, mx, my) <= this.minSpeed) {
          this.node.setPosition(mx, my)
          break
        }
        if (
          MathUtils.distance(this._vec3Cahce.x, this._vec3Cahce.y, p2.x, p2.y) <=
          this.minSpeed * 2
        ) {
          this.node.setPosition(p2.x, p2.y)
          this.targetMoveIndex += 2
          this.moveCount = 0
          break
        }
      }
    } else {
      Logger.log('moveBezier====', 'end')
      this.isMoving = false
    }
  }

  onDisable() {
    this.isMoving = false
  }

  // 导出贝塞尔曲线配置
  public exportBezierConfig() {
    Logger.warn('exportBezierConfig=')
    const tempConfig: Array<Array<number>> = []
    for (let i = 0; i < this.bezierPList.length; i++) {
      tempConfig[i] = []
      tempConfig[i].push(Math.floor(this.bezierPList[i].x))
      tempConfig[i].push(Math.floor(this.bezierPList[i].y))
    }
    Logger.warn('fishtype', this.fishType)
    Logger.warn('speed', this.speed)
    Logger.warn('scale', this.node.scale)
    Logger.warn(JSON.stringify(tempConfig))
  }
}
