package wow.doge.mygame

import org.scalatest.funsuite.AnyFunSuite
import monix.execution.Scheduler.Implicits.global
import cats.syntax.eq._
import com.jme3.{asset => jmea}
import com.jme3.asset.DesktopAssetManager
import wow.doge.mygame.utils.wrappers.jme.AssetManager
import wow.doge.mygame.utils.wrappers.jme.AssetManager.AssetNotFound
import com.jme3.scene.Geometry
import wow.doge.mygame.utils.wrappers.jme.AssetManager.CouldNotCastError
import com.jme3.scene.Node
import com.jme3.material.MaterialDef
import com.jme3.material.Material
import scala.annotation.nowarn

@nowarn("msg=method get in class LeftProjection is deprecated")
@nowarn("msg=method right in class Either is deprecated")
@nowarn("msg=method get in class RightProjection is deprecated")
class AssetManagerTest extends AnyFunSuite {

  val _assetManager: jmea.AssetManager = new DesktopAssetManager(true)
  val assetManager = new AssetManager(_assetManager)

  test("Test for AssetNotFound error") {
    val res =
      assetManager.loadModel(os.rel / "doesnotexist").attempt.runSyncUnsafe()
    assert(res.isLeft)
    assert(res.left.get eqv AssetNotFound("doesnotexist"))
  }

  test("Test for Model CouldNotCastError") {
    val modelPath = os.rel / "Models" / "Jaime" / "Jaime.j3o"
    val res1 = assetManager
      .loadModelAs[Geometry](modelPath)
      .attempt
      .runSyncUnsafe()
    assert(res1.isLeft)
    assert(res1.left.get eqv CouldNotCastError)

    val res2 = assetManager
      .loadModelAs[Node](modelPath)
      .attempt
      .runSyncUnsafe()
    assert(res2.isRight)
    assert(res2.map(_.getName).right.get eqv "JaimeGeom-ogremesh")
  }

  test("Test for Asset CouldNotCastError") {
    val assetPath = os.rel / "Common" / "MatDefs" / "Misc" / "Unshaded.j3md"

    val res1 = assetManager
      .loadAssetAs[Material](assetPath)
      .attempt
      .runSyncUnsafe()
    assert(res1.isLeft)
    assert(res1.left.get eqv CouldNotCastError)

    val res2 = assetManager
      .loadAssetAs[MaterialDef](assetPath)
      .attempt
      .runSyncUnsafe()
    assert(res2.isRight)
    assert(res2.map(_.getName).right.get eqv "Unshaded")
  }
}
