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;
using iSTP_Embedded;

namespace iSTP_DataProcessing
{
    /// <summary>
    /// Information about conditions for SPS mode
    /// </summary>
    public partial class TempStabTimeWnd : Form
    {
        private Dictionary<string, SPSLogger.ActiveAccGyroStruct> MySPSconfig;
        private bool TimeIs = false;
        private bool ConditionsIs = false;

        private const double Conditions_def_Offset = 30d;
        private const double Conditions_def_TimeLimit = 30d;
        private const double Conditions_def_Temp = 65d;
        private const double Conditions_def_MaxRangle = Conditions_def_Temp + 0.2;
        private const double Conditions_def_MinRangle = Conditions_def_Temp - 0.2;
        private const double Conditions_def_FailTime = 120d;

        public enum Headers_e
        { 
            Name = 0,
            Time = 1,
            Active_time = 2,
            Offset = 3,
            TimeLimit = 4,
            Temp = 5,
            MaxTemp = 6,
            MinTemp = 7,
            FailTime = 8,
            ActiveConditions = 9
        }

        public TempStabTimeWnd(Dictionary<string, SPSLogger.ActiveAccGyroStruct> configSPS)
        {
            InitializeComponent();
            InitializaWnd(configSPS);

            textBoxTime4All.Text = Conditions_def_TimeLimit.ToString();
            textBoxTemp4All.Text = Conditions_def_Temp.ToString();
            textBoxTempMAXlimit.Text = Conditions_def_MaxRangle.ToString();
            textBoxTempMINlimit.Text = Conditions_def_MinRangle.ToString();
            textBoxOffsetTime4All.Text = Conditions_def_Offset.ToString();
            textBoxFailTime4All.Text = Conditions_def_FailTime.ToString();

            for (int y = 0; y < configSPS.Count; y++)
            {
                dataGridView1.Rows[y].Frozen = false;
                ChangeEnablePartOfRow(y, false, false);
            }
        }

        public Dictionary<string, SPSLogger.ActiveAccGyroStruct> GetModifySPSconfig { get { return MySPSconfig; } }

        public Dictionary<string, SPSLogger.ActiveAccGyroStruct> ReturnAndModifySPSconfig(Dictionary<string, SPSLogger.ActiveAccGyroStruct> configSPS)
        {
            Dictionary<string, SPSLogger.ActiveAccGyroStruct> intern = MySPSconfig;

            for (int i = 0; i < configSPS.Count; i++)
            {
                string _key = configSPS.Keys.ElementAt(i);
                SPSLogger.ActiveAccGyroStruct _val = new SPSLogger.ActiveAccGyroStruct();

                if (configSPS.TryGetValue(_key, out _val))
                {
                    if (intern.ContainsKey(_key))
                    {
                        intern.TryGetValue(_key, out _val);
                    }
                    else
                    {
                        _val.tempStabTimes = new SPSLogger.TempStabTime_t
                        {
                            isTimeActive = false,
                            isConditionsActive = false,
                            Time = (long)Conditions_def_TimeLimit,
                            Conditions_Temp = Conditions_def_Temp,
                            Conditions_Maxlimit = Conditions_def_MaxRangle,
                            Conditions_Minlimit = Conditions_def_MinRangle,
                            Conditions_TimeLimit = Conditions_def_TimeLimit,
                            Conditions_FailTime = Conditions_def_FailTime,
                            Conditions_OffsetTime = Conditions_def_Offset,
                            Stopwatch = 0d,
                            Finish = false,
                            FirstPacketTimestamp = 0,
                            MeasureTime = new System.Diagnostics.Stopwatch()
                        };
                    }
                }

                configSPS[_key] = _val;
            }
            return configSPS;
        }

