package wow.doge.mygame.game.entities

import akka.actor.typed.Behavior
import akka.actor.typed.LogOptions
import akka.actor.typed.scaladsl.ActorContext
import akka.actor.typed.scaladsl.Behaviors
import com.jme3.math.FastMath
import com.jme3.math.Quaternion
import com.jme3.math.Vector3f
import com.jme3.scene.Node
import com.typesafe.scalalogging.Logger
import org.slf4j.event.Level

object PlayerCameraActor {
  sealed trait Command
  case object RotateLeft extends Command
  case object RotateRight extends Command
  case object RotateUp extends Command
  case object RotateDown extends Command
  case object Tick extends Command

  class Props(
      val cameraPivotNode: Node,
      val enqueueR: Function1[() => Unit, Unit],
      val getPlayerLocation: Function0[Vector3f]
  ) {
    def behavior =
      Behaviors.logMessages(
        LogOptions()
          .withLevel(Level.TRACE)
          .withLogger(
            Logger[PlayerCameraActor].underlying
          ),
        Behaviors.setup[Command] { ctx =>
          new PlayerCameraActor(ctx, this).receive()
        }
      )
  }

  case class State()
  object State {
    val empty = State()
  }
}
class PlayerCameraActor(
    ctx: ActorContext[PlayerCameraActor.Command],
    props: PlayerCameraActor.Props
) {
  import PlayerCameraActor._
  def receive(
      rotationBuf: Quaternion = new Quaternion(),
      state: State = State.empty
  ): Behavior[Command] =
    Behaviors.receiveMessage {
      case RotateLeft =>
        val rot = rotationBuf
          .fromAngleAxis(1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
        props.cameraPivotNode.rotate(rot)
        Behaviors.same
      case RotateRight =>
        val rot = rotationBuf
          .fromAngleAxis(-1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y)
        props.cameraPivotNode.rotate(rot)
        Behaviors.same
      case RotateUp =>
        val rot = rotationBuf
          .fromAngleAxis(-1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_X)
        props.cameraPivotNode.rotate(rot)
        Behaviors.same
      case RotateDown =>
        val rot = rotationBuf
          .fromAngleAxis(1 * FastMath.DEG_TO_RAD, Vector3f.UNIT_X)
        props.cameraPivotNode.rotate(rot)
        Behaviors.same
      case Tick =>
        val location = props.getPlayerLocation()
        props.cameraPivotNode.setLocalTranslation(location)

        Behaviors.same
    }
}
