﻿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;

namespace Pti4
{
    public partial class Alarm : Form
    {
        short m_Id;

        public Alarm()
        {
            InitializeComponent();
        }

        // Get the caller ID
        public void SetId(short id)
        {
            m_Id = id;
        }

        // Initialize dialog
        private void Alarm_Load(object sender, System.EventArgs e)
        {
            txtChannel.Text = "0";
            txtUU.Text = "0";
            txtUL.Text = "0";
            txtLU.Text = "0";
            txtLL.Text = "0";

            // Set the channel number to 0
            cboAlarm.SelectedIndex = 0;
            btnGet_Click(btnGet, new System.EventArgs());
        }

        // Click the [Read] button
        private void btnGet_Click(object sender, System.EventArgs e)
        {
            int ret;
            short enable;
            int uu, ul, lu, ll;
            short chno;

            // Get the alarm setting status
            enable = 0;
            uu = 0;
            ul = 0;
            lu = 0;
            ll = 0;
            try
            {
                chno = Convert.ToInt16(txtChannel.Text);
            }
            catch (System.FormatException)
            {
                chno = 0;
            }
            ret = Ccappt.PtGetAlarm(m_Id, chno, ref enable, ref uu, ref ul, ref lu, ref ll);
            if (ret != 0)
            {
                ErrorHandling("PtGetAlarm", ret);
                return;
            }

            // Reflect the setting status in dialog
            cboAlarm.SelectedIndex = enable;
            txtUU.Text = string.Format("{0:f}", uu / 100.0);
            txtUL.Text = string.Format("{0:f}", ul / 100.0);
            txtLU.Text = string.Format("{0:f}", lu / 100.0);
            txtLL.Text = string.Format("{0:f}", ll / 100.0);
        }

        // Click the [Set] button
        private void btnSet_Click(object sender, System.EventArgs e)
        {
            int ret;
            short enable;
            int uu, ul, lu, ll;
            double uu1, ul1, lu1, ll1;
            short chno;

            // Get the setting in the dialog
            enable = (short)cboAlarm.SelectedIndex;
            try
            {
                uu1 = Convert.ToDouble(txtUU.Text);
                ul1 = Convert.ToDouble(txtUL.Text);
                lu1 = Convert.ToDouble(txtLU.Text);
                ll1 = Convert.ToDouble(txtLL.Text);
            }
            catch (System.FormatException)
            {
                uu1 = 0;
                ul1 = 0;
                lu1 = 0;
                ll1 = 0;
            }
            uu = Convert.ToInt32(uu1 * 100);
            ul = Convert.ToInt32(ul1 * 100);
            lu = Convert.ToInt32(lu1 * 100);
            ll = Convert.ToInt32(ll1 * 100);

            // Set alarm
            try
            {
                chno = Convert.ToInt16(txtChannel.Text);
            }
            catch (System.FormatException)
            {
                chno = 0;
            }
            ret = Ccappt.PtSetAlarm(m_Id, chno, enable, uu, ul, lu, ll);
            if (ret != 0)
            {
                ErrorHandling("PtSetAlarm", ret);
                return;
            }
        }

        // Click the [Close] button
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        // Process error
        private void ErrorHandling(string Dfunction, int ret)
        {
            StringBuilder errstr = new StringBuilder(256);
            string str = new string(' ', 256);
            Ccappt.PtGetErrorString(ret, errstr);
            str = string.Format("{0}", errstr);
            MessageBox.Show(str, Dfunction, MessageBoxButtons.OK);
        }
    }
}
