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

using SmartX;
using INT63DC_2C.Forms;
using INT63DC_2C.DialogForms;

namespace INT63DC_2C.Controls
{
    public partial class ControlEquipmentTest : UserControl
    {
        #region Field
        private FormEquipmentTest m_ParentForm;

        private Collection<SmartLabel> CollectionConfigurationLabel;
        private Collection<SmartLabel> CollectionEquipmentSettingLabel;
        private Collection<SmartLabel> CollectionSystemSettingLabel;
        private Collection<SmartLabel> CollectionCalibrationLabel;
        #endregion
        
        #region Constructor
        public ControlEquipmentTest(FormEquipmentTest parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

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

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

        #region Method
        private void InitializeControl()
        {
            if (this.CollectionConfigurationLabel != null && this.CollectionConfigurationLabel.Count != 0)
            {
                foreach (SmartLabel label in this.CollectionConfigurationLabel)
                    label.Text = "-";

                foreach (SmartLabel label in this.CollectionEquipmentSettingLabel)
                    label.Text = "-";

                foreach (SmartLabel label in this.CollectionSystemSettingLabel)
                    label.Text = "-";

                foreach (SmartLabel label in this.CollectionCalibrationLabel)
                    label.Text = "-";
            }
        }
        private void DefaultSetting()
        {
            this.CollectionConfigurationLabel = new Collection<SmartLabel>();
            this.CollectionConfigurationLabel.Add(this.labelZeroParameterTime);
            this.CollectionConfigurationLabel.Add(this.labelZeroParameterRange);
            this.CollectionConfigurationLabel.Add(this.labelZeroParameterVariate);
            this.CollectionConfigurationLabel.Add(this.labelCH3Baudrate);
            this.CollectionConfigurationLabel.Add(this.labelCH3Mode);
            this.CollectionConfigurationLabel.Add(this.labelChattering);
            this.CollectionConfigurationLabel.Add(this.labelBuzzerOnTime);
            this.CollectionConfigurationLabel.Add(this.labelRelayOnTime);
            this.CollectionConfigurationLabel.Add(this.labelDecimalPlaces);

            this.CollectionEquipmentSettingLabel = new Collection<SmartLabel>();
            this.CollectionEquipmentSettingLabel.Add(this.labelEquipmentColumn);
            this.CollectionEquipmentSettingLabel.Add(this.labelWeightView);
            this.CollectionEquipmentSettingLabel.Add(this.labelIndividualNG);
            this.CollectionEquipmentSettingLabel.Add(this.labelLanguage);

            this.CollectionSystemSettingLabel = new Collection<SmartLabel>();
            this.CollectionSystemSettingLabel.Add(this.labelFilter);
            this.CollectionSystemSettingLabel.Add(this.labelJudgmentCount);
            this.CollectionSystemSettingLabel.Add(this.labelSorterRunTime);

            this.CollectionCalibrationLabel = new Collection<SmartLabel>();
            this.CollectionCalibrationLabel.Add(this.labelZeroParameterTime);
            this.CollectionCalibrationLabel.Add(this.labelZeroParameterRange);
            this.CollectionCalibrationLabel.Add(this.labelZeroParameterVariate);
        }

        public void UpdateZeroParameterDisplay(DataStore.EquipmentStatus status, ZeroParameterItem item)
        {
            string sValue = "";

            sValue = item.Time.Trim();
            if (this.labelZeroParameterTime.Text != sValue)
                this.labelZeroParameterTime.Text = sValue;

            sValue = item.Range.Trim();
            if (this.labelZeroParameterRange.Text != sValue)
                this.labelZeroParameterRange.Text = sValue;

            sValue = item.Variate.Trim();
            if (sValue == "0")
                this.labelZeroParameterVariate.Text = "0.05";
            else if (sValue == "1")
                this.labelZeroParameterVariate.Text = "0.1";
            else if (sValue == "2")
                this.labelZeroParameterVariate.Text = "0.2";
            else if (sValue == "3")
                this.labelZeroParameterVariate.Text = "0.5";
            else if (sValue == "4")
                this.labelZeroParameterVariate.Text = "1.0";
            else if (sValue == "5")
                this.labelZeroParameterVariate.Text = "1.2";
            else
                this.labelZeroParameterVariate.Text = "0.05";
        }
        public void UpdateOptionParameterDisplay(DataStore.EquipmentStatus status, OptionParameterItem item)
        {
            string value = "";

            value = item.Chattering.Trim();
            if (this.labelChattering.Text != value)
                this.labelChattering.Text = value;

            value = item.BuzzerOnTime.Trim();
            if (this.labelBuzzerOnTime.Text != value)
                this.labelBuzzerOnTime.Text = value;

            value = item.RelayOnTime.Trim();
            if (this.labelRelayOnTime.Text != value)
                this.labelRelayOnTime.Text = value;
        }
        private void UpdateCommunicationDisplay(SystemConfigurationItem item)
        {
            string value = "";

            value = item.Serial3BaudRate.ToString();
            if (this.labelCH3Baudrate.Text != value)
                this.labelCH3Baudrate.Text = value;

            if (item.Serial3Mode == 0)
                this.labelCH3Mode.Text = "None";
            else if (item.Serial3Mode == 1)
                this.labelCH3Mode.Text = "STD1";
            else if (item.Serial3Mode == 2)
                this.labelCH3Mode.Text = "STD2";
            else if (item.Serial3Mode == 3)
                this.labelCH3Mode.Text = "STD3";
            else
                this.labelCH3Mode.Text = "None";
        }
        public void UpdateEquipmentSettingDisplay(SystemConfigurationItem item)
        {
            int iValue = 0;
            string sValue = "";

            // 장비 열
            sValue = item.EquipmentColumns.ToString();
            if (this.labelEquipmentColumn.Text != sValue)
                this.labelEquipmentColumn.Text = sValue;

            // 중량표시
            if (item.IsWeightViewForward == true)
                this.labelWeightView.Text = "Forward";
            else
                this.labelWeightView.Text = "Reverse";

            // 개별NG
            if (item.IsEachNG == true)
                this.labelIndividualNG.Text = "ON";
            else
                this.labelIndividualNG.Text = "OFF";

            // 언어
            sValue = item.Language.ToString();
            if (this.labelLanguage.Text != sValue)
                this.labelLanguage.Text = sValue;
        }
        private void UpdateSystemSettingDisplay(JudgmentSetItem item)
        {
            string value = "";

            // 필터
            value = item.Filter.ToString();
            if (this.labelFilter.Text != value)
                this.labelFilter.Text = value;

            // 판정개수
            value = item.JudgmentCount.ToString();
            if (this.labelJudgmentCount.Text != value)
                this.labelJudgmentCount.Text = value;

            // 선별기 동작시간
            value = item.Sorter1RunTime.ToString();
            if (this.labelSorterRunTime.Text != value)
                this.labelSorterRunTime.Text = value;
        }
        public void UpdateCalibrationDisplay(CalibrationItem item)
        {
            string value = "";

            // 분동중량
            value = item.BalanceWeight;
            if (this.labelBalanceWeight.Text != value)
                this.labelBalanceWeight.Text = value;

            // 최대중량
            value = item.MaxWeight;
            if (this.labelMaxWeight.Text != value)
                this.labelMaxWeight.Text = value;

            // 한눈의값
            value = item.Digit;
            if (this.labelDigit.Text != value)
                this.labelDigit.Text = value;
        }
       
        public void DisplayRefresh()
        {
            this.InitializeControl();
        }
        #endregion

        #region Event Handler
        private void buttonReadConfiguration_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.SubBoard1, CommunicationAddress._4901_ParameterRead4901, "");
        }
        private void buttonReadEquipmentSetting_Click(object sender, EventArgs e)
        {
            this.UpdateEquipmentSettingDisplay(this.ParentForm.ParentForm.SystemConfig);
        }
        private void buttonReadSystemSetting_Click(object sender, EventArgs e)
        {
            this.UpdateSystemSettingDisplay(this.ParentForm.ParentForm.CurrentJudgmentSetItem);
        }
        private void buttonReadCalibration_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.SubBoardAll, CommunicationAddress._3901_ParameterRead3901, "");
        }
        private void buttonAllRead_Click(object sender, EventArgs e)
        {
            // 환경설정
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.SubBoard1, CommunicationAddress._4901_ParameterRead4901, "");
            Thread.Sleep(100);

            // 장비설정
            this.UpdateEquipmentSettingDisplay(this.ParentForm.ParentForm.SystemConfig);
            Thread.Sleep(100);

            // 시스템설정
            this.UpdateSystemSettingDisplay(this.ParentForm.ParentForm.CurrentJudgmentSetItem);
            Thread.Sleep(100);

            // 중량조정
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.SubBoardAll, CommunicationAddress._3901_ParameterRead3901, "");
        }
        private void buttonCapture_Click(object sender, EventArgs e)
        {
            string filePath = "";
            Bitmap bitMap = new Bitmap(800, 480);

            bitMap = IntechGraphics.CopyFromScreen();
            filePath = string.Format("{0}{1:yyyyMMdd_HHmm}_Capture_{2}.jpg", this.ParentForm.ParentForm.PathDataBackupFolder, DateTime.Now, this.ParentForm.ParentForm.SystemConfig.UsbID);
            bitMap.Save(filePath, ImageFormat.Jpeg);
        }
        private void buttonFileBackup_Click(object sender, EventArgs e)
        {
            int index = 0;
            bool fileCheck = false, directoryCheck = false;
            string fullFilePath = "";
            StringBuilder sb = new StringBuilder();

            fullFilePath = string.Format("{0}{1:yyyyMMdd}_CheckList.csv", this.ParentForm.ParentForm.PathDataBackupFolder, DateTime.Now);
            this.ParentForm.ParentForm.smartFileIO.FilePathName = fullFilePath;

            FileInfo fileInfo = new FileInfo(fullFilePath);
            fileCheck = fileInfo.Exists;

            if (fileCheck == true)
            {
                this.ParentForm.ParentForm.smartFileIO.Open(this.ParentForm.ParentForm.BufferSmartUart);

                for (int i = 0; i < 40; i++)
                {
                    this.ParentForm.ParentForm.smartFileIO.WriteString("");
                }
            }
            else
            {
                this.ParentForm.ParentForm.smartFileIO.Open(this.ParentForm.ParentForm.BufferSmartUart);
            }

            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("자동영점 시간,{0}", this.labelZeroParameterTime.Text), index);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("자동영점 범위,{0}", this.labelZeroParameterRange.Text), index++);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("자동영점 변량,{0}", this.labelZeroParameterVariate.Text), index++);

            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("통신설정 CH3-속도,{0}", this.labelCH3Baudrate.Text), index++);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("통신설정 CH3-모드,{0}", this.labelCH3Mode.Text), index++);

            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("기타옵션 채터링감지,{0}", this.labelChattering.Text), index++);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("기타옵션 부저ON시간,{0}", this.labelBuzzerOnTime.Text), index++);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("기타옵션 릴레이ON시간,{0}", this.labelRelayOnTime.Text), index++);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("기타옵션 소수점,{0}", this.labelDecimalPlaces.Text), index++);

            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("장비설정 장비열,{0}", this.labelEquipmentColumn.Text), index++);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("장비설정 중량표시,{0}", this.labelWeightView.Text), index++);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("장비설정 개별NG,{0}", this.labelIndividualNG.Text), index++);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("장비설정 언어,{0}", this.labelLanguage.Text), index++);

            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("판정설정 필터,{0}", this.labelFilter.Text), index++);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("판정설정 판정개수,{0}", this.labelJudgmentCount.Text), index++);

            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("선별기설정 동작시간,{0}", this.labelSorterRunTime.Text), index++);

            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("중량조정 분동중량,{0}", this.labelBalanceWeight.Text), index++);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("중량조정 최대중량,{0}", this.labelMaxWeight.Text), index++);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("중량조정 한눈의값,{0}", this.labelDigit.Text), index++);

            this.ParentForm.ParentForm.smartFileIO.Close();
        }
        #endregion
    }
}
