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

using SmartX;

using ITC81DB_2H.Forms;
using ITC81DB_2H.DialogForms;
using ITC81DB_2H_ImageDll;
using ITC81DB_2H.Datastore;

namespace ITC81DB_2H.Controls
{
    public partial class ControlCenterLogOthers : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
        private int FileIndex;
        private int TotalCount;
        private int CurrentCount;
        private static int ListBoxLineCount = 16; // 화면에 보여지는 listBox 줄 수
        public bool IsFileOpen;

        private string SelectedNodeFullPath;
        private List<DataBackupYear> ListOthersFile;
        private Collection<string> CollectionData;            // 파일에서 읽은 데이터
        #endregion

        #region Constructor
        public ControlCenterLogOthers(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()
        {
            if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.labelTitleFileList.Text = "File List";
                this.labelTitleFileName.Text = "File name";
                this.labelTitleFind.Text = "Search line";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.labelTitleFileList.Text = "档案清单";
                this.labelTitleFileName.Text = "文件名";
                this.labelTitleFind.Text = "搜索行";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
                this.labelTitleFileList.Text = "File List";
                this.labelTitleFileName.Text = "File name";
                this.labelTitleFind.Text = "Vyhledávací řádek";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
                this.labelTitleFileList.Text = "Список файлов";
                this.labelTitleFileName.Text = "Имя файла";
                this.labelTitleFind.Text = "Строка поиска";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.labelTitleFileList.Text = "Dateiliste";
                this.labelTitleFileName.Text = "Dateinamen";
                this.labelTitleFind.Text = "Suchzeile";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {
                this.labelTitleFileList.Text = "Lista de archivos";
                this.labelTitleFileName.Text = "Nombre del archivo";
                this.labelTitleFind.Text = "Línea de búsqueda";
            }
            else
            {
                this.labelTitleFileList.Text = "파일 목록";
                this.labelTitleFileName.Text = "파일 이름";
                this.labelTitleFind.Text = "데이터 검색(Line)";
            }
        }
        private void DefaultSetting()
        {
            this.FileIndex = 0;
            this.TotalCount = 0;
            this.IsFileOpen = false;

            this.ListOthersFile = new List<DataBackupYear>();
            this.CollectionData = new Collection<string>();
        }
        private void UpdateInitialStatus()
        {
            this.labelFileName.Text = "";
            this.labelCurrentIndex.Text = "0";
            this.labelTotalIndex.Text = "0";
            this.labelFind.Text = "";

            this.ClearData();
            this.ClearListBox();
        }
        private void UpdateFileList()
        {
            int fileCount = 0;
            //TreeNode node;
            List<string> years = new List<string>();
            List<string> months = new List<string>();
            List<string> days = new List<string>();

            this.treeView.Nodes.Clear();
            this.ListOthersFile.Clear();

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

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

            // year 폴더 가져오기
            DirectoryInfo[] yearDirectorys = dir.GetDirectories();
            // year 폴더 정렬 
            years = this.ParentForm.DirectorySort(yearDirectorys);

            // File 리스트 생성
            if (years.Count != 0)
            {
                // Year
                foreach (string year in years)
                {
                    DataBackupYear y = new DataBackupYear(year);
                    DirectoryInfo monthDir = new DirectoryInfo(string.Format("{0}{1}", this.ParentForm.ParentForm.PathDataOthersFolder, year));
                    DirectoryInfo[] monthDirectorys = monthDir.GetDirectories();

                    months = this.ParentForm.DirectorySort(monthDirectorys);

                    if (months.Count != 0)
                    {
                        // Month
                        foreach (string month in months)
                        {
                            DirectoryInfo dayDir = new DirectoryInfo(string.Format("{0}{1}\\{2}", this.ParentForm.ParentForm.PathDataOthersFolder, year, month));
                            FileInfo[] dayFiles = dayDir.GetFiles();

                            days = this.DayOthersDirectorySort(dayFiles);

                            DataBackupMonth m = new DataBackupMonth(month);
                            m.Days = days;

                            y.Months.Add(m);
                        }

                        this.ListOthersFile.Add(y);
                    }
                    else
                    {
                        this.ListOthersFile.Add(y);
                    }
                }

                // node 생성
                for (int i = 0; i < this.ListOthersFile.Count; i++)
                {
                    TreeNode node = new TreeNode(this.ListOthersFile[i].Year);
                    for (int j = 0; j < this.ListOthersFile[i].Months.Count; j++)
                    {
                        TreeNode nodeMonth = new TreeNode(this.ListOthersFile[i].Months[j].Month);

                        for (int k = 0; k < this.ListOthersFile[i].Months[j].Days.Count; k++)
                        {
                            nodeMonth.Nodes.Add(this.ListOthersFile[i].Months[j].Days[k]);
                            fileCount++;
                        }
                        node.Nodes.Add(nodeMonth);
                    }
                    this.treeView.Nodes.Add(node);
                }
            }

            this.labelCount.Text = fileCount.ToString();
        }

        private List<string> DayOthersDirectorySort(FileInfo[] files)
        {
            List<string> listFile = new List<string>();
            Dictionary<string, int> dirNames = new Dictionary<string, int>();
            Dictionary<string, int> dirNamesSort = new Dictionary<string, int>();

            foreach (FileInfo file in files)
            {
                if (file.Name.StartsWith("S") == true)
                    dirNames.Add(file.Name, int.Parse(file.Name.Substring(1, 8)));
            }

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

            foreach (var v in vrList)
                dirNamesSort.Add(v, dirNames[v]);

            foreach (var v in dirNamesSort)
                listFile.Add(v.Key);

            return listFile;
        }
        private void FileCopy(FileInfo file, string fileName)
        {
            try
            {
                if (this.FileIndex == 0)
                {
                    file.CopyTo(fileName);
                    this.FileIndex = 0;
                }
                else
                {
                    file.CopyTo(fileName.Insert(fileName.Length - 4, "_" + this.FileIndex.ToString()));
                    this.FileIndex = 0;
                }
            }
            catch
            {
                this.FileIndex++;
                this.FileCopy(file, fileName);
            }
        }

        private void SearchLineEnabled(bool bValue)
        {
            if (bValue == false)
                this.labelFind.BackColor = Color.Gray;
            else
                this.labelFind.BackColor = Color.White;

            this.labelFind.Enabled = bValue;
            this.buttonFind.Enabled = bValue;
        }

        public void ClearData()
        {
            this.CollectionData.Clear();
        }
        public void ClearListBox()
        {
            this.listBoxLog.ClearAll();
        }
        
        private void ListBoxScrollUp(int value)
        {
            if (this.CurrentCount == ListBoxLineCount || (this.CurrentCount < ListBoxLineCount && this.CurrentCount == this.TotalCount)
                || this.listBoxLog.ItemCount == 0)
                return;

            int startIndex = 0, endIndex = 0;

            this.ClearData();
            this.ClearListBox();

            if (this.CurrentCount - value <= ListBoxLineCount)
            {
                startIndex = 0;
                endIndex = ListBoxLineCount;

                this.CurrentCount = ListBoxLineCount;
            }
            else
            {
                this.CurrentCount = this.CurrentCount - value;

                startIndex = this.CurrentCount - ListBoxLineCount;
                endIndex = this.CurrentCount;
            }

            this.UpdateData(startIndex, endIndex);
        }
        private void ListBoxScrollDown(int value)
        {
            if (this.TotalCount == this.CurrentCount || this.listBoxLog.ItemCount == 0)
                return;

            int startIndex = 0, endIndex = 0;

            this.ClearData();
            this.ClearListBox();

            if (this.CurrentCount + value >= this.TotalCount)
            {
                startIndex = this.TotalCount - ListBoxLineCount + 1;
                endIndex = this.TotalCount + 1;

                this.CurrentCount = this.TotalCount;
            }
            else
            {
                this.CurrentCount = this.CurrentCount + value;

                startIndex = this.CurrentCount - ListBoxLineCount;
                endIndex = this.CurrentCount;
            }

            this.UpdateData(startIndex, endIndex);
        }

        private void UpdateData(int startIndex, int endIndex)
        {
            for (int i = startIndex; i < endIndex; i++)
                this.CollectionData.Add(this.smartFile1.ReadStringBuffer(i));

            this.UpdateListBoxDataDisplay();
            this.UpdateListBoxCount();
        }
        private void UpdateListBoxDataDisplay()
        {
            for (int i = 0; i < this.CollectionData.Count; i++)
                this.listBoxLog.AddItem(this.CollectionData[i]);
        }
        private void UpdateListBoxCount()
        {
            this.labelTotalIndex.Text = this.TotalCount.ToString();
            this.labelCurrentIndex.Text = this.CurrentCount.ToString();
        }
     
        public void LoadDatas(string fullFilePath)
        {
            // 맨 끝 ListBoxLineCount 만큼의 데이터 Load

            int startIndex = 0, endIndex = 0;

            try
            {
                this.ClearData();
                this.ClearListBox();

                this.ParentForm.SplashStart();

                this.smartFile1.FilePathName = fullFilePath;
                if (this.smartFile1.Open(2000000) == true)
                    this.IsFileOpen = true;
                bool bValue = this.smartFile1.ReadStringAllBuffer();
                if (bValue == false)
                {
                    this.smartFile1.Close();
                    this.IsFileOpen = false;
                    return;
                }

                long lineNum = this.smartFile1.GetCount();
                this.TotalCount = (int)lineNum;

                if (this.TotalCount > ListBoxLineCount)
                {
                    startIndex = this.TotalCount - ListBoxLineCount;
                    endIndex = this.TotalCount;
                }
                else
                {
                    startIndex = 0; // 맨 첫줄부터 표시
                    endIndex = this.TotalCount;
                }

                this.CurrentCount = this.TotalCount;

                if (this.TotalCount < ListBoxLineCount)
                    this.SearchLineEnabled(false);
                else
                    this.SearchLineEnabled(true);

                this.labelFind.Text = this.CurrentCount.ToString();
                this.UpdateData(startIndex, endIndex);

                // File Name
                string[] data = fullFilePath.Split('\\');
                this.labelFileName.Text = data[data.Length - 1];
                
                this.ParentForm.SplashFinish();

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

                DialogFormMessage myMsg = new DialogFormMessage(16, this.ParentForm.ParentForm.SystemConfig1.Language);
                myMsg.ShowDialog();
                this.ParentForm.SplashFinish();
                this.smartFile1.Close();
                this.IsFileOpen = false;
                this.Enabled = true;
            }
        }
        public void FileClose()
        {
            if (this.IsFileOpen == true)
            {
                this.ParentForm.ParentForm.IsLogProcessing = false;

                this.smartFile1.Close();
                this.IsFileOpen = false;

                this.ClearData();
                this.ClearListBox();

                this.labelFileName.Text = "";
                this.labelCurrentIndex.Text = "0";
                this.labelTotalIndex.Text = "0";
                this.labelFind.Text = "";
            }
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = Define.E_DisplayStore.LogOthers;
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_DisplayMode.Menu);

            this.UpdateInitialStatus();
            this.UpdateFileList();
            this.treeView.ExpandAll();
            this.TotalCount = 0;
            this.SelectedNodeFullPath = "";

            if (int.Parse(this.labelCount.Text) == 0)
            {
                DialogFormMessage msg = new DialogFormMessage(37, this.ParentForm.ParentForm.SystemConfig1.Language);
                msg.ShowDialog();
                return;
            }
        }
        #endregion

