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

using SmartX;

using INT51DB_ImageDll;
using INT51DB.DialogForms;
using INT51DB.Forms;

namespace INT51DB.Controls
{
    public partial class ControlMainDisplayFunction2 : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        #endregion

        #region Constructor
        public ControlMainDisplayFunction2(FormMainDisplay parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.InitializeDesign();
            this.DefaultSetting();
        } 
        #endregion

        #region Property
        public FormMainDisplay ParentForm
        {
            get { return this.m_ParentForm; }
            set { this.m_ParentForm = value; }
        } 
        #endregion

        #region Method
        public void InitializeDesign()
        {
            Class1 images = new Class1();

            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
            }
            else
            {
            }
        }
        private void DefaultSetting()
        {

        }

        private void ModbusStatusDisplay(bool isOpen)
        {
            if (isOpen == true)
            {
                this.labelModusStatus.Text = "OPEN";
                this.labelModusStatus.BackColor = Color.DarkGreen;
                this.labelModusStatus.ForeColor = Color.White;
            }
            else
            {
                this.labelModusStatus.Text = "CLOSE";
                this.labelModusStatus.BackColor = Color.Crimson;
                this.labelModusStatus.ForeColor = Color.White;
            }
        }
        public void ModbusOpen()
        {
            if (this.ParentForm.ParentForm.smartModbus.IsOpen == true)
                this.ParentForm.ParentForm.smartModbus.PortClose();

            this.ParentForm.ParentForm.smartModbus.PortOpen();
            this.ModbusStatusDisplay(this.ParentForm.ParentForm.smartModbus.IsOpen);
        }
        public void ModbusClose()
        {
            this.ParentForm.ParentForm.smartModbus.PortClose();
            this.ModbusStatusDisplay(this.ParentForm.ParentForm.smartModbus.IsOpen);
        }

        private void ServoMotorWrite(int slaveAddress, int parameterAddress, float value)
        {
            byte[] writeData = new byte[4];
            byte[] readData = new byte[8];

            writeData = BitConverter.GetBytes(value);
            Array.Reverse(writeData);

            this.ParentForm.ParentForm.smartModbus.SlaveAddress = slaveAddress;
            if (this.ParentForm.ParentForm.smartModbus.DirectParameter(0x06, parameterAddress, 1, writeData, ref readData) == SmartX.SmartModbus.EXCEPTIONCODE.SUCCESS)
            {
                MessageBox.Show("쓰기완료");
            }
        }
        private string ServoMotorRead(int slaveAddress, int parameterAddress)
        {
            float value;
            byte[] readData = new byte[4];

            this.ParentForm.ParentForm.smartModbus.SlaveAddress = slaveAddress;
            this.ParentForm.ParentForm.smartModbus.ReadHoldingRegister(parameterAddress, 1, ref readData);

            Array.Reverse(readData);
            value = BitConverter.ToSingle(readData, 0);

            return string.Format("{0:0}", value);
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ModbusOpen();

            //this.labelPosition10.Text = "0";
            //this.labelPosition11.Text = "0";
            //this.labelPosition20.Text = "0";
            //this.labelPosition21.Text = "0";
            //this.labelSpeed10.Text = "0";
            //this.labelSpeed20.Text = "0";
        }
        #endregion
      
        #region Event Handler
        private void buttonOpen_Click(object sender, EventArgs e)
        {
            this.ModbusOpen();
        }

        private void buttonParameterRead_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.ParentForm.smartModbus.IsOpen == false)
            {
                // 통신 연결상태를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(23, this.ParentForm.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();
                return;
            }

            this.ParentForm.ParentForm.smartModbus.ReadTimeout = 500;

            SmartSplash splash = new SmartSplash();
            splash.CenterPosition = true;
            splash.AnimationInterval = 200;
            splash.LoadingImagePathname = "SmartLoading4";
            splash.Start();

            this.Enabled = false;

            // ServoMotor 1
            this.labelSpeed10.Text = this.ServoMotorRead(1, ServoMotorParameterAddress.GropSpeed0);
            this.labelPosition10.Text = this.ServoMotorRead(1, ServoMotorParameterAddress.PositionCMD0);
            this.labelPosition11.Text = this.ServoMotorRead(1, ServoMotorParameterAddress.PositionCMD1);

            // ServoMotor 2
            this.labelSpeed20.Text = this.ServoMotorRead(2, ServoMotorParameterAddress.GropSpeed0);
            this.labelPosition20.Text = this.ServoMotorRead(2, ServoMotorParameterAddress.PositionCMD0);
            this.labelPosition21.Text = this.ServoMotorRead(2, ServoMotorParameterAddress.PositionCMD1);

            splash.Finish();

            this.Enabled = true;
        }
     
        private void labelMotorParameterSetup_Click(object sender, EventArgs e)
        {
            float fValue = float.NaN;

            SmartLabel label = sender as SmartLabel;

            if (label == null)
                return;

            if (this.ParentForm.ParentForm.smartModbus.IsOpen == false)
            {
                // 통신 연결상태를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(23, this.ParentForm.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();
                return;
            }

            if (label == this.labelSpeed10)
            {
                #region Conveyor Motor Speed 0
                DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSpeed10.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

                if (myKeyPad.ShowDialog() == DialogResult.OK)
                {
                    if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                    {
                        // 입력범위를 확인하세요
                        DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                        myMsg.ShowDialog();
                    }
                    else
                    {
                        this.labelSpeed10.Text = myKeyPad.StringValue;

                        fValue = Convert.ToSingle(this.labelSpeed10.Text);

                        this.ServoMotorWrite(1, ServoMotorParameterAddress.GropSpeed0, fValue);
                    }
                }
                #endregion
            }
            else if (label == this.labelPosition10)
            {
                #region Conveyor Motor Position 0
                DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelPosition10.Text, 4, 0, true, this.ParentForm.ParentForm.SystemConfig.Language);

                if (myKeyPad.ShowDialog() == DialogResult.OK)
                {
                    if (myKeyPad.doubleValue < -9999 || myKeyPad.doubleValue > 9999)
                    {
                        // 입력범위를 확인하세요
                        DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                        myMsg.ShowDialog();
                    }
                    else
                    {
                        this.labelPosition10.Text = myKeyPad.StringValue;

                        fValue = Convert.ToSingle(this.labelPosition10.Text);

                        this.ServoMotorWrite(1, ServoMotorParameterAddress.PositionCMD0, fValue);
                        this.ServoMotorWrite(1, ServoMotorParameterAddress.PositionCMD8, fValue);
                    }
                }
                #endregion
            }
            else if (label == this.labelPosition11)
            {
                #region Conveyor Motor Position 1
                DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelPosition11.Text, 4, 0, true, this.ParentForm.ParentForm.SystemConfig.Language);

                if (myKeyPad.ShowDialog() == DialogResult.OK)
                {
                    if (myKeyPad.doubleValue < -9999 || myKeyPad.doubleValue > 9999)
                    {
                        // 입력범위를 확인하세요
                        DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                        myMsg.ShowDialog();
                    }
                    else
                    {
                        this.labelPosition11.Text = myKeyPad.StringValue;

                        fValue = Convert.ToSingle(this.labelPosition11.Text);

                        this.ServoMotorWrite(1, ServoMotorParameterAddress.PositionCMD1, fValue);
                    }
                }
                #endregion
            }
            else if (label == this.labelSpeed20)
            {
                #region UpDown Motor Speed 0
                DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSpeed20.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

                if (myKeyPad.ShowDialog() == DialogResult.OK)
                {
                    if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                    {
                        // 입력범위를 확인하세요
                        DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                        myMsg.ShowDialog();
                    }
                    else
                    {
                        this.labelSpeed20.Text = myKeyPad.StringValue;

                        fValue = Convert.ToSingle(this.labelSpeed20.Text);

                        this.ServoMotorWrite(2, ServoMotorParameterAddress.GropSpeed0, fValue);
                    }
                }
                #endregion
            }
            else if (label == this.labelPosition20)
            {
                #region UpDown Motor Position 0
                DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelPosition20.Text, 4, 0, true, this.ParentForm.ParentForm.SystemConfig.Language);

                if (myKeyPad.ShowDialog() == DialogResult.OK)
                {
                    if (myKeyPad.doubleValue < -9999 || myKeyPad.doubleValue > 9999)
                    {
                        // 입력범위를 확인하세요
                        DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                        myMsg.ShowDialog();
                    }
                    else
                    {
                        this.labelPosition20.Text = myKeyPad.StringValue;

                        fValue = Convert.ToSingle(this.labelPosition20.Text);

                        this.ServoMotorWrite(2, ServoMotorParameterAddress.PositionCMD0, fValue);
                        this.ServoMotorWrite(2, ServoMotorParameterAddress.PositionCMD8, fValue);
                    }
                }
                #endregion
            }
            else if (label == this.labelPosition21)
            {
                #region UpDown Motor Position 1
                DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelPosition21.Text, 4, 0, true, this.ParentForm.ParentForm.SystemConfig.Language);

                if (myKeyPad.ShowDialog() == DialogResult.OK)
                {
                    if (myKeyPad.doubleValue < -9999 || myKeyPad.doubleValue > 9999)
                    {
                        // 입력범위를 확인하세요
                        DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                        myMsg.ShowDialog();
                    }
                    else
                    {
                        this.labelPosition21.Text = myKeyPad.StringValue;

                        fValue = Convert.ToSingle(this.labelPosition21.Text);

                        this.ServoMotorWrite(2, ServoMotorParameterAddress.PositionCMD1, fValue);
                    }
                }
                #endregion
            }
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.ModbusClose();
            this.Visible = false;
        }
        #endregion
    }
}
