using MySql.Data.MySqlClient;
using parkMonitor.entity;
using parkMonitor.model;
using parkMonitor.server.CoreThread;
using parkMonitor.server.uiLogServer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace parkMonitor.controlPanel
{
    public partial class ControlPanel : Form
    {
        public static ControlPanel ins = null;
        public IEquipments plc = null;
        Dictionary<int, CheckBox> IndexCbMap = null;

        public ControlPanel()
        {
            InitializeComponent();
            IndexCbMap = new Dictionary<int, CheckBox>();
            //BufferList.Controls.Add(flp_buffer);
            DisplayBufferStatus();
        }

        /*            if (IndexCbMap == null) { IndexCbMap = new Dictionary<int, CheckBox>(); }
            if (ParkingBufferManager.ins != null && IndexCbMap.Count == ParkingBufferManager.ins.bufferCount)
            {
                for (int i = 0; i < ParkingBufferManager.ins.bufferCount; i++)
                {
                    ParkingBufferManager.ins.bufferList[i].occupied = IndexCbMap[i].Checked;
                }
            }
            UpdateConfirmState();
            if (!ParkingBufferManager.ins.checkedManually)
            {
                lb_confirmed.Text = "缓冲位已确认";
            }
            else
            {
                lb_confirmed.Text = "缓冲位已修改";
            }
            ParkingBufferManager.ins.checkedManually = true;*/
        public void DisplayBufferStatus()
        {
            if (IndexCbMap == null) { IndexCbMap = new Dictionary<int, CheckBox>(); }
            if (ParkingBufferManager.ins != null)
            {
                if (IndexCbMap.Count != ParkingBufferManager.ins.bufferList.Count())
                {
                    flp_buffer.Controls.Clear();
                    for (int i = 0; i < ParkingBufferManager.ins.bufferList.Count(); i++)
                    {
                        ParkingBuffer temp = ParkingBufferManager.ins.bufferList[i];
                        CheckBox cb = new CheckBox();
                        cb.Text = "第 " + temp.bufferID + " 号缓冲位";
                        cb.Checked = temp.occupied;
                        IndexCbMap.Add(i, cb);
                        flp_buffer.Controls.Add(cb);
                    }
                }
                else
                {
                    for (int i = 0; i < ParkingBufferManager.ins.bufferList.Count(); i++)
                    {
                        IndexCbMap[i].Checked = ParkingBufferManager.ins.bufferList[i].occupied;
                    }
                }
            }
        }

        private void btn_refresh_Click(object sender, EventArgs e)
        {
            DisplayBufferStatus();
        }

        private void btn_remoteDB_Click(object sender, EventArgs e)
        {
            //bool initState = false;
            //if (ConnectionPoolManager.malfunctionRemote)
            //{
            //    using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
            //    //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
            //    {
            //        Operation.TryOpen(conn);
            //        initState = conn.Ping();
            //        Operation.DBClose(conn,null,null);
            //    }
            //    if (initState)
            //    {
            //        Operation.malfunctionRemote = false;
            //        MessageBox.Show("远端数据库已成功复位");
            //    }
            //}
        }

        private void btn_localDB_Click(object sender, EventArgs e)
        {
            //bool initState = false;
            //if (ConnectionPoolManager.malfunctionLocal)
            //{
            //    using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
            //    //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
            //    {
            //        Operation.TryOpen(conn);
            //        initState = conn.Ping();
            //        Operation.DBClose(conn, null, null);
            //    }
            //    if (initState)
            //    {
            //        Operation.malfunctionLocal = false;
            //        MessageBox.Show("本地数据库已成功复位");
            //    }
            //}
        }

        private void btn_reInit_Click(object sender, EventArgs e)
        {
            PLCReconnect();
            InitWin iw = new InitWin();
            iw.ShowDialog();
        }

        private void btn_pause_Click(object sender, EventArgs e)
        {
            if (EntityForCore.ins.globalStatus)
            {
                EntityForCore.ins.globalStatus = false;
                UILogServer.ins.warn("处理流程已暂停");
            }
        }

        private void btn_recover_Click(object sender, EventArgs e)
        {
            if (!EntityForCore.ins.globalStatus)
            {
                EntityForCore.ins.globalStatus = true;
                UILogServer.ins.warn("处理流程已恢复");
            }
        }

        private void btn_PLC_Click(object sender, EventArgs e)
        {
            PLCReconnect();
        }

        private void PLCReconnect()
        {
            IEquipments plc = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.PLC);
            if (plc != null)
            {
                ControlMessage cm = new ControlMessage();
                cm.status = 10;
                plc.SetMessage(cm);
            }
        }
    }
}