        #region Event Handler
        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Text.Substring(0, 1) == "S")
            {
                if (e.Node.Checked == true)
                    e.Node.Checked = false;
                else
                    e.Node.Checked = true;

                //this.labelFileName.Text = e.Node.Text;
                this.SelectedNodeFullPath = this.treeView.SelectedNode.FullPath;
            }
            //else
            //{
            //    var node = e.Node;
            //    int count = node.Nodes.Count;
                
            //    for (int i = 0; i < count; i++)
            //        node.Nodes[i].Checked = node.Checked;
            //}
        }
        private void buttonRead_Click(object sender, EventArgs e)
        {
            if (this.SelectedNodeFullPath == "" || this.SelectedNodeFullPath == null)
                return;

            this.FileClose();
            this.ParentForm.ParentForm.IsLogProcessing = true;

            this.Enabled = false;

            this.LoadDatas(this.ParentForm.ParentForm.PathDataOthersFolder + this.SelectedNodeFullPath);
        }

        private void labelFind_Click(object sender, EventArgs e)
        {
            if (this.listBoxLog.ItemCount == 0)
                return;

            string value = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelFind.Text, 6, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();
                }
                else if (myKeyPad.doubleValue > this.TotalCount)
                {
                    this.labelFind.Text = this.TotalCount.ToString();
                }
                else
                {
                    this.labelFind.Text = myKeyPad.StringValue;

                    if (this.labelFind.Text == "" || this.labelFind.Text == null || this.listBoxLog.ItemCount == 0)
                        return;

                    int startIndex = 0, endIndex = 0;
                    int index = int.Parse(this.labelFind.Text);

                    try
                    {
                        this.ClearData();
                        this.ClearListBox();

                        if (this.TotalCount > ListBoxLineCount)
                        {
                            if (index - ListBoxLineCount < 0)
                            {
                                startIndex = 0 + 1;
                                endIndex = ListBoxLineCount + 1;

                                this.CurrentCount = ListBoxLineCount;
                            }
                            else
                            {
                                startIndex = index - ListBoxLineCount;
                                endIndex = index;

                                this.CurrentCount = index;
                            }
                        }
                        else
                        {
                            startIndex = 0 + 1;
                            endIndex = this.TotalCount + 1;

                            this.CurrentCount = this.TotalCount;
                        }

                        this.UpdateData(startIndex, endIndex);
                    }
                    catch
                    {
                        DialogFormMessage myMsg = new DialogFormMessage(16, this.ParentForm.ParentForm.SystemConfig1.Language);
                        myMsg.ShowDialog();
                    }
                }
            }
        }
        private void buttonFind_Click(object sender, EventArgs e)
        {
            if (this.labelFind.Text == "" || this.labelFind.Text == null || this.listBoxLog.ItemCount == 0)
                return;

            int startIndex = 0, endIndex = 0;
            int index = int.Parse(this.labelFind.Text);

            try
            {
                this.ClearData();
                this.ClearListBox();

                if (this.TotalCount > ListBoxLineCount)
                {
                    if (index - ListBoxLineCount < 0)
                    {
                        startIndex = 0 + 1;
                        endIndex = ListBoxLineCount + 1;

                        this.CurrentCount = ListBoxLineCount;
                    }
                    else
                    {
                        startIndex = index - ListBoxLineCount;
                        endIndex = index;

                        this.CurrentCount = index;
                    }
                }
                else
                {
                    startIndex = 0 + 1;
                    endIndex = this.TotalCount + 1;

                    this.CurrentCount = this.TotalCount;
                }

                this.UpdateData(startIndex, endIndex);
            }
            catch
            {
                DialogFormMessage myMsg = new DialogFormMessage(16, this.ParentForm.ParentForm.SystemConfig1.Language);
                myMsg.ShowDialog();
            }
        }

        private void buttonBackup_Click(object sender, EventArgs e)
        {
            this.FileClose();
            this.ParentForm.Backup(Define.E_DataType.Others, this.treeView);

            if (this.ParentForm.ParentForm.SystemConfig3.IsPart11 == false)
            {
                this.UpdateFileList();
                this.treeView.ExpandAll();
            }
        }

        private void buttonUp_Click(object sender, EventArgs e)
        {
            this.ListBoxScrollUp(ListBoxLineCount);
        }
        private void buttonDown_Click(object sender, EventArgs e)
        {
            this.ListBoxScrollDown(ListBoxLineCount);
        }

        private void listBox_Click(object sender, EventArgs e)
        {
            SmartX.SmartListBox listBox = sender as SmartX.SmartListBox;

            listBox.SelectItemIndex = -1;
        }
        #endregion
    }
}
