using System.Collections.Generic;
using KitsuneCafe.System;
using UnityEngine;
using Wacs.Core.Runtime;

namespace KitsuneCafe.Scripting
{
    public interface IEnvironment
    {
        void Bind(WasmRuntime runtime, RuntimeState state);
    }

    public class RuntimeState : Dictionary<BindingPath, IFunctionState> { }

    [CreateAssetMenu(menuName = KitsuneCafeMenu.Module + "Environment")]
    public class Environment : ScriptableObject, IEnvironment
    {
        [SerializeField]
        private List<FunctionBinding> functions = new();

        public void Bind(WasmRuntime runtime, RuntimeState state)
        {
            BindFunctions(runtime, state);
        }

        private void BindFunctions(WasmRuntime runtime, RuntimeState state)
        {
            foreach (var fn in functions)
            {
                var result = fn.Bind(runtime);

                if (result.IsSome)
                {
                    state.Add(fn.BindingPath, result.Unwrap());
                }
            }
        }
    }
}
