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

using SmartX;
using ITC81DB_2H.Forms;
using ITC81DB_2H.DialogForms;
using ITC81DB_2H_ImageDll;
using ITC81DB_2H.Datastore;

namespace ITC81DB_2H.Controls
{
    public partial class ControlCenterSystemBLDCMotorSetting : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;

        private string CommID;
        private int MotorIndex;
        private Collection<SmartX.SmartListBox> CollectionListBox;

        private BLDCMotorParameter CopyBLDCMotorParameter;
        #endregion

        #region Constructor
        public ControlCenterSystemBLDCMotorSetting(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

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

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

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

            if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.labelTitleSelectOperMode.Text = "Op. Mode :";
                this.labelTitleMotorID.Text = "Motor ID(1~16)";
                this.labelTitleMotorSpeed.Text = "Motor speed(0~100)";
                this.labelTitleMotorPoleNum.Text = "Motor Pole Num";
                this.labelTitleAttenuatorRatio.Text = "Attenuator Ratio(1~)";
                this.labelTitleMotorDirectionWrite.Text = "Motor direction";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {

            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {

            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {

            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            else
            {
                this.labelTitleSelectOperMode.Text = "동작모드 :";
                this.labelTitleMotorID.Text = "모터ID(1~16)";
                this.labelTitleMotorSpeed.Text = "모터 속도(0~100)";
                this.labelTitleMotorPoleNum.Text = "모터 Pole 수";
                this.labelTitleAttenuatorRatio.Text = "감속기 비율(1~)";
                this.labelTitleMotorDirectionWrite.Text = "모터 방향";
            }
        }
        private void DefaultSetting()
        {
            this.MotorIndex = 0;
            this.CommID = "A";

            this.CopyBLDCMotorParameter = new BLDCMotorParameter();

            this.CollectionListBox = new Collection<SmartListBox>();
            this.CollectionListBox.Add(this.listBoxAlarm1);
            this.CollectionListBox.Add(this.listBoxAlarm2);
            this.CollectionListBox.Add(this.listBoxAlarm3);
            this.CollectionListBox.Add(this.listBoxAlarm4);
            this.CollectionListBox.Add(this.listBoxAlarm5);

            this.comboBoxMotorPoleNum.Items.Clear();
            this.comboBoxMotorPoleNum.Items.Add("3");
            this.comboBoxMotorPoleNum.Items.Add("6");
            this.comboBoxMotorPoleNum.Items.Add("9");
            this.comboBoxMotorPoleNum.Items.Add("12");
            this.comboBoxMotorPoleNum.Items.Add("15");

            this.VisibleMotorNum(this.ParentForm.ParentForm.SystemConfig2.BLDCTotalMotorNum);
        }

        private void RefreshDisplay()
        {
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, this.CommID,
    CommunicationAddress.BLDCMotorParameterRead, "");
        }
        public void DisplayRPM()
        {
            this.labelMotorRPM.Text = this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]._6734_MotorRPM;
        }

        public void VisibleMotorNum(int num)
        {
            Collection<SmartX.SmartButton> button = new Collection<SmartButton>();

            button.Clear();
            button.Add(this.buttonMotor1);
            button.Add(this.buttonMotor2);
            button.Add(this.buttonMotor3);
            button.Add(this.buttonMotor4);
            button.Add(this.buttonMotor5);

            for (int i = 0; i < button.Count; i++)
                button[i].Visible = true;

            for (int i = num; i < button.Count; i++)
                button[i].Visible = false;
        }
        public void UpdateAlarm(int index, Collection<BLDCMotorParameter> items)
        {
            switch (index)
            {
                case 0:
                    this.listBoxAlarm1.ClearAll();
                    if (items[index]._6790_Alarm_FaultSignal == true)
                        this.listBoxAlarm1.AddItem("Detect Fault Signal");
                    if (items[index]._6791_Alarm_HallSensorError == true)
                        this.listBoxAlarm1.AddItem("Hall Sensor Error");
                    if (items[index]._6792_Alarm_MotorStop == true)
                        this.listBoxAlarm1.AddItem("Motor stopped while operating");
                    break;
                case 1:
                    this.listBoxAlarm2.ClearAll();
                    if (items[index]._6790_Alarm_FaultSignal == true)
                        this.listBoxAlarm2.AddItem("Detect Fault Signal");
                    if (items[index]._6791_Alarm_HallSensorError == true)
                        this.listBoxAlarm2.AddItem("Hall Sensor Error");
                    if (items[index]._6792_Alarm_MotorStop == true)
                        this.listBoxAlarm2.AddItem("Motor stopped while operating");
                    break;
                case 2:
                    this.listBoxAlarm3.ClearAll();
                    if (items[index]._6790_Alarm_FaultSignal == true)
                        this.listBoxAlarm3.AddItem("Detect Fault Signal");
                    if (items[index]._6791_Alarm_HallSensorError == true)
                        this.listBoxAlarm3.AddItem("Hall Sensor Error");
                    if (items[index]._6792_Alarm_MotorStop == true)
                        this.listBoxAlarm3.AddItem("Motor stopped while operating");
                    break;
                case 3:
                    this.listBoxAlarm4.ClearAll();
                    if (items[index]._6790_Alarm_FaultSignal == true)
                        this.listBoxAlarm4.AddItem("Detect Fault Signal");
                    if (items[index]._6791_Alarm_HallSensorError == true)
                        this.listBoxAlarm4.AddItem("Hall Sensor Error");
                    if (items[index]._6792_Alarm_MotorStop == true)
                        this.listBoxAlarm4.AddItem("Motor stopped while operating");
                    break;
                case 4:
                    this.listBoxAlarm5.ClearAll();
                    if (items[index]._6790_Alarm_FaultSignal == true)
                        this.listBoxAlarm5.AddItem("Detect Fault Signal");
                    if (items[index]._6791_Alarm_HallSensorError == true)
                        this.listBoxAlarm5.AddItem("Hall Sensor Error");
                    if (items[index]._6792_Alarm_MotorStop == true)
                        this.listBoxAlarm5.AddItem("Motor stopped while operating");
                    break;
                default:
                    break;
            }
        }
        public void UpdateParameter(int index, Collection<BLDCMotorParameter> items)
        {
            this.labelVersionMajor.Text = items[index]._6730_VersionMajor + ".";
            this.labelVersionMinor.Text = items[index]._6731_VersionMinor + ".";
            this.labelVersionBuild.Text = items[index]._6732_VersionBuild;

            this.labelMotorRPM.Text = items[index]._6734_MotorRPM.ToString();
            switch (items[index]._6735_MotorDirection)
            {
                case 0:
                    this.labelMotorDirectionRead.Text = "None";
                    break;
                case 1:
                    this.labelMotorDirectionRead.Text = "CW";
                    break;
                case 2:
                    this.labelMotorDirectionRead.Text = "CCW";
                    break;
                default:
                    this.labelMotorDirectionRead.Text = "None";
                    break;
            }
            switch (items[index]._6765_SelectOperMode)
            {
                case 0:
                    this.labelSelectOperMode.Text = "Manual";
                    break;
                case 1:
                    this.labelSelectOperMode.Text = "MCU";
                    break;
                default:
                    this.labelMotorDirectionRead.Text = "Manual";
                    break;
            }
        }
        public void DisplayParameter(int index, Collection<BLDCMotorParameter> items)
        {
            this.labelMotorID.Text = items[index].MotorID.ToString();
            this.labelMotorSpeed.Text = items[index]._6704_MotorSpeed.ToString();
            this.comboBoxMotorPoleNum.SelectedIndex = items[index]._6705_PoleNum;
            this.labelAttenuatorRatio.Text = items[index]._6706_AttenuatorRatio.ToString();
            if (items[index]._6764_OperDirection == 1)
                this.buttonMotorDirectionWrite.ButtonDown();
            else
                this.buttonMotorDirectionWrite.ButtonUp();
        }

        public void DisplayRefresh(SystemStatus status, Collection<BLDCMotorParameter> items)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = Define.E_DisplayStore.SystemBLDCMotorSetting;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_DisplayMode.EquipmentTest);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.buttonCopy.Text = "";
            this.MotorIndex = 0;
            this.CommID = "A";
            this.listBoxAlarm1.BringToFront();