        private void InitializaWnd(Dictionary<string, SPSLogger.ActiveAccGyroStruct> configSPS)
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Sensors Name");
            dt.Columns.Add("Time");
            dt.Columns.Add("is Time active");
            dt.Columns.Add("Condition Offset[s]");
            dt.Columns.Add("Conditions Active Time[s]");
            dt.Columns.Add("Conditions Temperature[°C]");
            dt.Columns.Add("Conditions Temp Max Limit[°C]");
            dt.Columns.Add("Conditions Temp Min Limit[°C]");
            dt.Columns.Add("Conditions Time to fail[s]");
            dt.Columns.Add("is Conditions active");

            for (int i = 0; i < configSPS.Count; i++)
                dt.Rows.Add();

            //AddSensorsName
            int x = 0;
            for (int y = 0; y < configSPS.Count; y++)
            {
                dt.Rows[y][x] = configSPS.Keys.ElementAt(y);
            }

            //Default time in s
            x++;
            for (int y = 0; y < configSPS.Count; y++)
            {
                dt.Rows[y][x] = Conditions_def_TimeLimit;
            }

            //Create checkbox for time
            x++;
            for (int y = 0; y < configSPS.Count; y++)
            {
                dt.Rows[y][x] = Convert.ToInt32(TimeIs);
            }

            //Condition Offset[s]
            x++;
            for (int y = 0; y < configSPS.Count; y++)
            {
                dt.Rows[y][x] = Conditions_def_Offset;
            }

            //Conditions Active Time[s]
            x++;
            for (int y = 0; y < configSPS.Count; y++)
            {
                dt.Rows[y][x] = Conditions_def_TimeLimit;
            }

            //Conditions Temperature[°C]
            x++;
            for (int y = 0; y < configSPS.Count; y++)
            {
                dt.Rows[y][x] = Conditions_def_Temp;
            }

            //Conditions Temp Max Limit[°C]
            x++;
            for (int y = 0; y < configSPS.Count; y++)
            {
                dt.Rows[y][x] = Conditions_def_MaxRangle;
            }

            //Conditions Temp Min Limit[°C]
            x++;
            for (int y = 0; y < configSPS.Count; y++)
            {
                dt.Rows[y][x] = Conditions_def_MinRangle;
            }

            //Conditions Time to fail[s]
            x++;
            for (int y = 0; y < configSPS.Count; y++)
            {
                dt.Rows[y][x] = Conditions_def_FailTime;
            }

            //is Conditions active
            x++;
            for (int y = 0; y < configSPS.Count; y++)
            {
                dt.Rows[y][x] = Convert.ToInt32(ConditionsIs);
            }

            MySPSconfig = configSPS;

            dataGridView1.DataSource = dt;
            dataGridView1.ScrollBars = ScrollBars.Both;          

            for (int y = 0; y < MySPSconfig.Count; y++)
            {
                string _key = MySPSconfig.Keys.ElementAt(y);
                SPSLogger.ActiveAccGyroStruct _val = new SPSLogger.ActiveAccGyroStruct();

                if (MySPSconfig.TryGetValue(_key, out _val))
                {
                    _val.tempStabTimes = new SPSLogger.TempStabTime_t
                        {
                            isTimeActive = false,
                            isConditionsActive = false,
                            Time = (long)Conditions_def_TimeLimit,
                            Conditions_Temp = Conditions_def_Temp,
                            Conditions_Maxlimit = Conditions_def_MaxRangle,
                            Conditions_Minlimit = Conditions_def_MinRangle,
                            Conditions_TimeLimit = Conditions_def_TimeLimit,
                            Conditions_FailTime = Conditions_def_FailTime,
                            Conditions_OffsetTime = Conditions_def_Offset,
                            Stopwatch = 0d,
                            Finish = false,
                            FirstPacketTimestamp = 0,
                            MeasureTime = new System.Diagnostics.Stopwatch()
                        };
                    MySPSconfig[_key] = _val;
                }
            }
        }

