package wow.doge.mygame
import org.scalatest.funsuite.AnyFunSuite
import com.jme3.anim.AnimClip
import wow.doge.mygame.implicits._
import wow.doge.mygame.utils.wrappers.jme.AssetManager
import com.jme3.asset.DesktopAssetManager
import com.jme3.scene.Spatial
import monix.bio.UIO
import scala.concurrent.duration._
import com.jme3.scene.Node
import com.jme3.anim.AnimComposer
import com.jme3.anim.SkinningControl
import com.jme3.anim.tween.action.BaseAction
import com.jme3.anim.tween.Tweens
import scala.jdk.javaapi.CollectionConverters._
import com.jme3.anim.tween.Tween
import com.jme3.anim.tween.action.ClipAction
import cats.syntax.all._

class AnimTest extends AnyFunSuite {
  import monix.execution.Scheduler.Implicits.global
  val assetManager = new AssetManager(new DesktopAssetManager(true))

  test("test 1") {
    println((for {
      _ <- UIO.unit
      model <- assetManager.loadModelAs[Node](
        os.rel / "Models" / "Oto" / "Oto.mesh.xml"
      )
      animcontrol <- UIO(model.getControlMaybe(classOf[AnimComposer]))
      skinningcontrol <- UIO(model.getControlMaybe(classOf[SkinningControl]))
      _ <- UIO(println(animcontrol))
      _ <- UIO(println(skinningcontrol))
      _ <- UIO {
        animcontrol.map { ac =>
          // ac.actionSequence()
          // ac.makeAction()
          //   new BaseAction(Tweens.sequence())
          //   ac.getAnimClips().a
          Option(ac.getAnimClip("hmm"))
            .map(clip => new ClipAction(clip))
            .map(Tweens.sequence(_))
            .foreach { t =>
              val actions = new BaseAction(t)
              ac.addAction("hmm", actions)
            }

          val names = List("Walk", "Jump")
          for {
            clips <- names.traverse(name =>
              Option(ac.getAnimClip(name)).map(clip => new ClipAction(clip))
            )
            tween <- Tweens.sequence(clips: _*).some
            actions <- new BaseAction(tween).some
            _ <- ac.addAction("Sequence 1", actions).some
          } yield ()

          ()
        }
      }
    } yield model).attempt.runSyncUnsafe(10.seconds))
  }
}