            this.buttonMotor2.ButtonUp();
            this.buttonMotor3.ButtonUp();
            this.buttonMotor4.ButtonUp();
            this.buttonMotor5.ButtonUp();
            this.buttonMotor1.ButtonDown();

            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, this.CommID, 
                CommunicationAddress.BLDCMotorParameterRead, "");

            this.DisplayParameter(0, items);
        }
        #endregion
      
        #region Event Handler
        private void buttonMotor_Click(object sender, EventArgs e)
        {
            SmartX.SmartButton button = sender as SmartX.SmartButton;

            this.MotorIndex = int.Parse(button.Tag.ToString());
            this.CommID = this.ParentForm.ParentForm.CurrentCommunicationID(this.MotorIndex);
            this.CollectionListBox[this.MotorIndex].BringToFront();

            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, this.CommID, 
                CommunicationAddress.BLDCMotorParameterRead, "");

            this.DisplayParameter(this.MotorIndex, this.ParentForm.ParentForm.CollectionBLDCParameter);

            this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.BLDCMotorNum, (this.MotorIndex + 1).ToString(), "", "");
        }

        private void buttonDefaultSet_Click(object sender, EventArgs e)
        {
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 25);
            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.BLDCDefaultSet, CommunicationID.MainBoard);

                for (int i = 0; i < this.ParentForm.ParentForm.SystemConfig2.BLDCTotalMotorNum; i++)
                {
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6704_MotorSpeed = 80;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6705_PoleNum = 1;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6706_AttenuatorRatio = 5;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6764_OperDirection = 1;
                }
                this.ParentForm.ParentForm.SaveBLDCMotorFile(this.ParentForm.ParentForm.CollectionBLDCParameter);
                //this.ParentForm.ParentForm.TransferBLDCMotorData
                //    (this.CommID, this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]);
            }
        }
        private void buttonReboot_Click(object sender, EventArgs e)
        {
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 26);
            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.BLDCMCUReboot, CommunicationID.MainBoard);
            }
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            this.RefreshDisplay();
        }

        private void labelMotorID_Click(object sender, EventArgs e)
        {
            string value = "", message = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelMotorID.Text, 2, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue < 1 || myKeyPad.IntValue > 16)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelMotorID.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex].MotorID = myKeyPad.IntValue;

                    value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);
                    this.ParentForm.ParentForm.SaveBLDCMotorFile(this.ParentForm.ParentForm.CollectionBLDCParameter);
                    this.ParentForm.ParentForm.TransferBLDCMotorData
                        (this.CommID, this.ParentForm.ParentForm.SystemConfig2, this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]);
                }
            }
        }
        private void labelMotorSpeed_Click(object sender, EventArgs e)
        {
            string value = "", message = "";
            string before = "", after = "";

            before = this.labelMotorSpeed.Text;
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelMotorSpeed.Text, 3, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue < 0 || myKeyPad.IntValue > 100)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language, message);

                    myMsg.ShowDialog();
                }
                else
                {
                    after = this.labelMotorSpeed.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]._6704_MotorSpeed = myKeyPad.IntValue;

                    value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);
                    this.ParentForm.ParentForm.SaveBLDCMotorFile(this.ParentForm.ParentForm.CollectionBLDCParameter);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, this.CommID,
                        CommunicationAddress.BLDCMotorSpeed, value);

                    if (before != after)
                        this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.BLDCMotorSpeed, "", before, after);
                }
            }
        }
        private void comboBoxMotorPoleNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = "";
            string before = "", after = "";

            before = this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]._6705_PoleNum.ToString();
            this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]._6705_PoleNum = this.comboBoxMotorPoleNum.SelectedIndex;
            after = this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]._6705_PoleNum.ToString();

            value = Helper.StringZeroFillDigits4(this.comboBoxMotorPoleNum.SelectedIndex.ToString());
            this.ParentForm.ParentForm.SaveBLDCMotorFile(this.ParentForm.ParentForm.CollectionBLDCParameter);
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, this.CommID, CommunicationAddress.BLDCPoleNum, value);

            if (before != after)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.BLDCPollNum, "", before, after);
        }
        private void labelAttenuatorRatio_Click(object sender, EventArgs e)
        {
            string value = "", message = "";
            string before = "", after = "";

            before = this.labelAttenuatorRatio.Text;
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelAttenuatorRatio.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue < 1 || myKeyPad.IntValue > 9999)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    after = this.labelAttenuatorRatio.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]._6706_AttenuatorRatio = myKeyPad.IntValue;

                    value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);
                    this.ParentForm.ParentForm.SaveBLDCMotorFile(this.ParentForm.ParentForm.CollectionBLDCParameter);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, this.CommID, CommunicationAddress.BLDCAttenuatorRatio, value);

                    if (before != after)
                        this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.BLDCAttenuatorRatio, "", before, after);
                }
            }
        }
        private void buttonMotorDirectionWrite_Click(object sender, EventArgs e)
        {
            string value = "";
            string before = "", after = "";

            before = this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]._6764_OperDirection.ToString();
            if (this.buttonMotorDirectionWrite.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]._6764_OperDirection = 1; // CCW
            else
                this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]._6764_OperDirection = 0; // CW
            after = this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]._6764_OperDirection.ToString();

            this.ParentForm.ParentForm.SaveBLDCMotorFile(this.ParentForm.ParentForm.CollectionBLDCParameter);
            value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]._6764_OperDirection.ToString());
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, this.CommID, CommunicationAddress.BLDCOperDirection, value);

            if (before != after)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.BLDCMotorDirection, "", before, after);

            this.RefreshDisplay();
        }

        private void buttonCopy_Click(object sender, EventArgs e)
        {
            this.buttonCopy.Text = (this.MotorIndex + 1).ToString();

            BLDCMotorParameter BLDCParam = this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex];

            this.CopyBLDCMotorParameter._6700_CommBaudrate = BLDCParam._6700_CommBaudrate;
            this.CopyBLDCMotorParameter._6701_CommParityBit = BLDCParam._6701_CommParityBit;
            this.CopyBLDCMotorParameter._6702_PWMDelay = BLDCParam._6702_PWMDelay;
            this.CopyBLDCMotorParameter._6703_FeedbackDeviation = BLDCParam._6703_FeedbackDeviation;
            this.CopyBLDCMotorParameter._6704_MotorSpeed = BLDCParam._6704_MotorSpeed;
            this.CopyBLDCMotorParameter._6705_PoleNum = BLDCParam._6705_PoleNum;
            this.CopyBLDCMotorParameter._6706_AttenuatorRatio = BLDCParam._6706_AttenuatorRatio;
            this.CopyBLDCMotorParameter._6760_BootingStart = BLDCParam._6760_BootingStart;
            this.CopyBLDCMotorParameter._6761_PortUse = BLDCParam._6761_PortUse;
            this.CopyBLDCMotorParameter._6762_Feedback = BLDCParam._6762_Feedback;
            this.CopyBLDCMotorParameter._6763_InitDirection = BLDCParam._6763_InitDirection;
            this.CopyBLDCMotorParameter._6764_OperDirection = BLDCParam._6764_OperDirection;
        }
        private void buttonPasteAll_Click(object sender, EventArgs e)
        {
            if (this.buttonCopy.Text != "")
            {
                for (int i = 0; i < this.ParentForm.ParentForm.SystemConfig2.BLDCTotalMotorNum; i++)
                {
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6700_CommBaudrate = this.CopyBLDCMotorParameter._6700_CommBaudrate;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6701_CommParityBit = this.CopyBLDCMotorParameter._6701_CommParityBit;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6702_PWMDelay = this.CopyBLDCMotorParameter._6702_PWMDelay;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6703_FeedbackDeviation = this.CopyBLDCMotorParameter._6703_FeedbackDeviation;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6704_MotorSpeed = this.CopyBLDCMotorParameter._6704_MotorSpeed;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6705_PoleNum = this.CopyBLDCMotorParameter._6705_PoleNum;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6706_AttenuatorRatio = this.CopyBLDCMotorParameter._6706_AttenuatorRatio;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6760_BootingStart = this.CopyBLDCMotorParameter._6760_BootingStart;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6761_PortUse = this.CopyBLDCMotorParameter._6761_PortUse;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6762_Feedback = this.CopyBLDCMotorParameter._6762_Feedback;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6763_InitDirection = this.CopyBLDCMotorParameter._6763_InitDirection;
                    this.ParentForm.ParentForm.CollectionBLDCParameter[i]._6764_OperDirection = this.CopyBLDCMotorParameter._6764_OperDirection;
                }

                this.ParentForm.ParentForm.SaveBLDCMotorFile(this.ParentForm.ParentForm.CollectionBLDCParameter);
                this.ParentForm.ParentForm.TransferBLDCMotorData("0", this.ParentForm.ParentForm.SystemConfig2, 
                    this.ParentForm.ParentForm.CollectionBLDCParameter[this.MotorIndex]);
            }
            this.buttonCopy.Text = "";
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, this.CommID,
                CommunicationAddress.BLDCMotorRPM, "");
        }
        #endregion
    }
}
