﻿using System.Text;

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

        public Alarm()
        {
            InitializeComponent();
        }

        // コール元からIDを取得
        public void SetId(short id)
        {
            m_Id = id;
        }

        // ダイアログの初期化
        private void Alarm_Load(object sender, System.EventArgs e)
        {
            txtChannel.Text = "0";
            txtUU.Text      = "0";
            txtUL.Text      = "0";
            txtLU.Text      = "0";
            txtLL.Text      = "0";

            // チャネル番号を0に設定
            cboAlarm.SelectedIndex = 0;
            btnGet_Click(btnGet, new System.EventArgs());
        }

        // [読み込み]ボタンクリック
        private void btnGet_Click(object sender, System.EventArgs e)
        {
            int   ret;
            short enable;
            int   uu, ul, lu, ll;
            short chno;

            // アラームの設定状態を取得
            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;
            }

            // 設定状態をダイアログに反映
            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);
        }

        // [設定]ボタンクリック
        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;

            // ダイアログの設定内容を取得
            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);

            // アラームの設定
            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;
            }
        }

        // [閉じる]ボタンクリック
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        // エラー処理
        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);
        }
    }
}
