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

using INT51DB.Forms;
using INT51DB.DialogForms;
using INT51DB_ImageDll;

namespace INT51DB.Controls
{
    public partial class ControlCenterBasicDataBackup : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
        private int FileIndex;
        #endregion

        #region Constructor
        public ControlCenterBasicDataBackup(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()
        {
            Class1 images = new Class1();

            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.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(DataStore.UserGroup group)
        {
            bool directoryCheck = false;

            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.BasicDataBackup;
            this.ParentForm.ParentForm.SetDisplayMode(DataStore.DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

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

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

            FileInfo[] files = dir.GetFiles();

            this.listBoxDataList.Items.Clear();

            foreach (FileInfo file in files)
            {
                if (file.Name.StartsWith("20") == true)
                    this.listBoxDataList.Items.Add(file.Name);
            }

            if (this.ParentForm.ParentForm.SystemConfig.IsDataBackup == true)
                this.buttonDataBackupEnable.ButtonDown();
            else
                this.buttonDataBackupEnable.ButtonUp();

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

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

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

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

        private void labelUsbId_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelUsbId.Text, 3, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

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

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

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

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

            count = this.listBoxDataList.Items.Count;

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

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

            try
            {
                filePath = "하드 디스크\\";
                DirectoryInfo dir = new DirectoryInfo(this.ParentForm.ParentForm.PathDataBackupFolder);

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

                FileInfo[] files = dir.GetFiles();

                foreach (FileInfo file in files)
                {
                    if (file.Name.StartsWith("20"))
                    {
                        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.ParentForm.SystemConfig.Language);
                msg.ShowDialog();
            }
            catch
            {
                DialogFormMessage msg = new DialogFormMessage(6, this.ParentForm.ParentForm.SystemConfig.Language);
                msg.ShowDialog();
                return;
            }
        }
        #endregion
    }
}
