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 INT89DB_26_ImageDll;
using INT89DB_26.DialogForms;

namespace INT89DB_26.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()
        {
            Class1 images = new Class1();

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

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
            {
                this.labelTitle.Text = "Data backup";
                this.labelStaticDataBackup.Text = "Backup";
                //this.groupBoxSavedFiles.Text = "Check the saved files";
                this.labelStaticList.Text = "Lists of files";
                this.labelWarning.Text = "※Caution  :  Data in system are deleted when data is back up to USB memory";

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

                this.buttonBack.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
            {
                this.labelTitle.Text = "数据备份";
                this.labelStaticDataBackup.Text = "备份";
                //this.groupBoxSavedFiles.Text = "Check the saved files";
                this.labelStaticList.Text = "文件";
                this.labelWarning.Text = "※注意：当数据保存到USB中后，系统数据被删除";
                this.smartLabel1.Text = "USB标识符";

                this.buttonDataBackupEnable.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnEnable));
                this.buttonDataBackupEnable.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnDisable));

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

                this.buttonBack.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
            {

            }
            else
            {

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

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

        public void DisplayRefresh()
        {
            bool directoryCheck = false;
            string fullFilePath = "";

            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.buttonDataBackupEnable.ButtonDown();
            else
                this.buttonDataBackupEnable.ButtonUp();

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

            this.labelUsbId.Text = this.ParentForm.SystemConfig.UsbID.ToString();
        }
        #endregion

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

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

        #region Event Handler
        private void labelUsbId_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelUsbId.Text, 2, 0, false, this.ParentForm.SystemConfig.Language);
            myKeyPad.Location = new Point(270, 70);

            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.labelUsbId.Text = myKeyPad.doubleValue.ToString();

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

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

        private void buttonDataBackupEnable_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.SystemConfig.IsDataBackup == true)
            {
                this.ParentForm.SystemConfig.IsDataBackup = false;
                this.buttonDataBackupEnable.ButtonUp();
            }
            else
            {
                this.ParentForm.SystemConfig.IsDataBackup = true;
                this.buttonDataBackupEnable.ButtonDown();
            }

            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.smartFileAdcLog.Close();
            this.ParentForm.IsCommunicationLogOpen = 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);
                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.E_FormStore.FormMenu);
        }
        #endregion
    }
}