﻿using EzFramework.Utils;
using System;
using System.Collections.Generic;
using System.Linq;


namespace EzFramework.Data
{
    /// <summary>
    /// 数据库配置信息管理器
    /// 此类主要对数据库连接模型信息进行管理，包括连接模型信息的存储、获取、新增、移除等
    /// </summary>
    internal class ConnectMaster
    {

        /// <summary>
        /// 链接字符串集合
        /// </summary>
        private static IList<ConnectionInfo> connections = new List<ConnectionInfo>();

        /// <summary>
        /// 链接字符串集合
        /// </summary>
        private static IDictionary<string, IList<ConnectionInfo>> pluginConnections = new Dictionary<string, IList<ConnectionInfo>>();

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">数据库连接信息</param>
        public static void Add(ConnectionInfo entity)
        {
            if (connections.Count(p => entity.Key.Equals(p.Key)) == 0)
            {
                connections.Add(entity);
            }
        }
        /// <summary>
        /// 添加插件的链接对象
        /// </summary>
        /// <param name="pluginName">插件名称(不区分大小写)</param>
        /// <param name="entity">数据库连接信息</param>
        public static void Add(string pluginName, ConnectionInfo entity)
        {
            if (entity != null)
            {
                pluginName = pluginName.ToLower();
                IList<ConnectionInfo> conns = null;
                if (pluginConnections.ContainsKey(pluginName))
                {
                    conns = pluginConnections[pluginName];
                }
                else
                {
                    conns = new List<ConnectionInfo>();
                    pluginConnections.Add(pluginName, conns);
                }
                conns = conns ?? new List<ConnectionInfo>();
                if (conns.Count(p => entity.Key.Equals(p.Key)) == 0)
                {
                    conns.Add(entity);
                }
            }
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="key">域</param>
        /// <returns>数据库连接信息</returns>
        public static ConnectionInfo Get(string key)
        {
            ConnectionInfo entity = connections.FirstOrDefault(p => key.Equals(p.Key));
            if (entity == null)
            {
                LogHelper.Error("The connection is not exits in current key! " + key);
            }
            return entity;
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="pluginName">插件名</param>
        /// <param name="key">域</param>
        /// <returns></returns>
        public static ConnectionInfo Get(string pluginName, string key)
        {
            ConnectionInfo entity = null;
            IList<ConnectionInfo> entities = pluginConnections[pluginName.ToLower()];
            if (entities != null && entities.Count()>0)
            {
                entity = entities.FirstOrDefault(p => key.Equals(p.Key));
            }
            if (entity == null)
            {
                LogHelper.Error("The plugin '" + pluginName + "' connection is not exits in current key! " + key);
            }
            return entity;
        }

        /// <summary>
        /// 获取连接串集合
        /// </summary>
        /// <returns>数据库连接信息集合</returns>
        public static IList<ConnectionInfo> GetConnections()
        {
            return connections;
        }

        /// <summary>
        /// 获取插件连接串集合
        /// </summary>
        /// <param name="pluginName">插件名称(不区分大小写)</param>
        /// <returns>数据库连接信息集合</returns>
        public static IList<ConnectionInfo> GetConnections(string pluginName)
        {
            return pluginConnections[pluginName.ToLower()];
        }

        /// <summary>
        /// 检查是否配置了基础链接串
        /// </summary>
        /// <returns>true 已配置 fase 为配置</returns>
        public static bool BaseConnectionExits()
        {
            return connections.Count() > 0;
        }
    }
}
