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 LensConnect_Controller
{
    public partial class Form2 : Form
    {
        public ushort zoomSpeedMin, zoomSpeedMax, focusSpeedMin, focusSpeedMax, irisSpeedMin, irisSpeedMax;
        private ushort zoomBacklashFlag, focusBacklashFlag, irisBacklashFlag;
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            Form1.Instance.ErrorChk(LensCtrl.Instance.ZoomBacklashRead(out zoomBacklashFlag), Form1.MSGBOX);
            Form1.Instance.ErrorChk(LensCtrl.Instance.FocusBacklashRead(out focusBacklashFlag), Form1.MSGBOX);
            Form1.Instance.ErrorChk(LensCtrl.Instance.IrisBacklashRead(out irisBacklashFlag), Form1.MSGBOX);

            LensCtrl.Instance.ZoomSpeedMinRead(out zoomSpeedMin);
            LensCtrl.Instance.ZoomSpeedMaxRead(out zoomSpeedMax);
            LensCtrl.Instance.FocusSpeedMinRead(out focusSpeedMin);
            LensCtrl.Instance.FocusSpeedMaxRead(out focusSpeedMax);
            LensCtrl.Instance.IrisSpeedMinRead(out irisSpeedMin);
            LensCtrl.Instance.IrisSpeedMaxRead(out irisSpeedMax);

            if (Form1.Instance.withZoom == true)
            {
                ZoomSpeedVal.Enabled = true;
                ZoomSpeedVal.Text = LensCtrl.Instance.zoomSpeedPPS.ToString();
                ZoomSpeedMinTxt.Text =zoomSpeedMin.ToString();
                ZoomSpeedMaxTxt.Text = zoomSpeedMax.ToString();
                ZBCComboBox.Enabled = true;
                ZBCComboBox.Items.Clear();
                ZBCComboBox.Items.Add("OFF");
                ZBCComboBox.Items.Add("ON");
                ZBCComboBox.SelectedIndex = zoomBacklashFlag;       // 1:ON, 2:OFF
            }
            if (Form1.Instance.withFocus == true)
            {
                FocusSpeedVal.Enabled = true;
                FocusSpeedVal.Text = LensCtrl.Instance.focusSpeedPPS.ToString();
                FocusSpeedMinTxt.Text = focusSpeedMin.ToString();
                FocusSpeedMaxTxt.Text = focusSpeedMax.ToString();
                FBCComboBox.Enabled = true;
                FBCComboBox.Items.Clear();
                FBCComboBox.Items.Add("OFF");
                FBCComboBox.Items.Add("ON");
                FBCComboBox.SelectedIndex = focusBacklashFlag;       // 1:ON, 2:OFF
            }
            if (Form1.Instance.withIris == true)
            {
                IrisSpeedVal.Enabled = true;
                IrisSpeedVal.Text = LensCtrl.Instance.irisSpeedPPS.ToString();
                IrisSpeedMinTxt.Text = irisSpeedMin.ToString();
                IrisSpeedMaxTxt.Text = irisSpeedMax.ToString();
                IBCComboBox.Enabled = true;
                IBCComboBox.Items.Clear();
                IBCComboBox.Items.Add("OFF");
                IBCComboBox.Items.Add("ON");
                IBCComboBox.SelectedIndex = irisBacklashFlag;       // 1:ON, 2:OFF
            }

        }
        private void Form2_OKbtn_Click(object sender, EventArgs e)
        {
            bool okFlag = true;
            ushort zoomSpeedValNum = 0, focusSpeedValNum = 0, irisSpeedValNum = 0;
            if (Form1.Instance.withZoom == true)
            {
                if (ZoomSpeedVal.Text != "")
                {
                    zoomSpeedValNum = (ushort)Convert.ToInt32(ZoomSpeedVal.Text);
                    if (zoomSpeedValNum > zoomSpeedMax | zoomSpeedMin > zoomSpeedValNum)
                    {
                        MessageBox.Show("The zoom speed value is out of range.");
                        okFlag = false;
                    }
                }
                else
                {
                    MessageBox.Show("No zoom speed value.");
                    okFlag = false;
                }
            }
            if(Form1.Instance.withFocus == true)
            {
                if (FocusSpeedVal.Text != "")
                {
                    focusSpeedValNum = (ushort)Convert.ToInt32(FocusSpeedVal.Text);
                    if (focusSpeedValNum > focusSpeedMax | focusSpeedMin > focusSpeedValNum)
                    {
                        MessageBox.Show("The focus speed value is out of range.");
                        okFlag = false;
                    }
                }
                else
                {
                    MessageBox.Show("No focus speed value.");
                    okFlag = false;
                }
            }
            if (Form1.Instance.withIris == true)
            {
                if (IrisSpeedVal.Text != "")
                {
                    irisSpeedValNum = (ushort)Convert.ToInt32(IrisSpeedVal.Text);
                    if (irisSpeedValNum > irisSpeedMax | irisSpeedMin > irisSpeedValNum)
                    {
                        MessageBox.Show("The iris speed value is out of range.");
                        okFlag = false;
                    }
                }
                else
                {
                    MessageBox.Show("No iris speed value.");
                    okFlag = false;
                }
            }
            zoomBacklashFlag = (ushort)ZBCComboBox.SelectedIndex;
            focusBacklashFlag = (ushort)FBCComboBox.SelectedIndex;
            irisBacklashFlag = (ushort)IBCComboBox.SelectedIndex;

            if (okFlag == true)
            {
                if (Form1.Instance.withZoom == true)
                {
                    LensCtrl.Instance.ZoomSpeedWrite(zoomSpeedValNum);
                    LensCtrl.Instance.ZoomBacklashWrite(zoomBacklashFlag);
                }
                if (Form1.Instance.withFocus ==true)
                {
                    LensCtrl.Instance.FocusSpeedWrite(focusSpeedValNum);
                    LensCtrl.Instance.FocusBacklashWrite(focusBacklashFlag);
                }
                if (Form1.Instance.withIris == true)
                {
                    LensCtrl.Instance.IrisSpeedWrite(irisSpeedValNum);
                    LensCtrl.Instance.IrisBacklashWrite(irisBacklashFlag);
                }
                this.Close();
            }
        }
        private void Form2_Cancelbtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
