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_6CH.Forms;
using INT63DC_6CH.DialogForms;

namespace INT63DC_6CH.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.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.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);
        }
     
        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
                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;

            // 채터링
            sValue = item.Chattering.ToString();
            if (this.labelChattering.Text != sValue)
                this.labelChattering.Text = sValue;

            // 부저ON시간
            sValue = item.BuzzerRunTime.ToString();
            if (this.labelBuzzerOnTime.Text != sValue)
                this.labelBuzzerOnTime.Text = sValue;

            // 릴레이ON시간
            sValue = item.RelayRunTime.ToString();
            if (this.labelRelayOnTime.Text != sValue)
                this.labelRelayOnTime.Text = sValue;
        }
        private void UpdateSystemSettingDisplay(JudgmentSetItem item)
        {
            string 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 DisplayRefresh()
        {
            this.InitializeControl();
        }
        #endregion

        #region Event Handler
        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 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);
        }
        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.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.labelJudgmentCount.Text), index++);

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

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