using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using LFP_Manager.DataStructure;
using LFP_Manager.Utils;

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

        CommConfig Config;
        DeviceSystemData SystemData;
        DateTime bakDateTime;

        String LogFileName = "";
        String LogFileNameTotal = "";

        bool logging = false;
        bool Active = false;

        public event LogDataUpdate OnUpdate = null;

        #endregion

        #region CONSTRUCTORS

        public ucDataLog()
        {
            InitializeComponent();
        }

        #endregion

        #region PUBLIC UPDATE

        public void UpdateActiveStatus(bool aStatus, CommConfig aConfig, ref DeviceSystemData aSystemData)
        {
            Active = aStatus;
            Config = aConfig;
            SystemData = aSystemData;
        }

        public void UpdateData(ref 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)
                    )
                {
                    // CSV Log Process
                    try
                    {
                        csLog.SystemDataLog(1, Config, SystemData, cDate, Application.ExecutablePath, LogFileName);
                        bakDateTime = cDate;
                    }
                    catch (Exception)
                    {
                    }

                    //// Database Log Process
                    //csDbUtils.LogDbCreate(csConstData.CAN_MODEL[Config.CanModelIndex]);
                    //csDbUtils.BmsLogDataInsert(ref Config, ref SystemData, cDate);
                }
            }
        }

        #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";

                OnUpdate?.Invoke(this, String.Format("Logging: SHELFX_LOG_{0}.csv", LogFileName), true, Convert.ToInt16(cbLogTime.Text));

                csDbUtils.LogDbCreate(csConstData.CommType.CAN_MODEL[Config.TargetModelIndex]);
            }
        }

        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
    }
}
