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

using SmartX;
using INT69DC_ImageDll;
using INT69DC_7C.DialogForms;

namespace INT69DC_7C.Forms
{
    public partial class FormDataViewer : Form
    {
        #region Field
        private int m_SelectNodeIndex;
        private int RemainCNT;
        private int StartIndex;
        private int DataRemainCNT;
        private int DataAllCNT;
        private static int ListBoxLineCount = 30; // 화면에 보여지는 listBox 줄 수
        public bool IsFileOpen;
        public string PathDestinationFileName;
        private FormMain m_ParentForm;

        private Collection<string[]> CollectionHistoryData;            // 파일에서 읽은 데이터

        private List<string> ListTime;
        private List<string> ListLoginID;
        private List<string> ListEvent;
        private List<string> ListDetail;
        private List<string> ListBefore;
        private List<string> ListAfter;
        private List<string> ListType;
        #endregion

        #region Constructor
        public FormDataViewer(FormMain parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

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

        #region Property
        public int SelectNodeIndex
        {
            get { return this.m_SelectNodeIndex; }
            set { this.m_SelectNodeIndex = value; }
        }

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

        #region Method
        private void InitializeDesign()
        {
            ImageDll images = new ImageDll();

            if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
            {

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
                this.labelTitle.Text = "Data viewer";

                this.buttonSave.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engSaveDisable));
                this.buttonSave.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engSaveDown));
                this.buttonSave.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engSaveUp));
                this.buttonFilter.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engFilterDisable));
                this.buttonFilter.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engFilterDown));
                this.buttonFilter.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engFilterUp));
                this.buttonFileSelect.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engFileSelectDisable));
                this.buttonFileSelect.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engFileSelectDown));
                this.buttonFileSelect.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engFileSelectUp));
                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "数据查看器";

                this.buttonSave.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnSaveDisable));
                this.buttonSave.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnSaveDown));
                this.buttonSave.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnSaveUp));
                this.buttonFilter.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFilterDisable));
                this.buttonFilter.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFilterDown));
                this.buttonFilter.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFilterUp));
                this.buttonFileSelect.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFileSelectDisable));
                this.buttonFileSelect.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFileSelectDown));
                this.buttonFileSelect.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFileSelectUp));
                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
                this.labelTitle.Text = "Prohlížeč dat";

                this.buttonSave.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeSaveDisable));
                this.buttonSave.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeSaveDown));
                this.buttonSave.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeSaveUp));
                this.buttonFilter.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeFilterDisable));
                this.buttonFilter.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeFilterDown));
                this.buttonFilter.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeFilterUp));
                this.buttonFileSelect.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeFileSelectDisable));
                this.buttonFileSelect.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeFileSelectDown));
                this.buttonFileSelect.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeFileSelectUp));
                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.labelTitle.Text = "Datenbetrachter";

                this.buttonSave.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerSaveDisable));
                this.buttonSave.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerSaveDown));
                this.buttonSave.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerSaveUp));
                this.buttonFilter.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engFilterDisable));
                this.buttonFilter.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engFilterDown));
                this.buttonFilter.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engFilterUp));
                this.buttonFileSelect.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerFileSelectDisable));
                this.buttonFileSelect.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerFileSelectDown));
                this.buttonFileSelect.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerFileSelectUp));
                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.labelTitle.Text = "デㅡタビュㅡア";

                this.buttonSave.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnSaveDisable));
                this.buttonSave.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnSaveDown));
                this.buttonSave.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnSaveUp));
                this.buttonFilter.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFilterDisable));
                this.buttonFilter.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFilterDown));
                this.buttonFilter.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFilterUp));
                this.buttonFileSelect.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFileSelectDisable));
                this.buttonFileSelect.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFileSelectDown));
                this.buttonFileSelect.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFileSelectUp));
                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackUp));
            }
            else
            {

            }
        }
        private void InitializeControl()
        {
            this.ClearData();
        }
        private void DefaultSetting()
        {
            this.SelectNodeIndex = -1;
            this.StartIndex = 0;
            this.RemainCNT = 0;
            this.DataRemainCNT = 0;
            this.DataAllCNT = 0;
            this.IsFileOpen = false;

            this.PathDestinationFileName = "";
            this.CollectionHistoryData = new Collection<string[]>();
            //this.CollectionFileClassification = new Collection<List<string>>();

            this.ListTime = new List<string>();
            this.ListLoginID = new List<string>();
            this.ListEvent = new List<string>();
            this.ListDetail = new List<string>();
            this.ListBefore = new List<string>();
            this.ListAfter = new List<string>();
            this.ListType = new List<string>();
        }
       
        private void ListBoxScrollDown()
        {
            this.listBoxTime.ScrollDown();
            this.listBoxLoginID.ScrollDown();
            this.listBoxEvent.ScrollDown();
            this.listBoxDetail.ScrollDown();
            this.listBoxBefore.ScrollDown();
            this.listBoxAfter.ScrollDown();
            this.listBoxType.ScrollDown();

            this.ListBoxItemsCount();
        }
        private void ListBoxScrollDown(int value)
        {
            this.listBoxTime.ScrollDown(value);
            this.listBoxLoginID.ScrollDown(value);
            this.listBoxEvent.ScrollDown(value);
            this.listBoxDetail.ScrollDown(value);
            this.listBoxBefore.ScrollDown(value);
            this.listBoxAfter.ScrollDown(value);
            this.listBoxType.ScrollDown(value);

            this.ListBoxItemsCount();
        }
        private void ListBoxScrollUp()
        {
            this.listBoxTime.ScrollUp();
            this.listBoxLoginID.ScrollUp();
            this.listBoxEvent.ScrollUp();
            this.listBoxDetail.ScrollUp();
            this.listBoxBefore.ScrollUp();
            this.listBoxAfter.ScrollUp();
            this.listBoxType.ScrollUp();

            this.ListBoxItemsCount();
        }
        private void ListBoxScrollUp(int value)
        {
            this.listBoxTime.ScrollUp(value);
            this.listBoxLoginID.ScrollUp(value);
            this.listBoxEvent.ScrollUp(value);
            this.listBoxDetail.ScrollUp(value);
            this.listBoxBefore.ScrollUp(value);
            this.listBoxAfter.ScrollUp(value);
            this.listBoxType.ScrollUp(value);

            this.ListBoxItemsCount();
        }
        private void ListBoxItemsCount()
        {
            if (this.listBoxTime.ItemCount == 0)
            {
                this.StartIndex = 0;
                this.RemainCNT = 0;
                this.DataRemainCNT = 0;
                this.DataAllCNT = 0;
            }
            else
            {
                this.StartIndex = this.listBoxType.ViewStartItemIndex;
                this.RemainCNT = this.listBoxType.ViewRemainCount;
            }

            this.labelItemsCount.Text = this.StartIndex.ToString() + " / " + this.RemainCNT.ToString();
            this.labelRemainDataCount.Text = this.DataRemainCNT.ToString();

            if (this.listBoxTime.ItemCount == 0)
                this.labelItemCount.Text = "0 / 0";
            else
                this.labelItemCount.Text = (this.StartIndex + 29).ToString() + " / " + this.DataAllCNT.ToString();
        }
        private void UpdateDisplay()
        {
            int cnt = 0;
            if (this.ListTime.Count > 50)
            {
                cnt = 50;
                this.DataRemainCNT = this.ListTime.Count - 50;
            }
            else
                cnt = this.ListTime.Count;

            for (int i = 0; i < cnt; i++)
            {
                this.listBoxTime.AddItem(this.ListTime[i]);
                this.listBoxLoginID.AddItem(this.ListLoginID[i]);
                this.listBoxEvent.AddItem(this.ListEvent[i]);
                this.listBoxDetail.AddItem(this.ListDetail[i]);
                this.listBoxBefore.AddItem(this.ListBefore[i]);
                this.listBoxAfter.AddItem(this.ListAfter[i]);
                this.listBoxType.AddItem(this.ListType[i]);
            }

            this.ListBoxItemsCount();
        }
        private void UpdateDisplayAddListBox()
        {
            int index = 0, range = 0;
            SmartX.SmartSplash splash;

            // 화면 표시 로딩시간 타이머 on
            this.smartTimer1.StartWatch();

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

            index = this.ListTime.Count - this.DataRemainCNT;

            if (this.DataRemainCNT > 50)
            {
                range = index + 50;
                for (int i = index; i < range; i++)
                {
                    this.listBoxTime.AddItem(this.ListTime[i]);
                    this.listBoxLoginID.AddItem(this.ListLoginID[i]);
                    this.listBoxEvent.AddItem(this.ListEvent[i]);
                    this.listBoxDetail.AddItem(this.ListDetail[i]);
                    this.listBoxBefore.AddItem(this.ListBefore[i]);
                    this.listBoxAfter.AddItem(this.ListAfter[i]);
                    this.listBoxType.AddItem(this.ListType[i]);
                }

                this.DataRemainCNT = this.DataRemainCNT - 50;
            }
            else
            {
                for (int i = index; i < this.ListTime.Count; i++)
                {
                    this.listBoxTime.AddItem(this.ListTime[i]);
                    this.listBoxLoginID.AddItem(this.ListLoginID[i]);
                    this.listBoxEvent.AddItem(this.ListEvent[i]);
                    this.listBoxDetail.AddItem(this.ListDetail[i]);
                    this.listBoxBefore.AddItem(this.ListBefore[i]);
                    this.listBoxAfter.AddItem(this.ListAfter[i]);
                    this.listBoxType.AddItem(this.ListType[i]);
                }

                this.DataRemainCNT = 0;
            }

            this.ListBoxItemsCount();

            splash.Finish();

            // 화면 표시 로딩시간 타이머 off
            this.smartTimer1.StopWatch();
            this.labelDisplayUpdateElapsedTime.Text = this.smartTimer1.StopWatchElapsedMicrosecond.ToString();
        }

        private void UpdateListItemByFilter(DataViewerFilter filter)
        {
            bool enable = false;
            // List Item Add
            for (int i = 0; i < this.CollectionHistoryData.Count; i++)
            {
                // 필터 적용
                switch (this.CollectionHistoryData[i][7])
                {
                    case "Alarm":
                        enable = filter.TypeAlarm;
                        break;
                    case "Operation":
                        enable = filter.TypeOperation;
                        break;
                    case "Parameter":
                        enable = filter.TypeParameter;
                        break;
                    default:
                        enable = false;
                        break;
                }

                if (enable == true)
                {
                    this.ListTime.Add(this.CollectionHistoryData[i][1]);
                    this.ListLoginID.Add(this.CollectionHistoryData[i][2]);
                    this.ListEvent.Add(this.CollectionHistoryData[i][3]);
                    this.ListDetail.Add(this.CollectionHistoryData[i][4]);
                    this.ListBefore.Add(this.CollectionHistoryData[i][5]);
                    this.ListAfter.Add(this.CollectionHistoryData[i][6]);
                    this.ListType.Add(this.CollectionHistoryData[i][7]);
                }
            }
        }

        public int DataRead(string filePath)
        {
            //string path = "";
            int ret = 0, flag = 0;
            long count = 0;

            SmartSplash splash;

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

            this.ClearData();
            this.CollectionHistoryData.Clear();

            // 파일 로딩 시간 타이머 ON
            this.smartTimer1.StartWatch();

            this.ParentForm.smartFileIO.FilePathName = filePath;
            this.ParentForm.smartFileIO.Open(2000000);
            this.ParentForm.smartFileIO.ReadStringAllBuffer();

            try
            {
                count = this.ParentForm.smartFileIO.GetCount();

                for (int i = 0; i < count; i++)
                {
                    if (flag == 100)
                    {
                        Thread.Sleep(5);
                        flag = 0;
                    }
                    this.CollectionHistoryData.Add(this.ParentForm.smartFileIO.ReadStringBuffer(i).Split(','));
                    flag++;
                }

                this.ParentForm.smartFileIO.Close();
                this.CollectionHistoryData.RemoveAt(0);

                this.DataAllCNT = this.CollectionHistoryData.Count;

                // 파일 로딩 시간 타이머 off
                this.smartTimer1.StopWatch();
                this.labelFileReadElapsedTime.Text = this.smartTimer1.StopWatchElapsedMicrosecond.ToString();

                // 화면 표시 로딩시간 타이머 on
                this.smartTimer1.StartWatch();

                // File Name
                string[] data = filePath.Split('\\');
                data = data[data.Length - 1].Split('.');
                this.labelFileName.Text = data[0];
                // List 갱신
                this.UpdateListItemByFilter(this.ParentForm.CurrentDataViewerFilter);
                // 화면 표시
                this.UpdateDisplay();

                // 화면 표시 로딩시간 타이머 off
                this.smartTimer1.StopWatch();
                this.labelDisplayUpdateElapsedTime.Text = this.smartTimer1.StopWatchElapsedMicrosecond.ToString();

                this.CollectionHistoryData.Clear();
                ret = 0;
                splash.Finish();
            }
            catch
            {
                this.CollectionHistoryData.Clear();
                ret = -1;
                splash.Finish();
                DialogFormMessage msg = new DialogFormMessage(13, this.ParentForm.SystemConfig.Language);
                msg.ShowDialog();

                this.ParentForm.smartFileIO.Close();
            }

            return ret;
        }
        public void ClearData()
        {
            this.CollectionHistoryData.Clear();

            this.listBoxTime.ClearAll();
            this.listBoxLoginID.ClearAll();
            this.listBoxEvent.ClearAll();
            this.listBoxDetail.ClearAll();
            this.listBoxBefore.ClearAll();
            this.listBoxAfter.ClearAll();
            this.listBoxType.ClearAll();

            this.ListTime.Clear();
            this.ListLoginID.Clear();
            this.ListEvent.Clear();
            this.ListDetail.Clear();
            this.ListBefore.Clear();
            this.ListAfter.Clear();
            this.ListType.Clear();
        }
        public void DisplayRefresh()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormDataViewer;

            this.labelFileName.Text = "";
            this.ClearData();
            this.ListBoxItemsCount();
        }
        #endregion

        #region Event Handler
        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
        }
        private void buttonFileSelect_Click(object sender, EventArgs e)
        {
            this.SelectNodeIndex = -1;

            DialogFormHistoryData form = new DialogFormHistoryData(this);
            if (form.ShowDialog() == DialogResult.OK)
                this.buttonSave.Enabled = true;
            else
                this.buttonSave.Enabled = false;
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {

        }
        private void buttonFilter_Click(object sender, EventArgs e)
        {
            DialogFormDataViewerFilter form = new DialogFormDataViewerFilter(this);
            if (form.ShowDialog() == DialogResult.OK)
            {
                // 필터적용
                this.ClearData();
                this.UpdateListItemByFilter(this.ParentForm.CurrentDataViewerFilter);
                this.UpdateDisplay();
            }
        }
        private void buttonUp_Click(object sender, EventArgs e)
        {
            if (this.StartIndex == 0)
                return;

            if (this.StartIndex < 6)
                this.ListBoxScrollUp();
            else
                this.ListBoxScrollUp(50);
        }
        private void buttonDown_Click(object sender, EventArgs e)
        {
            if (this.RemainCNT == 0)
            {
                if (this.DataRemainCNT != 0)
                {
                    this.UpdateDisplayAddListBox();
                }
                return;
            }

            if (this.RemainCNT < 6)
            {
                this.ListBoxScrollDown();

                if (this.listBoxTime.ViewRemainCount == 0)
                    this.ListBoxScrollDown(20);
            }
            else
                this.ListBoxScrollDown(50);
        }

        private void listBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            SmartListBox lb = sender as SmartListBox;

            if (lb == null)
                return;

            this.listBoxTime.SelectItemIndex = lb.SelectItemIndex;
            this.listBoxLoginID.SelectItemIndex = lb.SelectItemIndex;
            this.listBoxEvent.SelectItemIndex = lb.SelectItemIndex;
            this.listBoxDetail.SelectItemIndex = lb.SelectItemIndex;
            this.listBoxBefore.SelectItemIndex = lb.SelectItemIndex;
            this.listBoxAfter.SelectItemIndex = lb.SelectItemIndex;
            this.listBoxType.SelectItemIndex = lb.SelectItemIndex;
        }
        #endregion
    }
}