import * as RE from 'rogue-engine';
import RaycastReceiver from './RaycastReceiver.re';
import { SimpleTween } from './Library/SimpleTweens'
import Easing from './Library/Easings';
import * as THREE from 'three'
import CombatLogic from './CombatLogic.re'

export default class TileRaycastReceiver extends RaycastReceiver {
  @RE.Prop("Vector2") tileSize = new THREE.Vector2(1.1, 1.1)

  @RE.Prop("Audio") dropInSound
  @RE.Prop("Audio") hoverSound
  @RE.Prop("Audio") clearSound

  onMouseOver(intersect) {
    if(this.object3d.isAnimating) {
      return
    }

    this.currentObj = intersect.object
    this.currentObj.material = this.currentObj.highlightMaterial
    
    RE.Runtime.rogueDOMContainer.style.cursor = "pointer"

    if(RE.Input.mouse.getButtonPressed(0) || RE.Input.touch.touches.length == 1) {
      let game = RE.getComponent(CombatLogic)
      let [isAdded, selectedTiles] = game.addTile(this)
      if(isAdded) {
        if(this.hoverSound) {
          this.hoverSound.play()
        }
      } else {
        if(selectedTiles[selectedTiles.length - 1] != this) {
          this.currentObj.material = this.currentObj.originalMaterial
        }
      }
    }
  }

  onMouseOut(){
    if(this.object3d.isAnimating || !this.currentObj) {
      return
    }

    if(RE.Input.mouse.getButtonPressed(0)) {
      //dragged out of this tile, should track state so a second "leave" will unselect
    }

    this.currentObj.material = this.currentObj.originalMaterial
    RE.Runtime.rogueDOMContainer.style.cursor = "default"
  }

  getOriginalMaterial() {
    return this.object3d.originalMaterial
  }

  getPosition() {
    return this.object3d.position
  }

  awake() {

  }

  start() {
    if(this.dropInSound) {
      this.dropInSound.setVolume(0.7)
      this.dropInSound.setLoop(false)
    }

    if(this.hoverSound) {
      this.hoverSound.setVolume(0.7)
      this.hoverSound.setLoop(false)
    }

    if(this.clearSound) {
      this.clearSound.setVolume(1)
      this.clearSound.setLoop(false)
    }
  }

  update() {

  }

  initialize(gridPosition, boardDimensions, originalMaterial, hoverMaterial, tweenManager, delay, startingZ, easingFunction) {
    let offset = new THREE.Vector2(
      (boardDimensions.x * this.tileSize.x - 1) / 2,
      (boardDimensions.y * this.tileSize.y - 1) / 2,
    )
    this.object3d.position.copy(new THREE.Vector3((this.tileSize.x * gridPosition.x) - offset.x, (this.tileSize.y * gridPosition.y) - offset.y, startingZ))
    
    this.object3d.originalMaterial = originalMaterial
    this.object3d.highlightMaterial = hoverMaterial
    this.object3d.material = originalMaterial
    this.object3d.gridPosition = gridPosition
    this.object3d.isAnimating = true
    
    setTimeout(() => {
      if(this.dropInSound) {
      this.dropInSound.play()
      }
    }, (delay/3) * Math.random())
    tweenManager.add(new SimpleTween(this.object3d.position, {z: 0}, delay, easingFunction, () => { 
      this.object3d.isAnimating = false
    }))
  }

  remove(tweenManager) {
    this.object3d.isAnimating = true
    setTimeout(() => {
      if(this.clearSound) {
        this.clearSound.play()
      }
    }, 150 * Math.random())
    
    tweenManager.add(new SimpleTween(this.object3d.position, {z: -14}, Math.floor(1500 * Math.random()) + 300, Easing.Quadratic.EaseIn, () => { 
      this.object3d.parent.remove(this.object3d)
    }))
  }
}

RE.registerComponent(TileRaycastReceiver);
