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

using INT69DB_2A.Forms;

namespace INT69DB_2A.DialogForms
{
    public partial class DialogFormDataStatistics : Form
    {
        #region Field
        private FormDataStatistics m_ParentForm;
        #endregion
        
        #region Constructor
        public DialogFormDataStatistics(FormDataStatistics parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

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

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

        #region Method
        private void InitializeDesign()
        { 
        }
        private void InitializeContnrol()
        {
            int x = 0, y = 0;

            x = Screen.PrimaryScreen.Bounds.Width / 2 - this.Size.Width / 2;
            y = Screen.PrimaryScreen.Bounds.Height / 2 - this.Size.Height / 2;

            this.Location = new Point(x, y);

            this.Size = new Size(450, 165);
        }
        private void DefaultSetting()
        {
            this.DisplayTreeViewUpdate();

            this.labelNodeIndex.Text = "-";
            this.labelFileName.Text = "-";
            this.labelDate.Text = "-";
            this.labelProductNo.Text = "-";
            this.labelId.Text = "-";
        }

        private Collection<List<string>> FileClassification(List<string> files)
        {
            string date = "", date1 = "";
            Collection<List<string>> fileClassification = new Collection<List<string>>();
            List<string> list = new List<string>();

            for (int i = 0; i < files.Count; i++)
            {
                date = files[i].Substring(0, 12);
                if (i == 0)
                    date1 = date;

                if (date == date1)
                {
                    list.Add(files[i]);
                }
                else
                {
                    date1 = date;

                    List<string> l = new List<string>();

                    foreach (string s in list)
                        l.Add(s);

                    fileClassification.Add(l);
                    list = new List<string>();
                    list.Add(files[i]);
                }
            }

            List<string> ls = new List<string>();
            foreach (string s in list)
                ls.Add(s);
            fileClassification.Add(ls);

            return fileClassification;
        }
        private List<string> FileSort(FileInfo[] files)
        {
            List<string> listFiles = new List<string>();
            Dictionary<string, int> fileNames = new Dictionary<string, int>();
            Dictionary<string, int> fileNamesSort = new Dictionary<string, int>();

            foreach (FileInfo file in files)
            {
                if (file.Name.StartsWith("20") == true)
                    fileNames.Add(file.Name, int.Parse(file.Name.Substring(11, 1)));
            }

            var vrList = fileNames.Keys.ToList();
            vrList.Sort();

            foreach (var v in vrList)
                fileNamesSort.Add(v, fileNames[v]);

            foreach (var v in fileNamesSort)
                listFiles.Add(v.Key);

            return listFiles;
        }
        private List<string> FileSort(List<string> lst)
        {
            List<string> listFiles = new List<string>();
            Dictionary<string, int> fileNames = new Dictionary<string, int>();
            Dictionary<string, int> fileNamesSort = new Dictionary<string, int>();

            foreach (string list in lst)
            {
                if (list.Length == 22)
                    fileNames.Add(list, int.Parse(list.Substring(13, 1)));
                else
                    fileNames.Add(list, int.Parse(list.Substring(13, 2)));
            }

            // Lane 순서데로 오름차순 정렬
            var vrList = fileNames.OrderBy(x => x.Value);

            foreach (var v in vrList)
                listFiles.Add(v.Key);

            return listFiles;
        }

        private void DisplayTreeViewUpdate()
        {
            int fileCount = 0;

            DirectoryInfo dir = new DirectoryInfo(this.ParentForm.ParentForm.PathDataBackupFolder);
            List<string> fileNames = new List<string>();

            // 폴더 체크
            if (dir.Exists == false)
                dir.Create();

            FileInfo[] files = dir.GetFiles();

            foreach (FileInfo file in files)
            {
                if (file.Name.StartsWith("20") == true)
                    fileCount++;
            }

            if (fileCount != 0)
            {
                // 파일 이름 순으로 정렬
                fileNames = this.FileSort(files);
                // 파일 날짜 순으로 분류
                this.ParentForm.CollectionFileClassification = this.FileClassification(fileNames);

                foreach (List<string> list in this.ParentForm.CollectionFileClassification)
                {
                    List<string> myList = new List<string>();

                    // Lane 순서데로 오름 차순 정렬
                    if (list.Count > 1 && list[0].Length > 18)
                        myList = this.FileSort(list);
                    else
                        myList = list;

                    TreeNode node = new TreeNode(myList[0].Substring(0, 12));

                    for (int i = 0; i < myList.Count; i++)
                    {
                        node.Nodes.Add(myList[i]);
                    }

                    this.treeView1.Nodes.Add(node);
                }
            }
        }

        public void DisplayRefresh()
        {
            this.DisplayTreeViewUpdate();
        }
        #endregion

        #region Event Handler
        private void buttonRead_Click(object sender, EventArgs e)
        {
            int ret = 0;

            if (this.ParentForm.SelectNodeIndex == -1)
                return;

            this.treeView1.Enabled = false;
            this.buttonRead.Enabled = false;
            this.buttonClose.Enabled = false;

            if (this.treeView1.Nodes.Count != 0)
            {
                this.ParentForm.ClearData();

                if (this.ParentForm.ParentForm.SystemConfig.EquipmentMode == 2)
                    ret = this.ParentForm.DataRead2();
                else
                    ret = this.ParentForm.DataRead1();

                if (ret == 0)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                    this.DialogResult = DialogResult.Cancel;
            }

            this.treeView1.Enabled = true;
            this.buttonRead.Enabled = true;
            this.buttonClose.Enabled = true;
        }
        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string fileName = "";

            if (e.Node.Parent == null)
            {
                fileName = e.Node.Text;
                this.ParentForm.SelectNodeIndex = e.Node.Index;
                //this.labelNodeIndex.Text = string.Format("{0}", this.ParentForm.SelectNodeIndex + 1);
            }
            else
            {
                fileName = e.Node.Parent.Text;
                this.ParentForm.SelectNodeIndex = e.Node.Parent.Index;
                //this.labelNodeIndex.Text = string.Format("{0}", this.ParentForm.SelectNodeIndex + 1);
            }

            this.labelFileName.Text = fileName;
            this.labelDate.Text = string.Format("{0}.{1}.{2}", fileName.Substring(0, 4), fileName.Substring(4, 2), fileName.Substring(6, 2));
            this.labelProductNo.Text = fileName.Substring(9, 1);
            this.labelId.Text = fileName.Substring(11, 1);
        }
        #endregion
    }
}