using LFP_Manager.DataStructure;
using LFP_Manager.Utils;
using System;
using System.Windows.Forms;

namespace LFP_Manager.Controls
{
    public delegate void LogDataUpdate(object sender, string LogResult, bool active, int aLogTime);

    public partial class ucDataLog : DevExpress.XtraEditors.XtraUserControl
    {
        #region VARIABLES

        private CommConfig Config;
        private DeviceSystemData[] SystemData;
        private DateTime bakDateTime;

        private int ModuleID = 1;

        private string LogFileName = "";
        private string LogFileNameTotal = "";

        private bool logging = false;
        private bool Active = false;

        public event LogDataUpdate OnUpdate = null;

        #endregion

        #region CONSTRUCTORS

        public ucDataLog()
        {
            InitializeComponent();
        }

        #endregion

        #region PUBLIC UPDATE

        public void UpdateActiveStatus(bool aStatus, int mID, CommConfig aConfig, DeviceSystemData[] aSystemData)
        {
            Active = aStatus;
            ModuleID = mID;
            Config = aConfig;
            SystemData = aSystemData;
        }

        public void UpdateData(DeviceSystemData[] aSystemData)
        {
            SystemData = aSystemData;
        }

        #endregion

        #region TIMER EVENT

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

            if ((logging) && (Active))
            {
                ss = Convert.ToInt16(cbLogTime.Text);

                if (
                    ((bakDateTime.Minute != cDate.Minute)
                    || (bakDateTime.Second != cDate.Second))
                    && ((cDate.Second % ss) == 0)
                    )
                {
                    try
                    {
                        switch (Config.CommType)
                        {
                            case csConstData.CommType.COMM_UART:
                                csLog.SystemDataLog(ModuleID, Config, SystemData[ModuleID - 1], cDate, Application.ExecutablePath, LogFileName);
                                break;
                            case csConstData.CommType.COMM_RS485:
                                if (Config.ModuleQty > 1)
                                {
                                    for (int i = 0; i < Config.ModuleQty; i++)
                                    {
                                        csLog.SystemDataLog(i + 1, Config, SystemData[i], cDate, Application.ExecutablePath, LogFileName);
                                    }
                                }
                                else
                                {
                                    csLog.SystemDataLog(ModuleID, Config, SystemData[ModuleID - 1], cDate, Application.ExecutablePath, LogFileName);
                                }
                                break;
                            case csConstData.CommType.COMM_SNMP:
                                csLog.SystemDataLog(1, Config, SystemData[0], cDate, Application.ExecutablePath, LogFileName);
                                break;
                            default:
                                break;
                        }
                        bakDateTime = cDate;
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }

        #endregion

        #region BUTTON EVENT

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

                if (OnUpdate != null)
                {
                    OnUpdate(this, String.Format("LogStop: {0:yyyy/MM/dd HH:mm:ss}", DateTime.Now), false, Convert.ToInt16(cbLogTime.Text));
                }
            }
            else
            {
                LogFileNameTotal = String.Format("{0:yyMMddHHmmss}", DateTime.Now);
                LogFileName = String.Format("{0:yyMMddHHmmss}", DateTime.Now);
                tmrLogging.Start();
                cbLogTime.Enabled = false;
                logging = true;
                btnLogStart.Text = "Log Stop";

                if (OnUpdate != null)
                {
                    OnUpdate(this, String.Format("Logging: SHELFX_LOG_{0}.csv", LogFileName), true, Convert.ToInt16(cbLogTime.Text));
                }
            }
        }

        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();
        }

        #endregion
    }
}
