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

namespace KitsuneCafe.Scripting
{
    public interface IFunctionBinding : IHostBinding
    {
        IOption<IFunctionState> Bind(WasmRuntime runtime);
    }

    public interface IFunctionState { }

    public abstract class FunctionBinding : ScriptableObject, IFunctionBinding
    {
        public abstract BindingPath BindingPath { get; }

        public abstract IOption<IFunctionState> Bind(WasmRuntime runtime);
    }

    [CreateAssetMenu(menuName = KitsuneCafeMenu.ModuleFunctionBinding + "SayC")]
    public class SayCBinding : FunctionBinding
    {
        public class State : IFunctionState
        {
            public StringBuilder Output = new();
        }

        public static readonly BindingPath SayCBindingPath = new("env", "sayc");

        public override BindingPath BindingPath => SayCBindingPath;

        public override IOption<IFunctionState> Bind(WasmRuntime runtime)
        {
            var state = new State();

            runtime.BindHostFunction<Action<char>>(BindingPath, c =>
            {
                state.Output.Append(c);
            });

            return Option.Some(state as IFunctionState);
        }
    }
}
