using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using SnmpSharpNet;

using System.Threading;

using LFP_Manager.DataStructure;
using LFP_Manager.Threads;
using LFP_Manager.Utils;

using System.Diagnostics; 

namespace LFP_Manager
{
    public partial class fmMain : Form
    {
        DeviceSystemData[] SystemData;
        csSnmpThread snmpThread;

        public fmMain()
        {
            InitializeComponent();

            StructDataInit();

            cbType.SelectedIndex = 0;

            ucShelfA.Start(0, SystemData[0]);
            ucShelfB.Start(1, SystemData[1]);
            ucShelfC.Start(2, SystemData[2]);

            tabSystemStatus.TabPages[1].PageVisible = false;
            tabSystemStatus.TabPages[2].PageVisible = false;

            pgSystem1.Text = "SYSTEM";

            tmrDisplay.Start();
        }

        private void StructDataInit()
        {
            SystemData = new DeviceSystemData[csConstData.SystemInfo.MAX_SYSTEM_SIZE];

            for (int i = 0; i < csConstData.SystemInfo.MAX_SYSTEM_SIZE; i++)
            {
                SystemData[i] = new DeviceSystemData();
                SystemData[i].ValueData = new DeviceValueData();
                SystemData[i].ValueData.CellVoltage = new short[csConstData.SystemInfo.MAX_MODULE_CELL_SIZE];
                SystemData[i].ValueData.CellTemperature = new short[csConstData.SystemInfo.MAX_MODULE_TEMP_SIZE];

                SystemData[i].AvgData = new DeviceAvgData();
                SystemData[i].StatusData = new DeviceStatusData();
                
                SystemData[i].CalibriationData = new DeviceCalibration();
                SystemData[i].CalibriationData.CellVoltge = new CellVoltageCalib();
                SystemData[i].CalibriationData.SystemVoltage = new SystemVoltageCalib();
                SystemData[i].CalibriationData.ForcedBalancing = new ForcedBalControl();
                SystemData[i].CalibriationData.Battery = new BatteryParameter();
            }
        }

        private void UpdateData(object sender, DeviceSystemData[] rData)
        {
            SystemData = rData;
            ucShelfA.UpdateData(SystemData[0]);
            ucShelfB.UpdateData(SystemData[1]);
            ucShelfC.UpdateData(SystemData[2]);
        }

        #region EVENT FUNCTION

        private void tmrDisplay_Tick(object sender, EventArgs e)
        {
            if (snmpThread != null)
            {
                string data = snmpThread.GetResult();

                if (data != "")
                {
                    meDataLog.Text = data + "\r\n" + meDataLog.Text;
                }
            }
        }

        private void fmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            tmrDisplay.Stop();
            if (snmpThread != null)
                snmpThread.disposeThread();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (snmpThread.GetStatus())
            {
                snmpThread.Start(teTargetIP.Text);
                teTargetIP.Enabled = false;
                btnStart.Text = "Stop";
            }
            else
            {
                snmpThread.Stop();
                teTargetIP.Enabled = true;
                btnStart.Text = "Start";
            }
        }

        private void btnStart_ClickA(object sender, EventArgs e)
        {
            //if (snmpThread == null)
            //{
            //    snmpThread = new csSnmpThread(Config, SystemData);
            //    snmpThread.OnUpdate += UpdateData;

            //    snmpThread.Start(teTargetIP.Text);

            //    teTargetIP.Enabled = false;
            //    cbType.Enabled = false;
            //    btnStart.Text = "Stop";
            //}
            //else
            //{
            //    snmpThread.Stop();
            //    snmpThread.disposeThread();
            //    snmpThread = null;

            //    teTargetIP.Enabled = true;
            //    cbType.Enabled = true;
            //    btnStart.Text = "Start";
            //}
        }

        #endregion

        #region SUB EVENT

        private void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbType.SelectedIndex)
            {
                case 0:         // KT LFPS-48100
                case 1:         // KT LFPR-48300
                case 4:         // ESS LFPS-48150
                    tabSystemStatus.TabPages[0].PageVisible = true;
                    tabSystemStatus.TabPages[1].PageVisible = false;
                    tabSystemStatus.TabPages[2].PageVisible = false;

                    pgSystem1.Text = "SYSTEM";
                    break;
                case 2:         // KT LFPR-48600
                    tabSystemStatus.TabPages[0].PageVisible = true;
                    tabSystemStatus.TabPages[1].PageVisible = true;
                    tabSystemStatus.TabPages[2].PageVisible = false;

                    pgSystem1.Text = "SYSTEM #A";
                    pgSystem2.Text = "SYSTEM #B";
                    break;
                case 3:         // KT LFPR-48900
                    tabSystemStatus.TabPages[0].PageVisible = true;
                    tabSystemStatus.TabPages[1].PageVisible = true;
                    tabSystemStatus.TabPages[2].PageVisible = true;

                    pgSystem1.Text = "SYSTEM #A";
                    pgSystem2.Text = "SYSTEM #B";
                    pgSystem3.Text = "SYSTEM #C";
                    break;
            }
        }

        #endregion

        private void btnOpenLogFolder_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process ps = new System.Diagnostics.Process();
            ps.StartInfo.FileName = "explorer.exe";
            ps.StartInfo.Arguments = csLog.GetLogFolder(Application.ExecutablePath);
            ps.StartInfo.WorkingDirectory = csLog.GetLogFolder(Application.ExecutablePath);
            ps.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;

            ps.Start();
        }

        #region LOG TIMER TICK EVENT

        DateTime bakDateTime;

        private void tmrLogging_Tick(object sender, EventArgs e)
        {
            DateTime cDate = DateTime.Now;
            int ss;

            if (cbType.Enabled == false)
            {
                if (SystemData[0].CommFail == false)
                {
                    ss = Convert.ToInt16(cbLogTime.Text);

                    if (
                        ((bakDateTime.Minute != cDate.Minute)
                        || (bakDateTime.Second != cDate.Second))
                        && ((cDate.Second % ss) == 0)
                        )
                    {
                        for (int i = 0; i < csConstData.SystemInfo.MAX_SYSTEM_SIZE; i++)
                        {
                            try
                            {
                                //csLog.SystemDataLog(i + 1, Config, SystemData[i], cDate, Application.ExecutablePath);
                                bakDateTime = cDate;
                            }
                            catch (Exception ex)
                            {
                                string snmpResult = "[" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "] " + String.Format("Exception : {0}", ex.Message);

                                meDataLog.Text = snmpResult + "\r\n" + meDataLog.Text;
                            }
                        }
                    }
                }
            }
        }

        #endregion

        bool logging = false;

        private void btnLogStart_Click(object sender, EventArgs e)
        {
            if (logging == true)
            {
                tmrLogging.Stop();
                cbLogTime.Enabled = true;
                logging = false;
                btnLogStart.Text = "Log Start";
            }
            else
            {
                tmrLogging.Start();
                cbLogTime.Enabled = false;
                logging = true;
                btnLogStart.Text = "Log Stop";
            }
        }

        private void btnLogClear_Click(object sender, EventArgs e)
        {
            meDataLog.Text = "";
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

        }

        private void serialPort1_ErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
        {

        }

        private void serialPort1_PinChanged(object sender, System.IO.Ports.SerialPinChangedEventArgs e)
        {

        }

        private void gcTargetConfig_Paint(object sender, PaintEventArgs e)
        {

        }

    }
}
