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

using SmartX;

using ITC81DB_0H.DialogForms;
using ITC81DB_0H.Forms;
using ITC81DB_2H_ImageDll;

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

        private int SelectedProductNo;
        private WeightData SelectedWeightData;
        private ProductItem SelectedProductItem;

        private Collection<SmartLabel> CollectionNormalDistributionLabel;
        private Collection<SmartProgressBar> CollectionNormalDistributionProgressBar;
        #endregion

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

            this.ParentForm = parent;

            this.InitializeDesign();
            this.DefaultSetting();
        }
        #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 == DataStore.LanguageID.English)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundDataStatistics));

                this.buttonItemNo.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engDataStatNoDisable));
                this.buttonItemNo.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engDataStatNoDown));
                this.buttonItemNo.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engDataStatNoUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundDataStatistics));

                this.buttonItemNo.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormDataStatNoDisable));
                this.buttonItemNo.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormDataStatNoDown));
                this.buttonItemNo.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormDataStatNoUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundDataStatistics));

                this.buttonItemNo.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engDataStatNoDisable));
                this.buttonItemNo.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engDataStatNoDown));
                this.buttonItemNo.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engDataStatNoUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundDataStatistics));

                this.buttonItemNo.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerDataStatNoDisable));
                this.buttonItemNo.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerDataStatNoDown));
                this.buttonItemNo.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerDataStatNoUp));
            }
            else
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundDataStatistics));

                this.buttonItemNo.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engDataStatNoDisable));
                this.buttonItemNo.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engDataStatNoDown));
                this.buttonItemNo.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engDataStatNoUp));
            }
        }
        private void DefaultSetting()
        {
            this.SelectedProductNo = 1;
            this.SelectedWeightData = new WeightData();
            this.SelectedProductItem = new ProductItem();

            this.CollectionNormalDistributionLabel = new Collection<SmartLabel>();
            this.CollectionNormalDistributionLabel.Clear();
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange1);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange2);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange3);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange4);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange5);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange6);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange7);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange8);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange9);

            this.CollectionNormalDistributionProgressBar = new Collection<SmartProgressBar>();
            this.CollectionNormalDistributionProgressBar.Clear();
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar1);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar2);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar3);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar4);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar5);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar6);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar7);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar8);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar9);
        }

        private void UpdateDisplay(WeightData data, ProductItem item)
        {
            string value = "";

            #region Range
            value = Helper.StringToDecimalPlaces(item.UnderRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelUnderRange.Text != value)
                this.labelUnderRange.Text = value;

            value = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelPassRange.Text != value)
                this.labelPassRange.Text = value;

            value = Helper.StringToDecimalPlaces(item.OverRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelOverRange.Text != value)
                this.labelOverRange.Text = value;
            #endregion

            #region Count
            value = data.UnderCount.ToString();
            if (this.labelUnderCount.Text != value)
                this.labelUnderCount.Text = value;

            value = data.PassCount.ToString();
            if (this.labelPassCount.Text != value)
                this.labelPassCount.Text = value;

            value = data.OverCount.ToString();
            if (this.labelOverCount.Text != value)
                this.labelOverCount.Text = value;
            #endregion

            #region Average
            value = Helper.DoubleToString(data.UnderAverage, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelUnderAverage.Text != value)
                this.labelUnderAverage.Text = value;

            value = Helper.DoubleToString(data.PassAverage, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelPassAverage.Text != value)
                this.labelPassAverage.Text = value;

            value = Helper.DoubleToString(data.OverAverage, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelOverAverage.Text != value)
                this.labelOverAverage.Text = value;
            #endregion

            #region Sum Weight
            value = data.UnderSumWeightKG;
            if (this.labelUnderSumWeight.Text != value)
                this.labelUnderSumWeight.Text = value;

            value = data.PassSumWeightKG;
            if (this.labelPassSumWeight.Text != value)
                this.labelPassSumWeight.Text = value;

            value = data.OverSumWeightKG;
            if (this.labelOverSumWeight.Text != value)
                this.labelOverSumWeight.Text = value;
            #endregion

            #region Ratio
            value = data.UnderRatio;
            if (this.labelUnderRatio.Text != value)
                this.labelUnderRatio.Text = value;

            value = data.PassRatio;
            if (this.labelPassRatio.Text != value)
                this.labelPassRatio.Text = value;

            value = data.OverRatio;
            if (this.labelOverRatio.Text != value)
                this.labelOverRatio.Text = value;
            #endregion

            #region NormalDistribution
            for (int i = 0; i < item.CollectionNormalDistributionViewRange.Count; i++)
            {
                value = Helper.StringToDecimalPlaces(item.CollectionNormalDistributionViewRange[i].ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                if (this.CollectionNormalDistributionLabel[i].Text != value)
                    this.CollectionNormalDistributionLabel[i].Text = value;
            }

            for (int i = 0; i < this.CollectionNormalDistributionProgressBar.Count; i++)
            {
                if (this.CollectionNormalDistributionProgressBar[i].Maximum != data.TotalCount)
                    this.CollectionNormalDistributionProgressBar[i].Maximum = data.TotalCount;

                if(this.CollectionNormalDistributionProgressBar[i].Value != data.CollectionNormalDistributionCount[i])
                    this.CollectionNormalDistributionProgressBar[i].Value = data.CollectionNormalDistributionCount[i];
            }
            #endregion

            value = data.TotalUnderOverCount.ToString();
            if (this.labelNGCount.Text != value)
                this.labelNGCount.Text = value;

            value = data.ExNGCount.ToString();
            if (this.labelExNGCount.Text != value)
                this.labelExNGCount.Text = value;

            value = data.TotalCount.ToString();
            if (this.labelTotalCount.Text != value)
                this.labelTotalCount.Text = value;

            if (data.StartTime.Year == 1111)
            {
                data.StartTime = DateTime.Now;
                this.ParentForm.ParentForm.CurrentWeightData.StartTime = data.StartTime;
                this.ParentForm.ParentForm.SaveCounterFile(this.ParentForm.ParentForm.CurrentWeightData, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
            }
            if (data.StopTime.Year == 1111)
            {
                data.StopTime = DateTime.Now;
                this.ParentForm.ParentForm.CurrentWeightData.StopTime = data.StopTime;
                this.ParentForm.ParentForm.SaveCounterFile(this.ParentForm.ParentForm.CurrentWeightData, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
            }

            value = data.StartTime.ToString("yyyy-MM-dd HH:mm:ss");
            if (this.labelStartTime.Text != value)
                this.labelStartTime.Text = value;

            value = data.StopTime.ToString("yyyy-MM-dd HH:mm:ss");
            if (this.labelStopTime.Text != value)
                this.labelStopTime.Text = value;
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.BasicDataStatistics;
            this.ParentForm.ParentForm.SetDisplayMode(DataStore.DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.SelectedProductNo = this.ParentForm.ParentForm.SystemConfig1.ProductNumber;
            this.buttonPrint.Visible = this.ParentForm.ParentForm.SystemConfig1.IsPrinterEnable;

            if (this.buttonItemNo.Text != this.SelectedProductNo.ToString())
                this.buttonItemNo.Text = this.SelectedProductNo.ToString();

            this.ParentForm.ParentForm.LoadCounterFile(ref this.SelectedWeightData, this.SelectedProductNo - 1);
            this.ParentForm.ParentForm.LoadProductFile(ref this.SelectedProductItem, this.SelectedProductNo - 1);
            this.UpdateDisplay(this.SelectedWeightData, this.SelectedProductItem);
        }
        #endregion

        #region Event Handler
        private void buttonItemNo_Click(object sender, EventArgs e)
        {
            string message = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonItemNo.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1 || myKeyPad.doubleValue > this.ParentForm.ParentForm.ProductCount)
                {
                    // 입력범위를 확인하세요
                    message = string.Format("1 ~ {0}", this.ParentForm.ParentForm.ProductCount);
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.buttonItemNo.Text = myKeyPad.StringValue;
                    this.SelectedProductNo = myKeyPad.IntValue;

                    this.ParentForm.ParentForm.LoadCounterFile(ref this.SelectedWeightData, this.SelectedProductNo - 1);
                    this.ParentForm.ParentForm.LoadProductFile(ref this.SelectedProductItem, this.SelectedProductNo - 1);
                    this.UpdateDisplay(this.SelectedWeightData, this.SelectedProductItem);
                }
            }
        }

        private void buttonBackup_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.SystemConfig1.UsbID);
            bitMap.Save(filePath, ImageFormat.Jpeg);

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

            filePath = string.Format("{0}{1:yyyyMMdd_HHmm}_Data_{2}.csv", this.ParentForm.ParentForm.PathDataBackupFolder, DateTime.Now, this.ParentForm.ParentForm.SystemConfig1.UsbID);
            this.ParentForm.ParentForm.smartFileIO.FilePathName = filePath;
            if (File.Exists(filePath) == false)
            {
                byte[] dd = new byte[3] { 0xEF, 0xBB, 0xBF }; //byte-order mark : UTF-8
                FileStream stm = new FileStream(this.ParentForm.ParentForm.smartFileIO.FilePathName, FileMode.Create);
                stm.Write(dd, 0, 3);
                stm.Close();
            }

            this.ParentForm.ParentForm.smartFileIO.Open(this.ParentForm.ParentForm.BufferSmartUart);
            this.ParentForm.ParentForm.smartFileIO.WriteString("##############################");
            this.ParentForm.ParentForm.smartFileIO.WriteString("          TOTAL  PRINT        ");
            this.ParentForm.ParentForm.smartFileIO.WriteString("##############################");
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" PART    = {0}", this.SelectedProductItem.Number));
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" LOT     = {0}", this.SelectedProductItem.LotNo));
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" START   = {0}", this.SelectedWeightData.StartTime.ToString("yyyy-MM-dd HH:mm:ss")));
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" END     = {0}", this.SelectedWeightData.StopTime.ToString("yyyy-MM-dd HH:mm:ss")));

            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("\n UNDER   = {0}",
                Helper.StringToDecimalPlaces(this.SelectedProductItem.UnderRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces)) + this.ParentForm.ParentForm.SystemConfig1.Unit);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" COUNT   = {0}", this.SelectedWeightData.UnderCount));
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" TOTAL.W = {0}kg", this.SelectedWeightData.UnderSumWeightKG));
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" AVG     = {0}",
                Helper.DoubleToString(this.SelectedWeightData.UnderAverage, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces)) + this.ParentForm.ParentForm.SystemConfig1.Unit);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" RATIO   = {0}%", this.SelectedWeightData.UnderRatio));

            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("\n PASS    = {0}",
              Helper.StringToDecimalPlaces(this.SelectedProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces)) + this.ParentForm.ParentForm.SystemConfig1.Unit);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" COUNT   = {0}", this.SelectedWeightData.PassCount));
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" TOTAL.W = {0}kg", this.SelectedWeightData.PassSumWeightKG));
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" AVG     = {0}",
                Helper.DoubleToString(this.SelectedWeightData.PassAverage, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces)) + this.ParentForm.ParentForm.SystemConfig1.Unit);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" RATIO   = {0}%", this.SelectedWeightData.PassRatio));

            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("\n OVER    = {0}",
              Helper.StringToDecimalPlaces(this.SelectedProductItem.OverRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces)) + this.ParentForm.ParentForm.SystemConfig1.Unit);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" COUNT   = {0}", this.SelectedWeightData.OverCount));
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" TOTAL.W = {0}kg", this.SelectedWeightData.OverSumWeightKG));
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" AVG     = {0}",
                Helper.DoubleToString(this.SelectedWeightData.OverAverage, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces)) + this.ParentForm.ParentForm.SystemConfig1.Unit);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" RATIO   = {0}%", this.SelectedWeightData.OverRatio));

            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("\n TARE    = {0}",
                Helper.StringToDecimalPlaces(this.SelectedProductItem.TareRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces)) + this.ParentForm.ParentForm.SystemConfig1.Unit);
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format("\n Faulty  = {0}", this.SelectedWeightData.TotalUnderOverCount));
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" Other.F = {0}", this.SelectedWeightData.ExNGCount));
            this.ParentForm.ParentForm.smartFileIO.WriteString(" TOTAL");
            this.ParentForm.ParentForm.smartFileIO.WriteString(string.Format(" COUNT   = {0}", this.SelectedWeightData.TotalCount));

            this.ParentForm.ParentForm.smartFileIO.Close();

            splash.Finish();
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.buttonPrint.Enabled = false;
            this.buttonClear.Enabled = false;
            this.timerPrinting.Interval = 2700;
            this.timerPrinting.Enabled = true;

            if(this.ParentForm.ParentForm.SystemConfig1.StatisticsPrintFormat == 1)
                this.ParentForm.ParentForm.PrintingWeight2(this.SelectedWeightData, this.SelectedProductItem);
            else
                this.ParentForm.ParentForm.PrintingWeight(this.SelectedWeightData, this.SelectedProductItem);
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 8);
            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                this.ParentForm.ParentForm.ChildFormMainDisplay.ClearCurrentData();

                this.ParentForm.ParentForm.LoadCounterFile(ref this.SelectedWeightData, this.SelectedProductNo - 1);
                this.ParentForm.ParentForm.LoadProductFile(ref this.SelectedProductItem, this.SelectedProductNo - 1);
                this.UpdateDisplay(this.SelectedWeightData, this.SelectedProductItem);
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.buttonPrint.Enabled = true;
            this.buttonClear.Enabled = true;
            this.timerPrinting.Enabled = false;
        }
        #endregion
    }
}