using ServiceCenter;
using ServiceCenter.SqlSugars;
using WCS.Core;
using WCS.Entity.Protocol;
using WCS.Entity.Protocol.BCR;
using WCS.Entity.Protocol.HUB;
using WCS.Entity.Protocol.RGV;
using WCS.Entity.Protocol.Robot;
using WCS.Entity.Protocol.SRM;
using WCS.Entity.Protocol.Station;
using WCS.Entity.Protocol.Truss;
using WCS.WorkEngineering.Extensions;
using DeviceFlags = WCS.WorkEngineering.Extensions.DeviceFlags;

namespace WCS.WorkEngineering
{
    /// <summary>
    /// 业务工程配置信息
    /// </summary>
    public static class WorkStart
    {
        /// <summary>
        ///  初始化 设备信息
        /// </summary>

        public static void InitializeDeviceInfo()
        {
            var conv = new Device("1");
            conv.AddFlag(DeviceFlags.输送机);
            conv.AddProtocol<IStation520>(0, 520, "1");
        }

        /// <summary>
        ///  初始化数据库连接
        /// </summary>
        /// <param name="datas"></param>
        public static void InitDB(this List<DataBaseConnectionString> datas)
        {
            //初始化数据库
            SqlSugarHelper.Do(db =>
            {
                foreach (var connectionString in datas!)
                {
                    var _db = db.Connect.GetConnectionScope(connectionString.Key);
                    switch (connectionString.Key)
                    {
                        case "WCSDB"://WCS基本数据库
                            SqlSugarHelper.SetDefault(connectionString.Key);
                            break;

                        case "WCSDlog"://WCS日志数据库

                            break;

                        case "PLC"://PLC

                            SqlSugarHelper.SetPLC(connectionString.Key);

                            break;

                        case "PlcEx"://PLC

                            SqlSugarHelper.SetPLCEX(connectionString.Key);

                            //_db.DbMaintenance.CreateDatabase();
                            _db.CodeFirst.InitTables<DevRunInfo>();
                            _db.CodeFirst.InitTables<DevAlarmInfo>();

                            break;

                        default: //其他库
                            break;
                    };
                };
            });
        }
    }

    public class DevDbConfig<T>
    {
        public DevDbConfig()
        {
        }

        public DevDbConfig(string ip, T code)
        {
            IP = ip;
            Code = code;
        }

        public DevDbConfig(string ip, List<DevInterval<T>> devIntervalList)
        {
            IP = ip;
            DevIntervalList = devIntervalList;
        }

        public DevDbConfig(string ip, List<T> devCodeList)
        {
            IP = ip;
            DevCodeList = devCodeList;
        }

        public string IP { get; set; }

        public T Code { get; set; }

        public T StartCode { get; set; }

        public T EndCode { get; set; }

        public List<T> DevCodeList { get; set; }

        public List<DevInterval<T>> DevIntervalList { get; set; }
    }

    public class DevInterval<T>
    {
        public DevInterval(T s, T e)
        {
            StartCode = s;
            EndCode = e;
        }

        public T StartCode { get; set; }

        public T EndCode { get; set; }
    }
}
