
var IsOk
var MoveTrigger
var craftPlaySolveType
onmessage = function(e) {
  IsOk = e.data.isOk
  MoveTrigger = JSON.parse(e.data.moveTrigger)
  craftPlaySolveType = e.data.craftPlaySolveType
  playMoveTrigger1(e.data.ActID, e.data.State)
}

function playMoveTrigger1(ActID, moveStatus) {
  // debugger
  if (!IsOk) return
  if (!MoveTrigger) return
  if (MoveTrigger.moveStatusPosition !== 0) return
  if (ActID === '') return
  if (MoveTrigger.moveStatus === moveStatus) {
    return
  } else {
    MoveTrigger.moveStatus = moveStatus
  }
  const m = MoveTrigger
  if (moveStatus === 0) {
    clearInterval(m.myVarSetInterval)
    m.doAnimation = false
    m.pausedFrame = m.currentFrame
    self.postMessage({
      ActID: ActID,
      currentFrameData: m.animOperations.OPERATION.frames[m.pausedFrame + 1].objStates,
      currentFrame: m.pausedFrame + 1,
      myVarSetInterval: m.myVarSetInterval,
      pausedFrame: m.pausedFrame + 1,
      frameType: ''

    })
  } else if (moveStatus === 1) {
    // 播放时，只能暂停和复原。不允许继续播放
    if (m.doAnimation === true) return
    m.doAnimation = true
    m.currentFrame = m.pausedFrame
  } else if (moveStatus === 2) {
    clearInterval(m.myVarSetInterval)
    m.moveStatusPosition = 0
    m.moveStatus = 0
    m.currentFrame = 0
    m.pausedFrame = 0
    self.postMessage({
      ActID: ActID,
      currentFrameData: m.animOperations.OPERATION.frames[0].objStates,
      currentFrame: 0,
      myVarSetInterval: m.myVarSetInterval,
      pausedFrame: 0,
      frameType: ''
    })
    // Worker 线程结束
    self.close()
  }
  m.myVarSetInterval = setInterval(() => {
    if (m.currentFrame < m.animOperations.OPERATION.frames.length && m.doAnimation === true) {
      const frameIndex = m.currentFrame
      const frameDataPlay = m.animOperations.OPERATION.frames[frameIndex].objStates
      const frameDataCraft = m.animOperations.OPERATION.frames[frameIndex].crafts
      const frameDataVisible = m.animOperations.OPERATION.frames[frameIndex].visibles
      const frameDataRotation = m.animOperations.OPERATION.frames[frameIndex].rotations
      const frameDataAttach = m.animOperations.OPERATION.frames[frameIndex].attachs
      for (const moveData of frameDataPlay) {
        self.postMessage({
          ActID: ActID,
          currentFrame: m.currentFrame,
          currentFrameData: moveData,
          myVarSetInterval: m.myVarSetInterval,
          pausedFrame: m.currentFrame,
          frameType: craftPlaySolveType.FrameData
        })
      }
      for (const craftData of frameDataCraft) {
        self.postMessage({
          ActID: ActID,
          currentFrame: m.currentFrame,
          currentFrameData: craftData,
          myVarSetInterval: m.myVarSetInterval,
          pausedFrame: m.currentFrame,
          frameType: craftPlaySolveType.craft
        })
      }
      for (const visibleData of frameDataVisible) {
        self.postMessage({
          ActID: ActID,
          currentFrame: m.currentFrame,
          currentFrameData: visibleData,
          frameDataPlay: m.myVarSetInterval,
          pausedFrame: m.currentFrame,
          frameType: craftPlaySolveType.visible
        })
      }
      for (const rotationData of frameDataRotation) {
        self.postMessage({
          ActID: ActID,
          currentFrame: m.currentFrame,
          currentFrameData: rotationData,
          myVarSetInterval: m.myVarSetInterval,
          pausedFrame: m.currentFrame,
          frameType: craftPlaySolveType.rotation
        })
      }
      for (const attachData of frameDataAttach) {
        self.postMessage({
          ActID: ActID,
          currentFrame: m.currentFrame,
          currentFrameData: attachData,
          myVarSetInterval: m.myVarSetInterval,
          pausedFrame: m.currentFrame,
          frameType: craftPlaySolveType.attach
        })
      }
      m.currentFrame++
    } else {
      // 结束仿真
      if (moveStatus === 2) {
        m.moveStatusPosition = 0
        m.moveStatus = 0
        m.currentFrame = 0
        m.pausedFrame = 0
        self.postMessage({
          ActID: ActID,
          currentFrame: 0,
          currentFrameData: m.animOperations.OPERATION.frames[0].objStates,
          myVarSetInterval: m.myVarSetInterval,
          pausedFrame: 0,
          frameType: ''
        })
        // Worker 线程结束
        self.close()
      }

      clearInterval(m.myVarSetInterval)

      // if we finished the entire simulation, we need to reset the frames
      if (m.doAnimation === true) {
        m.currentFrame = 0
        m.pausedFrame = 0
        self.postMessage({
          ActID: ActID,
          currentFrame: 0,
          currentFrameData: m.animOperations.OPERATION.frames[0].objStates,
          myVarSetInterval: m.myVarSetInterval,
          pausedFrame: 0,
          frameType: ''
        })
        // Worker 线程结束
        self.close()
      }
      m.doAnimation = false
    }
  }, m.simPaceTime)
}
