using System;
using System.Collections.Generic;
using UnityEngine;

namespace FVS
{
    /// <summary>
    /// FVS-Unity Action管理器:
    /// Action指 Unity端提供给 FVS端主动调用的方法
    /// </summary>
    public class FVSActionManager : MonoBehaviour
    {
        private static Dictionary<string, Action<string[]>> _actions = new Dictionary<string, Action<string[]>>();
        void CallUnityAction(string data)
        {
            try
            {
                ActionEvent actionEvent = LitJson.JsonMapper.ToObject<ActionEvent>(data);
                if (_actions.ContainsKey(actionEvent.name))
                {
                    if (_actions[actionEvent.name] != null)
                    {
                        _actions[actionEvent.name].Invoke(actionEvent.eventData);
                    }
                }
            } 
            catch (Exception e)
            {
                print("UnityAction参数解析错误:" + e);
            }
        }

        public static void Bind(string name, Action<string[]> action)
        {

            if (_actions.ContainsKey(name))
            {
                Debug.LogError("已存在同名的Action");
            }
            else
            {
                _actions.Add(name, action);
            }
        }

        public static void UnBind(string name)
        {
            if (_actions.ContainsKey(name))
            {
                _actions.Remove(name);
            }
        }

        struct ActionEvent
        {
            public string name;
            public string[] eventData;
        }

    }
}

