﻿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 Measure : Form
    {
        short m_Id;

        public Measure()
        {
            InitializeComponent();
        }

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

        // Initialize dialog
        private void Measure_Load(object sender, System.EventArgs e)
        {
            int ret;
            short linearize, averagetype;
            int updatetiming, averagedata;
            int channeldisable;

            linearize = 0;
            updatetiming = 0;
            averagetype = 0;
            averagedata = 0;
            txtAvCondition.Text = "0";
            // Get the linearize function
            ret = Ccappt.PtGetLinearise(m_Id, ref linearize);
            if (ret != 0)
            {
                ErrorHandling("PtGetLinearise", ret);
                return;
            }
            cboLinearize.SelectedIndex = linearize;

            // Get the update timing
            ret = Ccappt.PtGetUpdateTiming(m_Id, ref updatetiming);
            if (ret != 0)
            {
                ErrorHandling("PtGetUpdateTiming", ret);
                return;
            }
            switch (updatetiming)
            {
                case Ccappt.SPEED_150MS:
                    cboUpdate.SelectedIndex = 0;
                    break;
                case Ccappt.SPEED_40MS:
                    cboUpdate.SelectedIndex = 1;
                    break;
                case Ccappt.SPEED_5MS:
                    cboUpdate.SelectedIndex = 2;
                    break;
                case Ccappt.SPEED_1P5MS:
                    cboUpdate.SelectedIndex = 3;
                    break;
            }

            // Get the average function
            ret = Ccappt.PtGetAverage(m_Id, ref averagetype, ref averagedata);
            if (ret != 0)
            {
                ErrorHandling("PtGetAverage", ret);
                return;
            }
            cboAverage.SelectedIndex = averagetype;
            txtAvCondition.Text = string.Format("{0:d}", averagedata);

            // Get the conversion disabled channels
            channeldisable = 0;
            ret = Ccappt.PtGetChannelDisable(m_Id, ref channeldisable);
            if (ret != 0)
            {
                ErrorHandling("PtGetChannelDisable", ret);
                return;
            }
            txtDisable.Text = string.Format("{0:X}", channeldisable);
        }

        // Click the [OK] button
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            int ret;
            int index;
            int updatetiming;
            int data;
            string szText;
            int channeldisable;

            // Set the linearize function
            index = cboLinearize.SelectedIndex;
            ret = Ccappt.PtSetLinearise(m_Id, (short)index);
            if (ret != 0)
            {
                ErrorHandling("PtSetLinearise", ret);
            }

            // Set the update timing
            index = cboUpdate.SelectedIndex;
            switch (index)
            {
                case 0:
                    updatetiming = Ccappt.SPEED_150MS;
                    break;
                case 1:
                    updatetiming = Ccappt.SPEED_40MS;
                    break;
                case 2:
                    updatetiming = Ccappt.SPEED_5MS;
                    break;
                case 3:
                    updatetiming = Ccappt.SPEED_1P5MS;
                    break;
                default:
                    updatetiming = Ccappt.SPEED_150MS;
                    break;
            }
            ret = Ccappt.PtSetUpdateTiming(m_Id, updatetiming);
            if (ret != 0)
            {
                ErrorHandling("PtSetUpdateTiming", ret);
            }

            // Set the average function
            index = cboAverage.SelectedIndex;
            try
            {
                data = Convert.ToInt32(txtAvCondition.Text);
            }
            catch (System.FormatException)
            {
                data = 0;
            }
            ret = Ccappt.PtSetAverage(m_Id, (short)index, data);
            if (ret != 0)
            {
                ErrorHandling("PtSetAverage", ret);
            }

            // Set the conversion disabled channels
            szText = txtDisable.Text;
            try
            {
                channeldisable = int.Parse(szText, System.Globalization.NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                channeldisable = 0;
            }
            ret = Ccappt.PtSetChannelDisable(m_Id, channeldisable);
            if (ret != 0)
            {
                ErrorHandling("PtSetChannelDisable", ret);
            }

            this.Close();
        }

        // Click the [Cancel] button
        private void btnCancel_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);
        }
    }
}
