using MySql.Data.MySqlClient;
using parkMonitor.Database2;
using parkMonitor.LOG;
using parkMonitor.server.CoreThread;
using parkMonitor.server.uiLogServer;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using parkMonitor.tools;
using System.Threading;

namespace parkMonitor.entity
{
    class EntityForCore
    {
        /// <summary>
        /// 系统全局状态
        /// </summary>
        public bool globalStatus { get; set; }
        public int equipmentStatus_address { set; get; }
        public int park_completed_address { set; get; }
        public int parkingEntX { set; get; }
        public int parkingEntY { set; get; }
        public int parkingEntZ { set; get; }
        public int fetch_completed_address { set; get; }
        public int wheelbase_status_address { get; set; }
        public int parking_startRobot_address { get; set; }
        public int fetching_startRobot_address { get; set; }
        public int frontWheelbase_address { get; set; }
        public int rearWheelbase_address { get; set; }
        public int centerXLimit { get; set; }
        public int centerYLimit { get; set; }
        public int angleALimit { get; set; }
        public static BlockingQueue remoteBQ;
        public static BlockingQueue localBQ;
        public static EntityForCore ins = new EntityForCore();
        public EntityForCore()
        {
            try
            {
                parking_startRobot_address = Convert.ToInt32(ConfigurationManager.AppSettings["parking_startRobot_address"]);
                fetching_startRobot_address = Convert.ToInt32(ConfigurationManager.AppSettings["fetching_startRobot_address"]);
                equipmentStatus_address = Convert.ToInt32(ConfigurationManager.AppSettings["equipmentStatus_address"]);
                park_completed_address = Convert.ToInt32(ConfigurationManager.AppSettings["park_completed_address"]);
                parkingEntX = Convert.ToInt32(ConfigurationManager.AppSettings["parkingEntX"]);
                parkingEntY = Convert.ToInt32(ConfigurationManager.AppSettings["parkingEntY"]);
                parkingEntZ = Convert.ToInt32(ConfigurationManager.AppSettings["parkingEntZ"]);
                //轮距
                frontWheelbase_address = Convert.ToInt32(ConfigurationManager.AppSettings["frontWheelbase_address"]);
                rearWheelbase_address = Convert.ToInt32(ConfigurationManager.AppSettings["rearWheelbase_address"]);
                wheelbase_status_address = Convert.ToInt32(ConfigurationManager.AppSettings["wheelbase_status_address"]);
                //取车完成
                fetch_completed_address = Convert.ToInt32(ConfigurationManager.AppSettings["fetch_completed_address"]);

                centerXLimit = Convert.ToInt32(ConfigurationManager.AppSettings["centerX"]);
                centerYLimit = Convert.ToInt32(ConfigurationManager.AppSettings["centerY"]);
                angleALimit = Convert.ToInt32(ConfigurationManager.AppSettings["angleA"]);

            }
            catch
            {
                UILogServer.ins.log("停取车基本配置异常");
                Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "停取车基本配置异常");
            }
        }
        /// <summary>
        /// 数据库配置，车位与缓冲位初始化
        /// </summary>
        /// <returns></returns>
        public bool Init()
        {
            Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "******************************************* 新的开始 *******************************************");
            bool initState = true;
            //数据库连接参数初始化及连接测试
            Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "数据库初始配置");       
            //数据库初始化
            ConnectionPoolManager.Init();
            remoteBQ = ConnectionPoolManager.InitConnPooling(10, ConnectionPoolManager.remoteConf);
            //localBQ = ConnectionPoolManager.InitConnPooling(10, ConnectionPoolManager.localConf);
            //车位管理初始化
            Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "初始化车位");
            ParkingSpaceManager.ins = new ParkingSpaceManager();
            initState = initState && ParkingSpaceManager.ins.InitParkingSpace();
            //缓冲位初始化
            Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "初始化缓冲位");
            ParkingBufferManager.ins = new ParkingBufferManager();
            Thread.Sleep(4000);
            initState = initState && ParkingBufferManager.ins.InitParkingBuffers();
            if (initState)
            {
                UILogServer.ins.info("系统初始化完成");
            }
            return initState;
        }

        /// <summary>
        /// 中控系统初始化
        /// </summary>
        /// <param name="DBmode">0 默认检查所有DB；1 远端；2 本地</param>
        /// <returns></returns>
        public bool ReInit(int DBmode)
        {
            Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "------------------------------- 重新初始化系统 ---------------------------------");
            UILogServer.ins.info("系统初始化中......");
            bool initState = true;
            //数据库连接参数初始化及连接测试
            Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "数据库初始配置");
            if(DBmode==0 || DBmode == 1)
            {
                ConnectionPoolManager.CloseAll(remoteBQ);
                remoteBQ = ConnectionPoolManager.InitConnPooling(10, ConnectionPoolManager.remoteConf);
            }
            if (DBmode == 0 || DBmode == 2)
            {
                //ConnectionPoolManager.CloseAll(localBQ);
                //localBQ = ConnectionPoolManager.InitConnPooling(10, ConnectionPoolManager.localConf);
            }
            //车位管理初始化
            Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "初始化车位");
            if (ParkingSpaceManager.ins == null)
            {
                ParkingSpaceManager.ins = new ParkingSpaceManager();
            }
            initState = initState && ParkingSpaceManager.ins.InitParkingSpace();
            //缓冲位初始化
            Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "初始化缓冲位");
            if (ParkingSpaceManager.ins == null)
            {
                ParkingBufferManager.ins = new ParkingBufferManager();
            }
            initState = initState && ParkingBufferManager.ins.InitParkingBuffers();
            if (initState)
            {
                UILogServer.ins.info("系统初始化完成");
            }
            else
            {
                UILogServer.ins.info("系统初始化失败");
            }
            return initState;
        }
    }
}
