package wow.doge.mygame.subsystems.scriptsystem

import akka.actor.typed.ActorRef
import akka.actor.typed.Scheduler
import akka.actor.typed.SpawnProtocol
import akka.util.Timeout
import monix.bio.Task
import wow.doge.mygame.scriptsystem.ScriptCachingActor
import wow.doge.mygame.utils.AkkaUtils

/**
  * Scripts can either be searched and compiled at startup (Eager mode)
  * or compiled on demand (Lazy mode)
  */
sealed trait ScriptInitMode
object ScriptInitMode {
  case object Eager extends ScriptInitMode
  case object Lazy extends ScriptInitMode
}
class ScriptSystemResource(
    path: os.Path,
    mode: ScriptInitMode = ScriptInitMode.Lazy
)(implicit
    spawnProtocol: ActorRef[SpawnProtocol.Command],
    timeout: Timeout,
    scheduler: Scheduler
) {

  val init = for {
    scriptFiles <- Task(
      findScriptFiles(os.pwd / "assets" / "scripts")
    ).hideErrors
    scriptCacheActor <- AkkaUtils.spawnActorL(
      ScriptCachingActor()
      // "scriptCachingActor"
    )
  } yield scriptCacheActor

  def findScriptFiles(wd: os.Path) =
    os.walk
      .stream(wd)
      .filter(p =>
        os.isFile(p) &&
          (p.ext == "sc" || (p.baseName + "." + p.ext)
            .contains(".main.kts") || p.ext == "groovy")
      )
      .toList

  // def findExternalScriptFiles =
  //   findScriptFiles(os.pwd / "assets" / "scripts")
  // def findInternalScriptFiles =
  //   findScriptFiles((os.resource / "assets" / "scripts"))

  // def finalInternalScriptFiles =
}
// pwd / 'src / 'main / 'resources / 'assets / 'scripts
