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

namespace Stary.Evo
{
    public class IOCContainer
    {
        /// <summary>
        /// 实例
        /// </summary>
        public Dictionary<Type, object> mInstances = new Dictionary<Type, object>();

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="instance"></param>
        /// <typeparam name="T"></typeparam>
        public void Register<T>(T instance)
        {
            var key = typeof(T);

            if (mInstances.ContainsKey(key))
            {
                mInstances[key] = instance;
            }
            else
            {
                mInstances.Add(key, instance);
            }
        }

        /// <summary>
        /// 注销
        /// </summary>
        public void UnRegister<T>() where T : class
        {
            var key = typeof(T);


            if (mInstances.TryGetValue(key, out var retObj))
            {
                retObj = null;
                mInstances.Remove(key);
            }
            else
            {
                Debug.LogErrorFormat("IOC容器里不存在Key:--【{0}】--请用对应创建的key进行调用",key);
                
            }
        }
        public T Get<T>() where T : class
        {
            var key = typeof(T);


            if (mInstances.TryGetValue(key, out var retObj))
            {
                return retObj as T;
            }
            else
            {
                Debug.LogErrorFormat("IOC容器里不存在Key:--【{0}】--请用对应创建的key进行调用",key);
                
            }

            return null;
        }
    }

}