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_2H.DialogForms;
using ITC81DB_2H.Forms;
using ITC81DB_2H_ImageDll;
using ITC81DB_2H.Datastore;

namespace ITC81DB_2H.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 == Define.E_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 == Define.E_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 == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_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 == Define.E_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 if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            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 CreateScreenCapture(string path, DateTime time)
        {
            string filePath = "";
            Bitmap bitMap = new Bitmap(800, 600);

            bitMap = IntechGraphics.CopyFromScreen();
            filePath = string.Format("{0}S{1:yyyyMMdd-HHmmss}.jpg", path, time);

            bitMap.Save(filePath, ImageFormat.Jpeg);
        }
        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;

            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.DoubleEntryCount.ToString();
            if (this.labelDoubleEntryCount.Text != value)
                this.labelDoubleEntryCount.Text = value;

            value = data.TotalCount.ToString();
            if (this.labelTotalCount.Text != value)
                this.labelTotalCount.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
            
            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;
        }
        private void GetTrackingOthersFolderPath(ref string folder, ref string fileName)
        {
            DateTime time = DateTime.Now;

            folder = string.Format("{0}{1}\\{2}\\", this.ParentForm.ParentForm.PathDataOthersFolder, time.Year, time.Month);
            if (Directory.Exists(folder) == false)
                Directory.CreateDirectory(folder);
            fileName = string.Format("S{0:yyyyMMdd_HHmmss}.csv", time);
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = Define.E_DisplayStore.BasicDataStatistics;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_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)
        {
            try
            {
                string folderPath = "", fileName = "", fullFilePath = "";
                StreamWriter sw;

                this.Enabled = false;

                this.GetTrackingOthersFolderPath(ref folderPath, ref fileName);
                fullFilePath = string.Format("{0}{1}", folderPath, fileName);
                //Bitmap bitMap = new Bitmap(800, 480);

                //bitMap = IntechGraphics.CopyFromScreen();
                //filePath = string.Format("{0}{1:yyyyMMdd_HHmm}_Capture_{2}.jpg", this.ParentForm.ParentForm.PathDataOthersFolder, DateTime.Now, this.ParentForm.ParentForm.SystemConfig1.EquipmentID);
                //bitMap.Save(filePath, ImageFormat.Jpeg);

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

                //FormMain.Log(this.SelectedProductItem, this.SelectedWeightData);

                sw = new StreamWriter(fullFilePath, true, Encoding.UTF8);

                // ProductName
                sw.Write(this.SelectedProductItem.Name);
                sw.Write(",");
                // ProductNo
                sw.Write(this.SelectedProductItem.Number.ToString());
                sw.Write(",");
                // Lot
                sw.Write(this.SelectedProductItem.LotNo);
                sw.Write(",");
                // StartTime
                sw.Write(this.SelectedWeightData.StartTime.ToString("yyyy-MM-dd HH:mm:ss"));
                sw.Write(",");
                // EndTime
                sw.Write(this.SelectedWeightData.StopTime.ToString("yyyy-MM-dd HH:mm:ss"));
                sw.Write(",");

                // UnderRange
                sw.Write(Helper.StringToDecimalPlaces(this.SelectedProductItem.UnderRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                sw.Write(",");
                // UnderCount
                sw.Write(this.SelectedWeightData.UnderCount);
                sw.Write(",");
                // UnderTotalWeight
                sw.Write(this.SelectedWeightData.UnderSumWeightKG);
                sw.Write(",");
                // UnderAverage
                sw.Write(Helper.DoubleToString(this.SelectedWeightData.UnderAverage, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                sw.Write(",");
                // UnderRatio
                sw.Write(this.SelectedWeightData.UnderRatio);
                sw.Write(",");

                // PassRange
                sw.Write(Helper.StringToDecimalPlaces(this.SelectedProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                sw.Write(",");
                // PassCount
                sw.Write(this.SelectedWeightData.PassCount);
                sw.Write(",");
                // PassTotalWeight
                sw.Write(this.SelectedWeightData.PassSumWeightKG);
                sw.Write(",");
                // PassAverage
                sw.Write(Helper.DoubleToString(this.SelectedWeightData.PassAverage, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                sw.Write(",");
                // PassRatio
                sw.Write(this.SelectedWeightData.PassRatio);
                sw.Write(",");

                // OverRange
                sw.Write(Helper.StringToDecimalPlaces(this.SelectedProductItem.OverRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                sw.Write(",");
                // OverCount
                sw.Write(this.SelectedWeightData.OverCount);
                sw.Write(",");
                // OverTotalWeight
                sw.Write(this.SelectedWeightData.OverSumWeightKG);
                sw.Write(",");
                // OverAverage
                sw.Write(Helper.DoubleToString(this.SelectedWeightData.OverAverage, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                sw.Write(",");
                // OverRatio
                sw.Write(this.SelectedWeightData.OverRatio);
                sw.Write(",");

                // TareWeight
                sw.Write(Helper.StringToDecimalPlaces(this.SelectedProductItem.TareRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                sw.Write(",");

                // NG Count(Under + Over)
                sw.Write(this.SelectedWeightData.TotalUnderOverCount);
                sw.Write(",");
                // 기타불량 Count(ExNG)
                sw.Write(this.SelectedWeightData.ExNGCount);
                sw.Write(",");
                // Total Count
                sw.Write(this.SelectedWeightData.TotalCount);
                sw.WriteLine();

                sw.Close();

                splash.Finish();

                this.Enabled = true;
            }
            catch (Exception ex)
            {
                FormMain.Exception(ex);
            }
        }

        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
    }
}