        private void ChangeEnablePartOfRow(int y, bool isTimeActive, bool isConditionsActive)
        {
            if (isTimeActive)
            {
                dataGridView1.Rows[y].Cells[1].Style.BackColor = Color.LightGreen;
                dataGridView1.Rows[y].Cells[2].Style.BackColor = Color.LightGreen;

                dataGridView1.Rows[y].Cells[3].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[4].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[5].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[6].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[7].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[8].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[9].Style.BackColor = Color.PaleVioletRed;
            }
            else if (isConditionsActive)
            {
                dataGridView1.Rows[y].Cells[1].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[2].Style.BackColor = Color.PaleVioletRed;

                dataGridView1.Rows[y].Cells[3].Style.BackColor = Color.LightGreen;
                dataGridView1.Rows[y].Cells[4].Style.BackColor = Color.LightGreen;
                dataGridView1.Rows[y].Cells[5].Style.BackColor = Color.LightGreen;
                dataGridView1.Rows[y].Cells[6].Style.BackColor = Color.LightGreen;
                dataGridView1.Rows[y].Cells[7].Style.BackColor = Color.LightGreen;
                dataGridView1.Rows[y].Cells[8].Style.BackColor = Color.LightGreen;
                dataGridView1.Rows[y].Cells[9].Style.BackColor = Color.LightGreen;
            }
            else
            {
                dataGridView1.Rows[y].Cells[1].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[2].Style.BackColor = Color.PaleVioletRed;

                dataGridView1.Rows[y].Cells[3].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[4].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[5].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[6].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[7].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[8].Style.BackColor = Color.PaleVioletRed;
                dataGridView1.Rows[y].Cells[9].Style.BackColor = Color.PaleVioletRed;
            }
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int x = e.ColumnIndex;
            int y = e.RowIndex;
            double _newval;
            DataTable dt = (DataTable)dataGridView1.DataSource;

            if(IsDigitsOnly(dt.Rows[y][x].ToString()))
            {
                   _newval = Convert.ToDouble(dt.Rows[y][x].ToString());
            }
            else
                return;

            string _key = dt.Rows[y][0].ToString();
            SPSLogger.ActiveAccGyroStruct _val = new SPSLogger.ActiveAccGyroStruct();
            if (MySPSconfig.TryGetValue(_key, out _val))
            {
                switch (x)
                {
                        //Name
                    case 0:
                        break;
                        //Time 
                    case 1:
                        _val.tempStabTimes.Time = _newval;
                        break;
                        //Active time 
                    case 2:
                        if (_newval == 1)
                        {
                            _val.tempStabTimes.isTimeActive = true;
                            ChangeEnablePartOfRow(y, true, false);
                        }
                        else
                        {
                            _val.tempStabTimes.isTimeActive = false;
                            ChangeEnablePartOfRow(y, false, Convert.ToBoolean(dt.Rows[y][9]));
                        }
                        break;
                        //Offset
                    case 3:
                        _val.tempStabTimes.Conditions_OffsetTime = _newval;
                        break;
                        //TimeLimit
                    case 4:
                        _val.tempStabTimes.Conditions_TimeLimit = _newval;
                        break;
                        //Temp 
                    case 5:
                        _val.tempStabTimes.Conditions_Temp = _newval;
                        break;
                        //MaxTemp
                    case 6:
                        _val.tempStabTimes.Conditions_Maxlimit = _newval;
                        break;
                        //Min Temp
                    case 7:
                        _val.tempStabTimes.Conditions_Minlimit = _newval;
                        break;
                        //Fail time 
                    case 8:
                        _val.tempStabTimes.Conditions_FailTime = _newval;
                        break;
                        //Active Conditions
                    case 9:
                        if (_newval == 1)
                        {
                            _val.tempStabTimes.isConditionsActive = true;
                            ChangeEnablePartOfRow(y, false, true);
                        }
                        else
                        {
                            _val.tempStabTimes.isConditionsActive = false;
                            ChangeEnablePartOfRow(y, Convert.ToBoolean(dt.Rows[y][2]), false);
                        }
                        break;
                }
            }
        }

        private void buttonSwitchTime_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridView1.DataSource;
            TimeIs = !TimeIs;

            if (TimeIs)
                ConditionsIs = false;

