using KitsuneCafe.System;
using UnityEngine;
using Wacs.Core.Runtime;
using Wacs.Core.Runtime.Types;

namespace KitsuneCafe.Scripting
{
    public interface IModule
    {
        string Name { get; }
        ModuleInstance Instantiate(WasmRuntime runtime);
    }

    [CreateAssetMenu(menuName = KitsuneCafeMenu.Module + "Module")]
    public class Module : ScriptableObject, IModule
    {
        [SerializeField]
        private string moduleName;
        public string Name => moduleName;

        [SerializeField]
        private WasmAsset wasm;

        [SerializeField]
        private bool SkipModuleValidation = false;

        [SerializeField]
        private bool SkipStartFunction = false;

        public ModuleInstance Instantiate(WasmRuntime runtime)
        {
            var module = wasm.CreateModule();
            var instance = runtime.InstantiateModule(module, GetRuntimeOptions());
            return instance;
        }

        private RuntimeOptions GetRuntimeOptions()
        {
            return new RuntimeOptions
            {
                SkipModuleValidation = SkipModuleValidation,
                SkipStartFunction = SkipStartFunction
            };
        }
    }
}
