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

using SmartX;
using INT63DC_6CH.Controls;
using INT63DC_6CH.DialogForms;
using INT63DC_ImageDll;

namespace INT63DC_6CH.Forms
{
    public partial class FormEquipmentTest : Form
    {
        #region Field
        private FormMain m_ParentForm;
        private ControlEquipmentTest ChildControl1;
        #endregion

        #region Constructor
        public FormEquipmentTest(FormMain parent)
        {
            InitializeComponent();

            this.ParentForm = parent;
        }
        #endregion

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

        #region Method
        private void InitializeDesign()
        {
            ImageDll images = new ImageDll();

            if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Korean)
            {

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
            {
                this.labelTitle.Text = "Equipment Test";

                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackUp));
                this.buttonDisplay.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engScreenDisable));
                this.buttonDisplay.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engScreenDown));
                this.buttonDisplay.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engScreenUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
            {
                this.labelTitle.Text = "Equipment Test";

                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackUp));
                this.buttonDisplay.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnScreenDisable));
                this.buttonDisplay.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnScreenDown));
                this.buttonDisplay.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnScreenUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
            {
                this.labelTitle.Text = "Test zařízení";

                this.buttonCOM3LogOpen.Text = this.buttonLogOpen.Text = "Otevřít";
                this.buttonCOM3LogClose.Text = this.buttonLogClose.Text = "Zavřít";

                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackUp));
                this.buttonDisplay.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeScreenDisable));
                this.buttonDisplay.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeScreenDown));
                this.buttonDisplay.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeScreenUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.German)
            {
                this.labelTitle.Text = "Vorrichtungstest";

                this.labelStaticCommLog.Text = "Kommunikationsprotokoll";
                this.labelStaticCOM3CommLog.Text = "COM3 Protokoll";

                this.buttonLogOpen.Text = this.buttonCOM3LogOpen.Text = "Öffnen";
                this.buttonLogClose.Text = this.buttonCOM3LogClose.Text = "Schließen";

                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackUp));
                this.buttonDisplay.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerScreenDisable));
                this.buttonDisplay.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerScreenDown));
                this.buttonDisplay.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerScreenUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Japanese)
            {
                this.labelTitle.Text = "装置テスト";

                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackUp));
                this.buttonDisplay.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnScreenDisable));
                this.buttonDisplay.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnScreenDown));
                this.buttonDisplay.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnScreenUp));
            }
            else
            {

            }
        }
        private void InitializeControl()
        {
            if (this.ParentForm.EquipmentStatus == DataStore.EquipmentStatus.Stop)
            {
                this.pictureBoxStop.Visible = true;
                this.pictureBoxStart.Visible = false;
            }
            else
            {
                this.pictureBoxStop.Visible = false;
                this.pictureBoxStart.Visible = true;
            }
        }
        private void DefaultSetting()
        {
            this.ChildControl1 = new ControlEquipmentTest(this);
            this.Controls.Add(this.ChildControl1);
            this.ChildControl1.Location = new Point(0, 70);
            this.ChildControl1.Visible = false;
        }

        private void TransferDataStreamASetting(string value)
        {
            string length = "";

            length = Helper.StringZeroFillDigits4(value.Length.ToString());

            this.ParentForm.TransferDataStream(CommunicationCommand.BypassToMotor, CommunicationID.MainBoard, CommunicationAddress.None, length + "A" + value);
        }
        private void SetlabelOnOff(Label control, bool isOn)
        {
            if (isOn == true)
            {
                if (control.Visible != true)
                    control.Visible = true;
            }
            else
            {
                if (control.Visible != false)
                    control.Visible = false;
            }
        }

        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {
            if (status == DataStore.EquipmentStatus.Start)
            {
                this.pictureBoxStart.Visible = true;
                this.pictureBoxStop.Visible = false;
                this.buttonBack.Enabled = false;
            }
            else
            {
                this.pictureBoxStart.Visible = false;
                this.pictureBoxStop.Visible = true;
                this.buttonBack.Enabled = true;
            }
        }
      
        public void DisplayRefresh()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormEquipmentTest;

            this.ParentForm.TransferData(CommunicationCommand.ModeEquipmentTest, CommunicationID.MainBoard);

        }
        #endregion

        #region Override Member
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

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

        #region Event Handler
        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
        }
        private void buttonLogOpen_Click(object sender, EventArgs e)
        {
            this.ParentForm.smartFileCommunicationLog.Close();
            this.ParentForm.smartFileCommunicationLog.Open(this.ParentForm.BufferSmartUart);

            this.ParentForm.IsCommunicationLogOpen = true;
        }
        private void buttonLogClose_Click(object sender, EventArgs e)
        {
            this.ParentForm.IsCommunicationLogOpen = false;

            this.ParentForm.smartFileCommunicationLog.Close();
        }
        private void buttonCOM3LogOpen_Click(object sender, EventArgs e)
        {
            this.ParentForm.smartFileCom3Log.Close();
            this.ParentForm.smartFileCom3Log.Open(this.ParentForm.BufferSmartUart);

            this.ParentForm.IsCom3LogOpen = true;
        }
        private void buttonCOM3LogClose_Click(object sender, EventArgs e)
        {
            this.ParentForm.IsCom3LogOpen = false;

            this.ParentForm.smartFileCom3Log.Close();
        }
        private void buttonCOM1LogOpen_Click(object sender, EventArgs e)
        {
            this.ParentForm.smartFileCom1Log.Close();
            this.ParentForm.smartFileCom1Log.Open(this.ParentForm.BufferSmartUart);

            this.ParentForm.IsCom1LogOpen = true;
        }
        private void buttonCOM1LogClose_Click(object sender, EventArgs e)
        {
            this.ParentForm.IsCom1LogOpen = false;

            this.ParentForm.smartFileCom1Log.Close();
        }
        private void buttonOrigin_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.MotorOrigin, CommunicationID.MainBoard);
        }
        private void buttonPosition0_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.MotorPosition0, CommunicationID.MainBoard);
        }
        private void buttonPosition1_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.MotorPosition1, CommunicationID.MainBoard);
        }
        private void buttonPosition2_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.MotorPosition2, CommunicationID.MainBoard);
        }
        private void buttonStep_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.MotorStep, CommunicationID.MainBoard);
        }
        private void buttonAlarmReset_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.MotorAlarmReset, CommunicationID.MainBoard);
        }
        private void buttonDisplay_Click(object sender, EventArgs e)
        {
            if (this.ChildControl1.Visible == false)
            {
                this.ChildControl1.BringToFront();
                this.ChildControl1.Visible = true;
            }
            else
                this.ChildControl1.Visible = false;
        }

        private void pictureBoxStart_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.Stop, CommunicationID.MainBoard);
        }
        private void pictureBoxStop_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.Start, CommunicationID.MainBoard);
        }

        private void checkBoxCutView_Click(object sender, EventArgs e)
        {
            if (this.checkBoxCutView.Checked == true)
                this.ParentForm.SystemConfig.IsCutView = true;
            else
                this.ParentForm.SystemConfig.IsCutView = false;
        }
        #endregion
    }
}