﻿using Fineness.Framework.Interfaces;

/// <summary>
/// 
/// 数据代理管理器
/// 
/// add by Minghua.ji
/// 
/// </summary>
public class DataProxyManager : SingletonBase {
    
    public const string NAME = "FDataProxyManager";

    #region 单例
    public static DataProxyManager Instance
    {
        get
        {
            return SingletonManager.Manager.GetInstance<DataProxyManager>(NAME);
        }
    }
    #endregion

    private HashMap mDataProxyMap;

    public DataProxyManager() : base(NAME)
    {
        mDataProxyMap = new HashMap();
    }

    public override void Dispose()
    {
        base.Dispose();
        
        mDataProxyMap.Clear();
        mDataProxyMap = null;
    }

    public T GetDataProxy<T>(string name)
    {
        if (mDataProxyMap == null)
            return default(T);
        
        return (mDataProxyMap.IsContainsKey(name)) ? mDataProxyMap.GetValue<T>(name) : default(T);
    }

    public void AddDataProxy(IDataProxy proxy)
    {
        if (mDataProxyMap == null)
            return;

        if (!mDataProxyMap.IsContainsKey(proxy.Name))
            mDataProxyMap.Put(proxy.Name, proxy);
        else
            proxy.Dispose();
    }

    public void RemoveDataProxy(IDataProxy proxy)
    {
        if (mDataProxyMap == null)
            return;

        if (mDataProxyMap.IsContainsKey(proxy.Name))
            mDataProxyMap.Remove(proxy.Name);
    }

    public void Notify(string name, object param = null)
    {
        if (mDataProxyMap == null)
            return;

        if (mDataProxyMap.IsContainsKey(name))
            mDataProxyMap.GetValue<IDataProxy>(name).Notify(param);
    }
}
