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 INT69DC_7C.DialogForms;
using INT69DC_ImageDll;

namespace INT69DC_7C.Forms
{
    public partial class FormDataBackup : Form
    {
        #region Field
        private FormMain m_ParentForm;
        private int FileIndex;
        #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();

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

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

                this.labelStaticBackupEnable.Text = "Create backup file";
                this.labelStaticSaveFile.Text = "Saved files";
                this.labelStaticWarning1.Text = "※ Caution";
                this.labelStaticWarning2.Text = "The saved data in this system is deleted if the backup is performed through USB memory device.";

                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));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "数据备份";

                this.labelStaticBackupEnable.Text = "备份";
                this.labelStaticSaveFile.Text = "文件";
                this.labelStaticWarning1.Text = "※注意：";
                this.labelStaticWarning2.Text = "当数据保存到USB中后，系统数据被删除";

                this.radioButtonEnable.Text = "启用";
                this.radioButtonDisable.Text = "禁用";

                this.buttonUSBID.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFormDataBackupUSBIDDown));
                this.buttonUSBID.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFormDataBackupUSBIDUp));

                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));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
                this.labelTitle.Text = "Záloha dat";

                this.labelStaticBackupEnable.Text = "Zálohovat";
                this.labelStaticSaveFile.Text = "Soubory";
                this.labelStaticWarning1.Text = "※ Pozor";
                this.labelStaticWarning2.Text = "Pokud je záloha provedena na USB disk, data v systému se vymažou.";
                this.radioButtonEnable.Text = "Umožnit";
                this.radioButtonDisable.Text = "Zakázat";

                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));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.labelTitle.Text = "Datensicherung";

                this.labelStaticBackupEnable.Text = "Sicherung";
                this.labelStaticSaveFile.Text = "Datei";
                this.labelStaticWarning1.Text = "※ Vorsicht";
                this.labelStaticWarning2.Text = "Nach der USB-Sicherung werden die Daten im System gelöscht!";
                this.radioButtonEnable.Text = "Aktivieren";
                this.radioButtonDisable.Text = "Deaktivieren";

                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));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.labelTitle.Text = "デㅡタバックアップ";

                this.radioButtonEnable.Text = "使用";
                this.radioButtonDisable.Text = "未使用";

                this.labelStaticBackupEnable.Text = "バックアップ機能";
                this.labelStaticSaveFile.Text = "ファイル";
                this.labelStaticWarning1.Text = "※ 注意";
                this.labelStaticWarning2.Text = "システム内のデㅡタは削除されるUSBメモリにデㅡタをバックアップする場合";

                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));
            }
            else
            {

            }
        }
        private void DefaultSetting()
        {
            this.FileIndex = 0;
        }

        public void DisplayRefresh()
        {
            bool directoryCheck = false;

            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormDataBackup;

            DirectoryInfo dir = new DirectoryInfo(this.ParentForm.PathDataBackupFolder);

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

            FileInfo[] files = dir.GetFiles();

            this.listBoxDataList.Items.Clear();

            foreach (FileInfo file in files)
            {
                this.listBoxDataList.Items.Add(file.Name);
            }

            if (this.ParentForm.SystemConfig.IsDataBackup == true)
            {
                this.radioButtonEnable.Checked = true;
                this.radioButtonDisable.Checked = false;
            }
            else
            {
                this.radioButtonEnable.Checked = false;
                this.radioButtonDisable.Checked = true;
            }

            this.labelFileCount.Text = this.listBoxDataList.Items.Count.ToString();

            this.buttonUSBID.Text = this.ParentForm.SystemConfig.UsbID.ToString();
        }

        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);
            }
        }
        #endregion

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

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

        #region Event Handler
        private void buttonUSBID_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonUSBID.Text, 2, 0, false, this.ParentForm.SystemConfig.Language);

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

                    this.ParentForm.SystemConfig.UsbID = myKeyPad.IntValue;

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

        private void radioButtonEnable_Click(object sender, EventArgs e)
        {
            if (this.radioButtonEnable.Checked == true)
            {
                this.ParentForm.SystemConfig.IsDataBackup = true;

                if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                    this.ParentForm.SystemConfig.IsIntegratedTransmission = true;
            }
            else
                this.ParentForm.SystemConfig.IsDataBackup = false;

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

        private void buttonBackup_Click(object sender, EventArgs e)
        {
            int count = 0;
            bool directoryCheck = false;
            string filePath = "";
            SmartX.SmartSplash splash;

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

            count = this.listBoxDataList.Items.Count;

            // ADC값 로그, 통신 로그 찍기 중지
            this.ParentForm.timerAdcLog.Enabled = false;
            this.ParentForm.smartFileCommunicationLog.Close();
            this.ParentForm.smartFileCom3Log.Close();
            this.ParentForm.smartFileAdcLog.Close();
            this.ParentForm.IsCommunicationLogOpen = false;
            this.ParentForm.IsCom3LogOpen = false;
            this.ParentForm.IsAdcLogOpen = false;

            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
            {
                filePath = "하드 디스크\\";
                DirectoryInfo dir = new DirectoryInfo(this.ParentForm.PathDataBackupFolder);

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

                FileInfo[] files = dir.GetFiles();

                foreach (FileInfo file in files)
                {
                    this.FileCopy(file, filePath + file.Name);

                    this.listBoxDataList.Items.Remove(file.Name);
                    file.Delete();
                }
                this.labelFileCount.Text = this.listBoxDataList.Items.Count.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);
        }
        #endregion
    }
}