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

using SmartX;
using INT63DC_ImageDll;
using INT63DC_2C.DialogForms;

namespace INT63DC_2C.Forms
{
    public partial class FormDataBackup : Form
    {
        #region Field
        private FormMain m_ParentForm;

        private DatabackupFileInfo2 SelectedFiles;
        #endregion

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

            this.ParentForm = parent;
        }
        #endregion

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

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

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    break;
                case DataStore.E_LanguageID.English:
                    #region English
                    this.labelTitle.Text = "Data backup";

                    this.labelTitleBackupEnable.Text = "Enable backup";
                    this.labelTitleDeleteAfterBackup.Text = "Delete after backup";
                    this.labelTitleSavedFiles.Text = "Saved files";
                    this.labelTitleFilesToBackup.Text = "Files to Back Up";

                    this.buttonBackup.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackupDisable));
                    this.buttonBackup.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackupDown));
                    this.buttonBackup.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackupUp));

                    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)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Chinese:
                    #region Chinese
                    this.labelTitle.Text = "数据备份";

                    this.labelTitleBackupEnable.Text = "启用备份";
                    this.labelTitleDeleteAfterBackup.Text = "备份后删除";
                    this.labelTitleSavedFiles.Text = "已保存文件";
                    this.labelTitleFilesToBackup.Text = "待备份文件";

                    this.buttonBackup.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackupDisable));
                    this.buttonBackup.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackupDown));
                    this.buttonBackup.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackupUp));

                    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)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Czech:
                    #region Czech
                    this.labelTitle.Text = "Záloha dat";

                    this.labelTitleBackupEnable.Text = "Povolit zálohování";
                    this.labelTitleDeleteAfterBackup.Text = "Odstranit po zálohování";
                    this.labelTitleSavedFiles.Text = "Soubor";
                    this.labelTitleFilesToBackup.Text = "Soubory k zálohování";

                    this.buttonBackup.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackupDisable));
                    this.buttonBackup.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackupDown));
                    this.buttonBackup.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackupUp));

                    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)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.German:
                    #region German
                    this.labelTitle.Text = "Datensicherung";

                    this.labelTitleBackupEnable.Text = "Sicherung aktivieren";
                    this.labelTitleDeleteAfterBackup.Text = "Nach der Sicherung löschen";
                    this.labelTitleSavedFiles.Text = "Datei";
                    this.labelTitleFilesToBackup.Text = "Zu sichernde Dateien";

                    this.buttonBackup.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackupDisable));
                    this.buttonBackup.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackupDown));
                    this.buttonBackup.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackupUp));

                    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)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Japanese:
                    #region Japanese
                    this.labelTitle.Text = "デㅡタバックアップ";

                    this.labelTitleBackupEnable.Text = "バックアップを有効にする";
                    this.labelTitleDeleteAfterBackup.Text = "バックアップ後に削除";
                    this.labelTitleSavedFiles.Text = "ファイル";
                    this.labelTitleFilesToBackup.Text = "バックアップ待ち";

                    this.buttonBackup.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackupDisable));
                    this.buttonBackup.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackupDown));
                    this.buttonBackup.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackupUp));

                    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)); 
                    #endregion
                    break;
                default:
                    break;
            }
        }
        private void DefaultSetting()
        {
            this.SelectedFiles = new DatabackupFileInfo2();
        }
        private void UpdateFileList()
        {
            this.listBoxDataList.ClearAll();
            this.listBoxDataListCHK.ClearAll();
            this.listBoxBackupList.ClearAll();
            this.SelectedFiles.Clear();

            DirectoryInfo dir = new DirectoryInfo(this.ParentForm.PathDataBackupFolder);
            bool directoryCheck = dir.Exists;

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

            FileInfo[] files = dir.GetFiles();

            // ListBox
            if (files.Length != 0)
            {
                foreach (FileInfo file in files)
                {
                    this.listBoxDataList.AddItem(file.Name);
                    this.listBoxDataListCHK.AddItem("");
                }
            }

            this.labelFileCount.Text = files.Length.ToString();
            this.labelBackupFileCount.Text = this.listBoxBackupList.ItemCount.ToString();
        }

        public void DisplayRefresh()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormDataBackup;
            
            // DataBackup Enable
            if (this.ParentForm.SystemConfig.IsDataBackup == true)
                this.buttonDataBackupEnable.ButtonDown();
            else
                this.buttonDataBackupEnable.ButtonUp();
            // Delete After Backup
            if (this.ParentForm.SystemConfig.IsDeleteAfterBackup == true)
                this.buttonDeleteAfterBackup.ButtonDown();
            else
                this.buttonDeleteAfterBackup.ButtonUp();
            // Update File List
            this.UpdateFileList();
        }
        #endregion

        #region Override Member
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

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

        #region Event Handler
        private void buttonBackup_Click(object sender, EventArgs e)
        {
            int count = 0;
            SmartSplash splash;
            string srcFilePath = "", dstFilePath1 = "", dstFilePath2 = ""; ;

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

            count = this.listBoxBackupList.ItemCount;

            if (count == 0)
            {
                DialogFormMessage msg = new DialogFormMessage(4, this.ParentForm.SystemConfig.Language);
                msg.ShowDialog();
                return;
            }

            splash.Start();

            try
            {
                DirectoryInfo dinfo = new DirectoryInfo("하드 디스크\\");
                dinfo.GetDirectories();
            }
            catch
            {
                DialogFormMessage msg = new DialogFormMessage(5, this.ParentForm.SystemConfig.Language);
                splash.Finish();
                msg.ShowDialog();
                return;
            }

            try
            {
                dstFilePath1 = string.Format("하드 디스크\\{0}\\{1:yyyMMdd_HHmmss}\\", this.ParentForm.SystemConfig.SerialNumber, DateTime.Now);
                DirectoryInfo dir = new DirectoryInfo(dstFilePath1);
                if (dir.Exists == false)
                    dir.Create();

                for (int i = 0; i < count; i++)
                {
                    srcFilePath = string.Format("{0}{1}", this.ParentForm.PathDataBackupFolder, this.SelectedFiles.Files[i].Name);
                    dstFilePath2 = string.Format("{0}{1}", dstFilePath1, this.SelectedFiles.Files[i].Name);
                    File.Copy(srcFilePath, dstFilePath2, true);

                    if (this.ParentForm.SystemConfig.IsDeleteAfterBackup == true)
                        File.Delete(srcFilePath);
                }

                this.UpdateFileList();
                this.SelectedFiles.Clear();
                this.listBoxBackupList.ClearAll();
                this.labelFileCount.Text = this.listBoxDataList.ItemCount.ToString();
                this.labelBackupFileCount.Text = this.listBoxBackupList.ItemCount.ToString();

                DialogFormMessage msg = new DialogFormMessage(count.ToString(), this.ParentForm.SystemConfig.Language, "Data backup");
                splash.Finish();
                msg.ShowDialog();
            }
            catch
            {
                DialogFormMessage msg = new DialogFormMessage(6, this.ParentForm.SystemConfig.Language);
                splash.Finish();
                msg.ShowDialog();
                return;
            }
        }
        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
        }
        private void buttonFileAdd_Click(object sender, EventArgs e)
        {
            if (this.listBoxDataListCHK.Items[this.listBoxDataListCHK.SelectItemIndex].ToString() != "V")
            {
                if (this.listBoxBackupList.ItemCount < 10)
                {
                    int index = this.listBoxDataList.SelectItemIndex;
                    string name = this.listBoxDataList.Items[index].ToString();

                    this.SelectedFiles.Add(index, name);
                    this.listBoxBackupList.AddItem(name);

                    this.listBoxDataListCHK.Items[index] = "V";
                    this.listBoxDataListCHK.Refresh();
                }
                else
                {
                    DialogFormMessage msg = new DialogFormMessage(12, this.ParentForm.SystemConfig.Language);
                    msg.ShowDialog();
                }

                this.labelBackupFileCount.Text = this.listBoxBackupList.ItemCount.ToString();
            }
        }
        private void buttonFileRemove_Click(object sender, EventArgs e)
        {
            if (this.listBoxBackupList.SelectItemIndex != -1)
            {
                string name = this.listBoxBackupList.Items[this.listBoxBackupList.SelectItemIndex].ToString();
                int fileIndex = this.SelectedFiles.GetIndex(name);
                int listboxIndex = this.listBoxBackupList.SelectItemIndex;

                this.listBoxDataListCHK.Items[fileIndex] = "";
                this.listBoxDataListCHK.Refresh();
                this.listBoxBackupList.RemoveItem(listboxIndex);
                this.SelectedFiles.RemoveAt(name);
                this.labelBackupFileCount.Text = this.listBoxBackupList.ItemCount.ToString();
            }
        }
        private void buttonScrollUp_Click(object sender, EventArgs e)
        {
            this.listBoxDataList.ScrollUp(100);
            this.listBoxDataListCHK.ScrollUp(100);
        }
        private void buttonScrollDown_Click(object sender, EventArgs e)
        {
            this.listBoxDataList.ScrollDown(100);
            this.listBoxDataListCHK.ScrollDown(100);
        }
        private void buttonDataBackupEnable_Click(object sender, EventArgs e)
        {
            if (this.buttonDataBackupEnable.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                this.ParentForm.SystemConfig.IsDataBackup = true;
            else
                this.ParentForm.SystemConfig.IsDataBackup = false;

            this.ParentForm.SaveSystemConfigurationFile1(this.ParentForm.SystemConfig);
        }

        private void buttonDeleteAfterBackup_Click(object sender, EventArgs e)
        {
            if (this.buttonDeleteAfterBackup.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                this.ParentForm.SystemConfig.IsDeleteAfterBackup = true;
            else
                this.ParentForm.SystemConfig.IsDeleteAfterBackup = false;

            this.ParentForm.SaveSystemConfigurationFile1(this.ParentForm.SystemConfig);
        }

        private void listBoxDataList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.listBoxDataListCHK.SelectItemIndex = this.listBoxDataList.SelectItemIndex;
        }
        private void listBoxDataListCHK_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.listBoxDataList.SelectItemIndex = this.listBoxDataListCHK.SelectItemIndex;
        }
        #endregion
    }
}