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 Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            int count;

            Form1.Instance.ErrorChk(LensCtrl.Instance.ProtocolVersion(out string version), Form1.MSGBOX);
            ProtocolTxt.Text = version;
            
            Form1.Instance.ErrorChk(LensCtrl.Instance.FWVersion(out version), Form1.MSGBOX);
            FWVersionTxt.Text = version;
            
            Form1.Instance.ErrorChk(LensCtrl.Instance.LensRevision(out int rivision), Form1.MSGBOX);
            LensRevisionTxt.Text = rivision.ToString();
            
            Form1.Instance.ErrorChk(LensCtrl.Instance.LensAddress(out int i2cAddr), Form1.MSGBOX);
            LensAddrTxt.Text = "0x" + i2cAddr.ToString("x") + " (7bit)";
            
            Form1.Instance.ErrorChk(LensCtrl.Instance.ModelName(out string userName), Form1.MSGBOX);
            ModelNameTxt.Text = userName;
            
            Form1.Instance.ErrorChk(LensCtrl.Instance.CapabilitiesRead(out ushort capabilities), Form1.MSGBOX);
            CapabilitiesTxt.Text = Separating4digits(capabilities);
            
            Form1.Instance.ErrorChk(LensCtrl.Instance.Status1Read(out ushort status1), Form1.MSGBOX);
            Status1Txt.Text = Separating4digits(status1);
            
            Form1.Instance.ErrorChk(LensCtrl.Instance.Status2ReadSet(), Form1.MSGBOX);
            Status2Txt.Text = Separating4digits(LensCtrl.Instance.status2);

            if (Form1.Instance.withZoom == true)
            {
                zPosValTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.ZOOM_POSITION_VAL).ToString();
                ZBCTxt.Text = OnOff(LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.ZOOM_BACKLASH_CANCEL));
                zInitPosTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.ZOOM_INIT_POSITION).ToString();
                zMechMinTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.ZOOM_MECH_STEP_MIN).ToString();
                zMechMaxTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.ZOOM_MECH_STEP_MAX).ToString();
                zPosMinTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.ZOOM_POSITION_MIN).ToString();
                zPosMaxTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.ZOOM_POSITION_MAX).ToString();
                zSpeedValTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.ZOOM_SPEED_VAL).ToString();
                zSpeedMinTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.ZOOM_SPEED_MIN).ToString();
                zSpeedMaxTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.ZOOM_SPEED_MAX).ToString();
                Form1.Instance.ErrorChk(LensCtrl.Instance.ZoomCountValRead(out count), Form1.MSGBOX);
                zCountValTxt.Text = count.ToString();
                Form1.Instance.ErrorChk(LensCtrl.Instance.ZoomCountMaxRead(out count), Form1.MSGBOX);
                zCountMaxTxt.Text = count.ToString();
            }
            if (Form1.Instance.withFocus == true)
            {
                fPosValTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.FOCUS_POSITION_VAL).ToString();
                FBCTxt.Text = OnOff(LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.FOCUS_BACKLASH_CANCEL));
                fInitPosTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.FOCUS_INIT_POSITION).ToString();
                fMechMinTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.FOCUS_MECH_STEP_MIN).ToString();
                fMechMaxTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.FOCUS_MECH_STEP_MAX).ToString();
                fPosMinTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.FOCUS_POSITION_MIN).ToString();
                fPosMaxTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.FOCUS_POSITION_MAX).ToString();
                fSpeedValTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.FOCUS_SPEED_VAL).ToString();
                fSpeedMinTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.FOCUS_SPEED_MIN).ToString();
                fSpeedMaxTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.FOCUS_SPEED_MAX).ToString();
                Form1.Instance.ErrorChk(LensCtrl.Instance.FocusCountValRead(out count), Form1.MSGBOX);
                fCountValTxt.Text = count.ToString();
                Form1.Instance.ErrorChk(LensCtrl.Instance.FocusCountMaxRead(out count), Form1.MSGBOX);
                fCountMaxTxt.Text = count.ToString();
            }

            if (Form1.Instance.withIris == true)
            {
                iPosValTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.IRIS_POSITION_VAL).ToString();
                IBCTxt.Text = OnOff(LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.IRIS_BACKLASH_CANCEL));
                iInitPosTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.IRIS_INIT_POSITION).ToString();
                iMechMinTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.IRIS_MECH_STEP_MIN).ToString();
                iMechMaxTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.IRIS_MECH_STEP_MAX).ToString();
                iPosMinTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.IRIS_POSITION_MIN).ToString();
                iPosMaxTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.IRIS_POSITION_MAX).ToString();
                iSpeedValTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.IRIS_SPEED_VAL).ToString();
                iSpeedMinTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.IRIS_SPEED_MIN).ToString();
                iSpeedMaxTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.IRIS_SPEED_MAX).ToString();
                Form1.Instance.ErrorChk(LensCtrl.Instance.IrisCountValRead(out count), Form1.MSGBOX);
                iCountValTxt.Text = count.ToString();
                Form1.Instance.ErrorChk(LensCtrl.Instance.IrisCountMaxRead(out count), Form1.MSGBOX);
                iCountMaxTxt.Text = count.ToString();
            }
            if (Form1.Instance.withOptFil == true)
            {
                oPosValTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.OPT_FILTER_POSITION_VAL).ToString();
                oInitPosTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.OPT_FILTER_INIT_POSITION).ToString();
                oMechMinTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.OPT_FILTER_MECH_STEP_MIN).ToString();
                oMechMaxTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.OPT_FILTER_MECH_STEP_MAX).ToString();
                oPosMinTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.OPT_FILTER_POSITION_MIN).ToString();
                oPosMaxTxt.Text = LensCtrl.Instance.NoErrChk2BytesRead(DevAddr.OPT_FILTER_POSITION_MAX).ToString();
                Form1.Instance.ErrorChk(LensCtrl.Instance.OptFilterCountValRead(out count), Form1.MSGBOX);
                oCountValTxt.Text = count.ToString();
                Form1.Instance.ErrorChk(LensCtrl.Instance.OptFilterCountMaxRead(out count), Form1.MSGBOX);
                oCountMaxTxt.Text = count.ToString();
            }
        }
        private string Separating4digits(ushort value)
        {
            string rcvString = Convert.ToString(value, 2).PadLeft(16, '0');
            for (int i = 4; i < rcvString.Length; i += 5)
            {
                rcvString = rcvString.Insert(i, " ");
            }
            return rcvString;
        }
        private string OnOff(ushort flag)
        {
            if (flag == 1)
                return "ON";
            else
                return "OFF";
        }
        private void Form3Close_btn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
