package wow.doge.mygame.game

import com.jme3.app.SimpleApplication

import com.jme3.app.state.AppState
import akka.actor.typed.ActorRef
import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.Behaviors

object Greeter {
  final case class Greet(whom: String, replyTo: ActorRef[Greeted])
  final case class Greeted(whom: String, from: ActorRef[Greet])

  def apply(): Behavior[Greet] =
    Behaviors.receive { (context, message) =>
      //   context.log.info("Hello {}!", message.whom)
      //#greeter-send-messages
      message.replyTo ! Greeted(message.whom, context.self)
      //#greeter-send-messages
      Behaviors.same
    }
}

class GameApp(
    // actorSystem: ActorSystem[SpawnProtocol.Command],
    appStates: AppState*
) extends SimpleApplication(appStates: _*) {
  // implicit val timeout = Timeout(10.seconds)
  // implicit val scheduler = actorSystem.scheduler

  override def simpleInitApp(): Unit = {

    // val ship = ed.createEntity()
    // val mbState = stateManager().state[EntityDataState]().map(_.getEntityData())
    // val mbState = Try(
    //   stateManager()
    //     .state[TestAppState]()
    //     .entity
    // ).toOption.flatten.toRight(println("empty"))
    // // .flatMap(_.entity)
    // val x = mbState.flatMap(
    //   _.query
    //     .filter[TestComponent]("name", new Object())
    //     // .filterOr[TestEntity](
    //     //   Filters
    //     //     .fieldEquals(classOf[TestEntity], "", null)
    //     // )
    //     .component[Tag]()
    //     .component[TestComponent]()
    //     .result
    //     .toRight(println("failed"))
    // )

    // rootNode
    //   .child(geom)
    //   .child(geom)
    //   .child(geom)
    //   .child(geom)
    //   .child(geom)
    //   .child(geom)
    //   .child(geom)
    //   .child(geom)
    // Future(println("hello"))(jmeEC(this))
    // val wbActor: Future[ActorRef[Greeter.Greet]] = actorSystem.ask(
    //   SpawnProtocol.Spawn(
    //     behavior = Greeter(),
    //     name = "listener",
    //     DispatcherSelector.fromConfig("jme-dispatcher"),
    //     _
    //   )
    // )

    // wbActor.map(a => a.ask(Greeter.Greet("hello", _)).map(println))

  }
  override def simpleUpdate(tpf: Float): Unit = {
    // val rot2 = rot.fromAngleAxis(FastMath.PI, new Vector3f(0, 0, 1))
    // val rotation = geom.getLocalRotation()
    // rotation.add(rot2)
    // geom.rotate(rot2)

    // geom.updateModelBound()
    // geom.updateGeometricState()
  }

  // override def stop(): Unit = {
  //   actorSystem.terminate()
  //   super.stop()
  // }

}

object GameApp {
  // def myExec(app: SimpleApplication, command: Runnable) = {
  //   app.enqueue(command)
  // }
  // val javaFxExecutionContext: ExecutionContext =
  //   ExecutionContext.fromExecutor(new Executor {
  //     def execute(command: Runnable): Unit = {
  //       Platform.runLater(command)
  //     }
  //   })
  // def jmeEC(app: SimpleApplication): ExecutionContext =
  //   ExecutionContext.fromExecutor(new Executor {
  //     override def execute(command: Runnable): Unit = app.enqueue(command)
  //   })

  // def jmeScheduler(app: SimpleApplication) = Sch
}
