using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FIT;

namespace FitCalc
{
    public partial class IDD_FITCALC_DIALOG : Form
    {

        #region Construction

        public IDD_FITCALC_DIALOG()
        {
            InitializeComponent();
        }

        #endregion

        #region Global variables

        static public ushort hHandle;       //FIT_Open Handle

        #endregion

        #region Event

        #region Forms load

        private void IDD_FITCALC_DIALOG_Load(object sender, EventArgs e)
        {
            IP_ADDRESS1.Text = "172";
            IP_ADDRESS2.Text = "17";
            IP_ADDRESS3.Text = "8";
            IP_ADDRESS4.Text = "151";

            Calc_Address();
        }

        #endregion

        #region GroupId Changed

        private void IDC_COMBO_GROUP_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            Calc_Address();
        }

        #endregion

        #region UnitId Changed

        private void IDC_COMBO_UNIT_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            Calc_Address();
        }

        #endregion

        #region DeviceId Changed

        private void IDC_COMBO_DEVICE_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            Calc_Address();
        }

        #endregion

        #region Address space Changed

        private void IDC_COMBO_AREA_SelectedIndexChanged(object sender, EventArgs e)
        {
            Calc_Address();
        }

        #endregion

        #region Open
        private void IDC_BUTTON_OPEN_Click(object sender, EventArgs e)
        {

            #region Get the IP address from the screen.
            byte[] IPAddress = { Convert.ToByte(IP_ADDRESS1.Text)
                , Convert.ToByte(IP_ADDRESS2.Text)
                , Convert.ToByte(IP_ADDRESS3.Text)
                , Convert.ToByte(IP_ADDRESS4.Text) };
            #endregion

            #region FIT_Open
            hHandle = Fit.FIT_Open(IPAddress, 0x5007, null);
            #endregion

            #region Error check
            if (hHandle == 0)
            {
                IDC_EDIT_RET.Text = "FIT_Open:Error";
                return;
            }
            #endregion

            #region Successed
            IDC_EDIT_RET.Text = "FIT_Open:Successed";
            IDC_BUTTON_OPEN.Enabled = false;
            IDC_BUTTON_CLOSE.Enabled = true;
            IDC_BUTTON_INPUT.Enabled = true;
            IDC_BUTTON_OUTPUT.Enabled = true;
            #endregion
        }
        #endregion

        #region Close
        private void IDC_BUTTON_CLOSE_Click(object sender, EventArgs e)
        {
            ushort Ret;     //Return code

            #region FIT_Close
            Ret = Fit.FIT_Close(hHandle);
            #endregion

            #region Error check
            if (Ret != 0)
            {
                IDC_EDIT_RET.Text = "FIT_Close:Failed(" + Ret.ToString("X") + ")";
                return;
            }
            #endregion

            #region Successed
            IDC_EDIT_RET.Text = "FIT_Close:Successed";
            IDC_BUTTON_OPEN.Enabled = true;
            IDC_BUTTON_CLOSE.Enabled = false;
            IDC_BUTTON_INPUT.Enabled = false;
            IDC_BUTTON_OUTPUT.Enabled = false;
            #endregion
        }
        #endregion

        #region Exit
        private void IDC_BUTTON_EXIT_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region input
        private void IDC_BUTTON_INPUT_Click(object sender, EventArgs e)
        {
            #region Variable declaration
            uint VAddress;      //Virtual Address
            ushort nSize;       //Size
            ushort Ret;         //Return code
            byte[] Data = new byte[16];
            char[] szText = new char[100];
            #endregion

            #region Get Virtual Address
            VAddress = Convert.ToUInt32(IDC_EDIT_ADDRESS.Text, 16);
            #endregion

            #region Call FIT_Read
            nSize = 16;
            Ret = Fit.FIT_Read(hHandle, VAddress, nSize, Data);
            #endregion

            #region Error check
            if (Ret != 0)
            {
                IDC_EDIT_RET.Text = "FIT_Read:Failed(" + Ret.ToString("X") + ")";
                return;
            }
            #endregion

            #region When successed, show the result in HEX
            IDC_EDIT_DATA0.Text = string.Format("{0:X2}", Data[0]);
            IDC_EDIT_DATA1.Text = string.Format("{0:X2}", Data[1]);
            IDC_EDIT_DATA2.Text = string.Format("{0:X2}", Data[2]);
            IDC_EDIT_DATA3.Text = string.Format("{0:X2}", Data[3]);
            IDC_EDIT_DATA4.Text = string.Format("{0:X2}", Data[4]);
            IDC_EDIT_DATA5.Text = string.Format("{0:X2}", Data[5]);
            IDC_EDIT_DATA6.Text = string.Format("{0:X2}", Data[6]);
            IDC_EDIT_DATA7.Text = string.Format("{0:X2}", Data[7]);
            IDC_EDIT_DATA8.Text = string.Format("{0:X2}", Data[8]);
            IDC_EDIT_DATA9.Text = string.Format("{0:X2}", Data[9]);
            IDC_EDIT_DATA10.Text = string.Format("{0:X2}", Data[10]);
            IDC_EDIT_DATA11.Text = string.Format("{0:X2}", Data[11]);
            IDC_EDIT_DATA12.Text = string.Format("{0:X2}", Data[12]);
            IDC_EDIT_DATA13.Text = string.Format("{0:X2}", Data[13]);
            IDC_EDIT_DATA14.Text = string.Format("{0:X2}", Data[14]);
            IDC_EDIT_DATA15.Text = string.Format("{0:X2}", Data[15]);


            IDC_EDIT_RET.Text = "FIT_Read:Successed";
            #endregion
        }
        #endregion

        #region Output
        private void IDC_BUTTON_OUTPUT_Click(object sender, EventArgs e)
        {
            #region Variable declaration
            uint VAddress;            //Virtual Address
            ushort nSize;               //Size
            byte[] Data = new byte[16]; //Data
            ushort Ret;                 //Return code
            #endregion

            #region Get Virtual Address
            VAddress = Convert.ToUInt32(IDC_EDIT_ADDRESS.Text, 16);
            #endregion

            #region Get Data
            Data[0] = Convert.ToByte(IDC_EDIT_DATA0.Text, 16);
            Data[1] = Convert.ToByte(IDC_EDIT_DATA1.Text, 16);
            Data[2] = Convert.ToByte(IDC_EDIT_DATA2.Text, 16);
            Data[3] = Convert.ToByte(IDC_EDIT_DATA3.Text, 16);
            Data[4] = Convert.ToByte(IDC_EDIT_DATA4.Text, 16);
            Data[5] = Convert.ToByte(IDC_EDIT_DATA5.Text, 16);
            Data[6] = Convert.ToByte(IDC_EDIT_DATA6.Text, 16);
            Data[7] = Convert.ToByte(IDC_EDIT_DATA7.Text, 16);
            Data[8] = Convert.ToByte(IDC_EDIT_DATA8.Text, 16);
            Data[9] = Convert.ToByte(IDC_EDIT_DATA9.Text, 16);
            Data[10] = Convert.ToByte(IDC_EDIT_DATA10.Text, 16);
            Data[11] = Convert.ToByte(IDC_EDIT_DATA11.Text, 16);
            Data[12] = Convert.ToByte(IDC_EDIT_DATA12.Text, 16);
            Data[13] = Convert.ToByte(IDC_EDIT_DATA13.Text, 16);
            Data[14] = Convert.ToByte(IDC_EDIT_DATA14.Text, 16);
            Data[15] = Convert.ToByte(IDC_EDIT_DATA15.Text, 16);
            #endregion

            #region Call FIT_Write
            nSize = 16;
            Ret = Fit.FIT_Write(hHandle, VAddress, nSize, Data);
            #endregion

            #region Error check
            if (Ret != 0)
            {
                IDC_EDIT_RET.Text = "FIT_Write:Failed(" + Ret.ToString("X") + ")";
                return;
            }
            #endregion

            #region Successed
            IDC_EDIT_RET.Text = "FIT_Write:Successed";
            #endregion
        }
        #endregion

        #endregion

        #region Method

        #region Calculate Address
        private void Calc_Address()
        {
            #region Variable declaration
            uint GroupID = 0;        //GroupID
            uint UnitID = 0;         //UnitID
            uint DeviceID = 0;       //DeviceID
            uint VAddress = 0;       //VirtualAddress
            String CalcExpression = null; //Expression
            #endregion

            #region Get Items from Screen
            GroupID = Convert.ToUInt32(IDC_COMBO_GROUP_ID.Text, 16);
            UnitID = Convert.ToUInt32(IDC_COMBO_UNIT_ID.Text, 16);
            DeviceID = Convert.ToUInt32(IDC_COMBO_DEVICE_ID.Text, 16);
            #endregion

            #region Setting Area
            if (string.Compare(IDC_COMBO_AREA.Text, "Configuration") == 0)
            {
                #region Donot use I/O Assist Server
                // When not using I/O Assist Server
                // the configuration is :
                // from Offset & H1000H
                // there is 128 bytes area for each device.
                if (GroupID == 0x08 || GroupID == 0x0A)
                {
                    VAddress = 0x300000 + 0x1000 + DeviceID * 128;
                    CalcExpression = "VAddress = 0x300000 + 0x1000 + DeviceID * 128;";
                }
                #endregion

                #region Use I/O Assist Server
                // When use I/O Assist Server
                // the configuration is :
                // at the &H10000 boundary for each unit 
                // offset from &H1000H,
                // there is 128 bytes area for each device.
                else
                {
                    VAddress = 0x300000 + 0x10000 * UnitID + 0x1000 + DeviceID * 128;
                    CalcExpression = "VAddress = 0x300000 + 0x10000 * UnitID + 0x1000 + DeviceID * 128;";
                }
                #endregion
            }
            #endregion

            #region Input Area
            else if (string.Compare(IDC_COMBO_AREA.Text, "Basic input data") == 0)
            {
                #region Donot use I/O Assist Server
                // Donot use I/O Assist Server's
                // the basic input data is :
                // offset from &H4000H, 
                // there is 128 bytes area for each device.
                if (GroupID == 0x08 || GroupID == 0x0A)
                {
                    VAddress = 0x300000 + 0x4000 + DeviceID * 128;
                    CalcExpression = "VAddress = 0x300000 + 0x4000 + DeviceID * 128;";
                }
                #endregion

                #region Use I/O Assist Server
                // When using I/O Assist Server,
                // the basic input data is : 
                // at the &H10000 boundary for each unit 
                // offset from &H4000H, 
                // there is 128 bytes area for each device.
                else
                {
                    VAddress = 0x300000 + 0x10000 * UnitID + 0x4000 + DeviceID * 128;
                    CalcExpression = "VAddress = 0x300000 + 0x10000 * UnitID + 0x4000 + DeviceID * 128;";
                }
                #endregion
            }
            #endregion

            #region Output Area
            else if (string.Compare(IDC_COMBO_AREA.Text, "Basic Output data") == 0)
            {
                #region Donot use I/O Assist Server
                // When not using I/O Assist Server,
                // the basic output data is : 
                // offset from &H5000H,
                // there is 128 bytes area for each device.
                if (GroupID == 0x08 || GroupID == 0x0A)
                {
                    VAddress = 0x300000 + 0x5000 + DeviceID * 128;
                    CalcExpression = "VAddress = 0x300000 + 0x5000 + DeviceID * 128;";
                }
                #endregion

                #region Use I/O Assist Server
                // When using I/O Assist Server,
                // the basic output data is :
                // at the &H10000 boundary for each unit, 
                // offset from &H5000H,
                // there is 128 bytes area for each device.
                else
                {
                    VAddress = 0x300000 + 0x10000 * UnitID + 0x5000 + DeviceID * 128;
                    CalcExpression = "VAddress = 0x300000 + 0x10000 * UnitID + 0x5000 + DeviceID * 128;";
                }
                #endregion
            }
            #endregion

            #region I/O Area
            else if (string.Compare(IDC_COMBO_AREA.Text, "I / O intensive data storage") == 0)
            {
                #region Donot use I/O Assist Server
                // When not using I/O Assist Server,
                // the area to storage I/O data is from &H380000,
                // 16 bytes for start register, after the error register area, 
                // there is 16 bytes area for each device.
                if (GroupID == 0x08 || GroupID == 0x0A)
                {
                    VAddress = 0x380000 + 16 + DeviceID * 16;
                    CalcExpression = "VAddress = 0x380000 + 16 + DeviceID * 16;";
                }
                #endregion

                #region Use I/O Assist Server
                // When use I/O Assist Server, 
                // the area to storage I/O data is from &H380000,
                // at the &H10000 boundary for each unit, 
                // 16*8=128 bytes for start register, after the error register area,
                // there is 16*8=128 bytes area for each unit,
                // there is 16 bytes area for each device.
                else
                {
                    VAddress = 0x380000 + 128 + UnitID * 128 + DeviceID * 16;
                    CalcExpression = "VAddress = 0x380000 + 128 + UnitID * 128 + DeviceID * 16;";
                }
                #endregion
            }
            #endregion

            #region Show Result
            IDC_EDIT_ADDRESS.Text = VAddress.ToString("X");
            IDC_EDIT_EXPRESSION.Text = CalcExpression;
            #endregion
        }
        #endregion

        #endregion
    }
}