            for (int y = 0; y < MySPSconfig.Count; y++)
            {
                dt.Rows[y][2] = Convert.ToInt32(TimeIs);
                if(TimeIs)
                    dt.Rows[y][9] = Convert.ToInt32(!TimeIs);

                string _key = MySPSconfig.Keys.ElementAt(y);
                SPSLogger.ActiveAccGyroStruct _val = new SPSLogger.ActiveAccGyroStruct();

                if (MySPSconfig.TryGetValue(_key, out _val))
                {
                    _val.tempStabTimes.isTimeActive = TimeIs;
                    ChangeEnablePartOfRow(y, TimeIs, ConditionsIs);
                    if(TimeIs)
                        _val.tempStabTimes.isConditionsActive = !TimeIs;
                }

                MySPSconfig[_key] = _val;
            }

            dataGridView1.DataSource = dt;
        }

        private void buttonSwitchConditions_Click(object sender, EventArgs e)
        {           
            DataTable dt = (DataTable)dataGridView1.DataSource;

            ConditionsIs = !ConditionsIs;

            if (ConditionsIs)
                TimeIs = false;

            for (int y = 0; y < MySPSconfig.Count; y++)
            {
                dt.Rows[y][9] = Convert.ToInt32(ConditionsIs);
                if (ConditionsIs)
                    dt.Rows[y][2] = Convert.ToInt32(!ConditionsIs);

                string _key = MySPSconfig.Keys.ElementAt(y);
                SPSLogger.ActiveAccGyroStruct _val = new SPSLogger.ActiveAccGyroStruct();

                if (MySPSconfig.TryGetValue(_key, out _val))
                {
                    _val.tempStabTimes.isConditionsActive = ConditionsIs;
                    ChangeEnablePartOfRow(y, TimeIs, ConditionsIs);
                    if(ConditionsIs)
                        _val.tempStabTimes.isTimeActive = !ConditionsIs;
                }

                MySPSconfig[_key] = _val;
            }

            dataGridView1.DataSource = dt;
        }

        private void textBoxTime4All_KeyUp(object sender, KeyEventArgs e)
        {
            string s = ((TextBox)sender).Text;
            if (s == string.Empty)
                return;
            if (IsDigitsOnly(s))
            {
                double _newval = Convert.ToDouble(s);
                DataTable dt = (DataTable)dataGridView1.DataSource;

                for (int y = 0; y < MySPSconfig.Count; y++)
                {
                    dt.Rows[y][1] = _newval;
                    dt.Rows[y][4] = _newval;

                    string _key = MySPSconfig.Keys.ElementAt(y);
                    SPSLogger.ActiveAccGyroStruct _val = new SPSLogger.ActiveAccGyroStruct();

                    if (MySPSconfig.TryGetValue(_key, out _val))
                    {
                        _val.tempStabTimes.Time = (long)_newval;
                        _val.tempStabTimes.Conditions_TimeLimit = _newval;
                    }

                    MySPSconfig[_key] = _val;
                }

                dataGridView1.DataSource = dt;
            }
        }

        bool IsDigitsOnly(string str)
        {
            foreach (char c in str)
            {
                if ((c < '0' || c > '9') && c != '.')
                    return false;
            }

            return true;
        }

        private void textBoxTemp4All_KeyUp(object sender, KeyEventArgs e)
        {
            string s = ((TextBox)sender).Text;
            if (s == string.Empty)
                return;
            if (IsDigitsOnly(s))
            {
                double _newval = Convert.ToDouble(s);
                DataTable dt = (DataTable)dataGridView1.DataSource;

                for (int y = 0; y < MySPSconfig.Count; y++)
                {
                    dt.Rows[y][5] = _newval;

                    string _key = MySPSconfig.Keys.ElementAt(y);
                    SPSLogger.ActiveAccGyroStruct _val = new SPSLogger.ActiveAccGyroStruct();

                    if (MySPSconfig.TryGetValue(_key, out _val))
                    {
                        _val.tempStabTimes.Conditions_Temp = _newval;
                    }

                    MySPSconfig[_key] = _val;
                }

                dataGridView1.DataSource = dt;
            }
        }

        private void textBoxTempMAXlimit_KeyUp(object sender, KeyEventArgs e)
        {
            string s = ((TextBox)sender).Text;
            if (s == string.Empty)
                return;
            if (IsDigitsOnly(s))
            {
                double _newval = Convert.ToDouble(s);
                DataTable dt = (DataTable)dataGridView1.DataSource;

                for (int y = 0; y < MySPSconfig.Count; y++)
                {
                    dt.Rows[y][6] = _newval;

                    string _key = MySPSconfig.Keys.ElementAt(y);
                    SPSLogger.ActiveAccGyroStruct _val = new SPSLogger.ActiveAccGyroStruct();

                    if (MySPSconfig.TryGetValue(_key, out _val))
                    {
                        _val.tempStabTimes.Conditions_Maxlimit = _newval;
                    }

                    MySPSconfig[_key] = _val;
                }

                dataGridView1.DataSource = dt;
            }
        }

        private void textBoxTempMINlimit_KeyUp(object sender, KeyEventArgs e)
        {
            string s = ((TextBox)sender).Text;
            if (s == string.Empty)
                return;
            if (IsDigitsOnly(s))
            {
                double _newval = Convert.ToDouble(s);
                DataTable dt = (DataTable)dataGridView1.DataSource;

                for (int y = 0; y < MySPSconfig.Count; y++)
                {
                    dt.Rows[y][7] = _newval;

                    string _key = MySPSconfig.Keys.ElementAt(y);
                    SPSLogger.ActiveAccGyroStruct _val = new SPSLogger.ActiveAccGyroStruct();

                    if (MySPSconfig.TryGetValue(_key, out _val))
                    {
                        _val.tempStabTimes.Conditions_Minlimit = _newval;
                    }

                    MySPSconfig[_key] = _val;
                }

                dataGridView1.DataSource = dt;
            }
        }

        private void textBoxOffsetTime4All_KeyUp(object sender, KeyEventArgs e)
        {
            string s = ((TextBox)sender).Text;
            if (s == string.Empty)
                return;
            if (IsDigitsOnly(s))
            {
                double _newval = Convert.ToDouble(s);
                DataTable dt = (DataTable)dataGridView1.DataSource;

                for (int y = 0; y < MySPSconfig.Count; y++)
                {
                    dt.Rows[y][3] = _newval;

                    string _key = MySPSconfig.Keys.ElementAt(y);
                    SPSLogger.ActiveAccGyroStruct _val = new SPSLogger.ActiveAccGyroStruct();

                    if (MySPSconfig.TryGetValue(_key, out _val))
                    {
                        _val.tempStabTimes.Conditions_OffsetTime = _newval;
                    }

                    MySPSconfig[_key] = _val;
                }

                dataGridView1.DataSource = dt;
            }
        }

        private void textBoxFailTime4All_KeyUp(object sender, KeyEventArgs e)
        {
            string s = ((TextBox)sender).Text;
            if (s == string.Empty)
                return;
            if (IsDigitsOnly(s))
            {
                double _newval = Convert.ToDouble(s);
                DataTable dt = (DataTable)dataGridView1.DataSource;

                for (int y = 0; y < MySPSconfig.Count; y++)
                {
                    dt.Rows[y][8] = _newval;

                    string _key = MySPSconfig.Keys.ElementAt(y);
                    SPSLogger.ActiveAccGyroStruct _val = new SPSLogger.ActiveAccGyroStruct();

                    if (MySPSconfig.TryGetValue(_key, out _val))
                    {
                        _val.tempStabTimes.Conditions_FailTime = _newval;
                    }

                    MySPSconfig[_key] = _val;
                }

                dataGridView1.DataSource = dt;
            }
        }

        private void TempStabTimeWnd_Resize(object sender, EventArgs e)
        {
            this.dataGridView1.Size = new Size(this.Size.Width - 10, this.dataGridView1.Size.Height);
        }

        private void textBoxTempMAXlimit_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBoxTemp4All_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBoxTempMINlimit_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
