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

namespace TheDarkWraith.WM16MEV
{
    public partial class ViewAlarms : Form
    {
        #region Variables
        private bool viewing = false;
        private SpPerfChart.PerfChart pc;
        #endregion

        #region Constructor
        public ViewAlarms(bool viewingpassed, SpPerfChart.PerfChart pcpassed)
        {
            InitializeComponent();

            viewing = viewingpassed;
            pc = pcpassed;

            this.Text += " for " + (String)pc.Tag;

            if (viewing)
            {
                toolTip1.SetToolTip(lbAlarms, toolTip1.GetToolTip(lbAlarms) + "view");
            }
            else
            {
                toolTip1.SetToolTip(lbAlarms, toolTip1.GetToolTip(lbAlarms) + "delete");
            }

            if (pc != null)
            {
                foreach (SpPerfChart.PerfChart.AlarmSetpoint a in pc.AlarmSetpoints)
                {
                    lbAlarms.Items.Add((String)pc.Tag + " - " + a.Message);
                }
            }

            lbAlarms.MouseDoubleClick += new MouseEventHandler(lbAlarms_MouseDoubleClick);
            lbAlarms.SelectedIndex = -1;
        }
        #endregion

        #region lbAlarms_MouseDoubleClick
        void lbAlarms_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lbAlarms.SelectedIndex < 0)
            {
                return;
            }

            if (viewing)
            {
                AddViewAlarm ava = null;

                try
                {
                    ava = new AddViewAlarm(pc.AlarmSetpoints[lbAlarms.SelectedIndex], (String)pc.Tag);
                    ava.ShowDialog();
                }
                finally
                {
                    if (ava != null)
                    {
                        ava.Dispose();
                        ava = null;
                    }
                }
            }
            else
            {
                DialogResult dr = MessageBox.Show("Delete the alarm " + (String)lbAlarms.SelectedItem, "Confirmation", MessageBoxButtons.YesNo);
                if (dr != DialogResult.Yes)
                {
                    return;
                }

                pc.RemoveAlarm(pc.AlarmSetpoints[lbAlarms.SelectedIndex]);
                lbAlarms.Items.RemoveAt(lbAlarms.SelectedIndex);
            }
        }
        #endregion

        #region Destroy
        public void Destroy()
        {
            pc = null;
        }
        #endregion
    }
}
