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 AddViewAlarm : Form
    {
        #region Variables
        private SpPerfChart.PerfChart.AlarmSetpoint alarmsetpoint = null;
        #endregion

        #region Properties
        #region AlarmType
        public SpPerfChart.PerfChart.AlarmSetpoint.enumAlarmType AlarmType
        {
            get
            {
                return (SpPerfChart.PerfChart.AlarmSetpoint.enumAlarmType)cbAlarmType.SelectedIndex; 
            }
        }
        #endregion

        #region Setpoint
        public decimal Setpoint
        {
            get
            {
                return nudSetpoint.Value;
            }
        }
        #endregion

        #region Hysteresis
        public decimal Hystersis
        {
            get
            {
                return nudHysteresis.Value;
            }
        }
        #endregion

        #region AlarmTime
        public int AlarmTime
        {
            get
            {
                return (int)nudAlarmTime.Value;
            }
        }
        #endregion

        #region Rearm
        public bool Rearm
        {
            get
            {
                return cbRearm.Checked;
            }
        }
        #endregion

        #region Sound
        public String Sound
        {
            get
            {
                if (tbSound.Text == "")
                {
                    return null;
                }
                return tbSound.Text;
            }
        }
        #endregion

        #region Message
        public String Message
        {
            get
            {
                if (tbMessage.Text == "")
                {
                    return null;
                }
                return tbMessage.Text;
            }
        }
        #endregion

        #region RenderSetpoint
        public bool RenderSetpoint
        {
            get
            {
                return cbRender.Checked;
            }
        }
        #endregion
        #endregion

        #region Constructor
        public AddViewAlarm(SpPerfChart.PerfChart.AlarmSetpoint aspassed, String pcname)
        {
            InitializeComponent();

            alarmsetpoint = aspassed;

            // initialize the alarm type combo box
            foreach (String name in Enum.GetNames(typeof(SpPerfChart.PerfChart.AlarmSetpoint.enumAlarmType)))
            {
                cbAlarmType.Items.Add(name);
            }

            if (aspassed == null)
            {
                this.Text = "Create new alarm for " + pcname;

                cbAlarmType.SelectedIndex = 0;
                nudSetpoint.Value = 0;
                nudHysteresis.Value = 0;
                toolTip1.SetToolTip(tbSound, toolTip1.GetToolTip(tbSound) + "\r\n\r\nDouble click to set");
                tbSound.DoubleClick += new EventHandler(tbSound_DoubleClick);
                cbRearm.Checked = false;
                cbRender.Checked = false;
            }
            else
            {
                this.Text = "View alarm in " + pcname;

                cbAlarmType.SelectedIndex = Convert.ToInt32(aspassed.AlarmType);
                nudSetpoint.Value = aspassed.Setpoint;
                nudHysteresis.Value = aspassed.Hysteresis;
                tbSound.Text = aspassed.Sound;
                tbMessage.Text = aspassed.Message;
                nudAlarmTime.Value = aspassed.AlarmTime;
                cbRearm.Checked = aspassed.ReArm;
                cbRender.Checked = aspassed.RenderSetpoint;
            }

            cbAlarmType.Enabled = (aspassed == null);
            nudSetpoint.ReadOnly = (aspassed != null);
            nudHysteresis.ReadOnly = (aspassed != null);
            tbMessage.ReadOnly = (aspassed != null);
            nudAlarmTime.ReadOnly = (aspassed != null);
            cbRearm.Enabled = (aspassed == null);
            cbRender.Enabled = true;
        }
        #endregion

        #region tbSound_DoubleClick
        void tbSound_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog ofd = null;

            try
            {
                ofd = new OpenFileDialog();
                ofd.AddExtension = true;
                ofd.AutoUpgradeEnabled = false;
                ofd.CheckFileExists = false;
                ofd.CheckPathExists = false;
                ofd.DefaultExt = "wav";
                ofd.Filter = "WAV files|*.wav";
                ofd.Multiselect = false;
                ofd.ReadOnlyChecked = false;
                ofd.RestoreDirectory = true;
                ofd.ShowHelp = false;
                ofd.ShowReadOnly = false;
                ofd.SupportMultiDottedExtensions = false;
                ofd.Title = "Select sound file to play when alarm is triggered";
                ofd.ValidateNames = true;
                ofd.InitialDirectory = WM16MEV.soundfolderpath;
                DialogResult dr = ofd.ShowDialog();
                if (dr != DialogResult.OK || ofd.FileName == null || ofd.FileName == "")
                {
                    return;
                }
                tbSound.Text = ofd.FileName;
            }
            finally
            {
                if (ofd != null)
                {
                    ofd.Dispose();
                    ofd = null;
                }
            }
        }
        #endregion

        #region cbAlarmType_SelectedIndexChanged
        private void cbAlarmType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((SpPerfChart.PerfChart.AlarmSetpoint.enumAlarmType)cbAlarmType.SelectedIndex == SpPerfChart.PerfChart.AlarmSetpoint.enumAlarmType.high)
            {
                toolTip1.SetToolTip(nudAlarmTime, "The time (in ms) that value has to be >= alarm setpoint in order to trigger the alarm");
            }
            else
            {
                toolTip1.SetToolTip(nudAlarmTime, "The time (in ms) that value has to be <= alarm setpoint in order to trigger the alarm");
            }
        }
        #endregion

        #region btnAccept_Click
        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (alarmsetpoint != null)
            {
                alarmsetpoint.RenderSetpoint = cbRender.Checked;
            }

            this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region btnCancel_Click
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
        #endregion

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