using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OPCTrendControl;

namespace OPCMonitoring
{
    /// <summary>
    /// Form where you edit conditions for trigering of alarms
    /// </summary>
    public partial class AlarmEditor : Form
    {
        private MainForm mf;

        /// <summary>
        /// Initializes new form for editing alarm options
        /// </summary>
        /// <param name="pensXPProcessor">Xp Processor pens</param>
        /// <param name="pensServerProcessor">Server Processor pens</param>
        /// <param name="pensXPmemory">Xp Memory pens</param>
        /// <param name="pensServerMemory">Server Memory pens</param>
        /// <param name="pensXPHardDisc">Xp Hard Disc pens</param>
        /// <param name="pensServerHardDisc">Server Hard Disc pens</param>
        /// <param name="pensXPnetwork">Xp Network pens</param>
        /// <param name="pensServerNetwork">Server Network pens</param>
        /// <param name="_mf">Reference to the main form</param>
        public AlarmEditor(List<ClassPen> pensXPProcessor, List<ClassPen> pensServerProcessor, List<ClassPen> pensXPmemory, List<ClassPen> pensServerMemory, List<ClassPen> pensXPHardDisc, List<ClassPen> pensServerHardDisc, List<ClassPen> pensXPnetwork, List<ClassPen> pensServerNetwork, MainForm _mf)
        {
            InitializeComponent();

            mf = _mf;

            int index = 0;
            foreach (ClassPen pen in pensXPProcessor)
            {
                grdXpProcessor.Rows.Add();
                grdXpProcessor.Rows[index].Cells[0].Value = pen.Description;

                grdXpProcessor.Rows[index].Cells[3].Value = pen.TagPath;
                grdXpProcessor.Rows[index].Cells[4].Value = pen.Description;

                index++;
            }

            index = 0;
            foreach (ClassPen pen in pensServerProcessor)
            {
                grdServerProcessor.Rows.Add();
                grdServerProcessor.Rows[index].Cells[0].Value = pen.Description;
                grdServerProcessor.Rows[index].Cells[3].Value = pen.TagPath;
                grdServerProcessor.Rows[index].Cells[4].Value = pen.Description;
                index++;
            }

            index = 0;
            foreach (ClassPen pen in pensXPmemory)
            {
                grdXpMemory.Rows.Add();
                grdXpMemory.Rows[index].Cells[0].Value = pen.Description;
                grdXpMemory.Rows[index].Cells[3].Value = pen.TagPath;
                grdXpMemory.Rows[index].Cells[4].Value = pen.Description;
                index++;
            }

            index = 0;
            foreach (ClassPen pen in pensServerMemory)
            {
                grdServerMemory.Rows.Add();
                grdServerMemory.Rows[index].Cells[0].Value = pen.Description;
                grdServerMemory.Rows[index].Cells[3].Value = pen.TagPath;
                grdServerMemory.Rows[index].Cells[4].Value = pen.Description;
                index++;
            }

            index = 0;
            foreach (ClassPen pen in pensXPHardDisc)
            {
                grdXpHardDisc.Rows.Add();
                grdXpHardDisc.Rows[index].Cells[0].Value = pen.Description;
                grdXpHardDisc.Rows[index].Cells[3].Value = pen.TagPath;
                grdXpHardDisc.Rows[index].Cells[4].Value = pen.Description;
                index++;
            }

            index = 0;
            foreach (ClassPen pen in pensServerHardDisc)
            {
                grdServerHardDisc.Rows.Add();
                grdServerHardDisc.Rows[index].Cells[0].Value = pen.Description;
                grdServerHardDisc.Rows[index].Cells[3].Value = pen.TagPath;
                grdServerHardDisc.Rows[index].Cells[4].Value = pen.Description;
                index++;
            }

            index = 0;
            foreach (ClassPen pen in pensXPnetwork)
            {
                grdXpNetwork.Rows.Add();
                grdXpNetwork.Rows[index].Cells[0].Value = pen.Description;
                grdXpNetwork.Rows[index].Cells[3].Value = pen.TagPath;
                grdXpNetwork.Rows[index].Cells[4].Value = pen.Description;
                index++;
            }

            index = 0;
            foreach (ClassPen pen in pensServerNetwork)
            {
                grdServerNetwork.Rows.Add();
                grdServerNetwork.Rows[index].Cells[0].Value = pen.Description;
                grdServerNetwork.Rows[index].Cells[3].Value = pen.TagPath;
                grdServerNetwork.Rows[index].Cells[4].Value = pen.Description;
                index++;
            }

            grdServerHardDisc.CellEndEdit += new DataGridViewCellEventHandler(grdCellEdit);
            grdServerMemory.CellEndEdit += new DataGridViewCellEventHandler(grdCellEdit);
            grdServerNetwork.CellEndEdit += new DataGridViewCellEventHandler(grdCellEdit);
            grdServerProcessor.CellEndEdit += new DataGridViewCellEventHandler(grdCellEdit);
            grdXpHardDisc.CellEndEdit += new DataGridViewCellEventHandler(grdCellEdit);
            grdXpMemory.CellEndEdit += new DataGridViewCellEventHandler(grdCellEdit);
            grdXpNetwork.CellEndEdit += new DataGridViewCellEventHandler(grdCellEdit);
            grdXpProcessor.CellEndEdit += new DataGridViewCellEventHandler(grdCellEdit);
        }

        private void AlarmEditor_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            List<DataGridViewRow> rows = new List<DataGridViewRow>();

            foreach (DataGridViewRow row in grdXpProcessor.Rows)
                rows.Add(row);
            foreach (DataGridViewRow row in grdXpNetwork.Rows)
                rows.Add(row);
            foreach (DataGridViewRow row in grdXpMemory.Rows)
                rows.Add(row);
            foreach (DataGridViewRow row in grdXpHardDisc.Rows)
                rows.Add(row);
            foreach (DataGridViewRow row in grdServerProcessor.Rows)
                rows.Add(row);
            foreach (DataGridViewRow row in grdServerNetwork.Rows)
                rows.Add(row);
            foreach (DataGridViewRow row in grdServerMemory.Rows)
                rows.Add(row);
            foreach (DataGridViewRow row in grdServerHardDisc.Rows)
                rows.Add(row);

            foreach (Alarm alarm in mf.Alarms)
            {
                mf.opcControlsData1.RemoveTag(alarm.Pen);
            }
            mf.Alarms.Clear();

            foreach (DataGridViewRow row in rows)
            {
                if (row.Cells[1].Value == null || row.Cells[2].Value == null || row.Cells[2].ToString() == string.Empty)
                    continue;

                try
                {
                    Convert.ToDouble(row.Cells[2].Value);
                }
                catch
                {
                    continue;
                }

                mf.Alarms.Add(new Alarm {Pen = row.Cells[3].Value.ToString(), Option=row.Cells[1].Value.ToString(), Value=Convert.ToDouble(row.Cells[2].Value), Description=row.Cells[4].Value.ToString() });
            }

            DialogResult = DialogResult.OK;
        }


        private void grdCellEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                DataGridViewCell cell = ((DataGridView)sender).Rows[e.RowIndex].Cells[2];

                if (cell.Value != null && cell.Value.ToString() != string.Empty)
                {
                    try
                    {
                        Convert.ToDouble(cell.Value);
                        cell.OwningRow.ErrorText = string.Empty;
                    }
                    catch
                    {
                        cell.OwningRow.ErrorText = "Value must be of type double";
                        cell.Value = string.Empty;
                    }
                }
            }
        }
    }
}
