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 SmartX;

using ITC81DB.Forms;
using ITC81DB.DialogForms;
using ITC81DB_ImageDll;

namespace ITC81DB.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.SystemConfig1.Language == DataStore.LanguageID.English)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundDataBackup));

                this.buttonDataBackupEnable.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engON_Black));
                this.buttonDataBackupEnable.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engOFF_Black));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundDataBackup));

                this.buttonDataBackupEnable.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnON_Black));
                this.buttonDataBackupEnable.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnOFF_Black));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundDataBackup));

                this.buttonDataBackupEnable.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusON_Black));
                this.buttonDataBackupEnable.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusOFF_Black));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundDataBackup));

                this.buttonDataBackupEnable.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerON_Black));
                this.buttonDataBackupEnable.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerOFF_Black));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundDataBackup));

                this.buttonDataBackupEnable.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engON_Black));
                this.buttonDataBackupEnable.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engOFF_Black));
            }
            else
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundDataBackup));

                this.buttonDataBackupEnable.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engON_Black));
                this.buttonDataBackupEnable.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engOFF_Black));
            }
        }
        private void DefaultSetting()
        {
            this.FileIndex = 0;
        }

        public void DisplayHiddenMenu(bool bValue)
        {
            this.buttonClear.Visible = true;
        }

        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(SystemStatus status)
        {
            bool directoryCheck = false;

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

            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.Contains("Data") == true || file.Name.Contains("Stat") == true || file.Name.Contains(".jpg") == true)
                    this.listBoxDataList.Items.Add(file.Name);
            }

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

            this.labelFileCount.Text = this.listBoxDataList.Items.Count.ToString();
            this.buttonClear.Visible = false;
        }
        #endregion

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

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

        private void buttonBackup_Click(object sender, EventArgs e)
        {
            int count = 0;
            bool directoryCheck = false;
            string filePath = "";
            DirectoryInfo dir = new DirectoryInfo(this.ParentForm.ParentForm.PathDataBackupFolder);
            directoryCheck = dir.Exists;
            // 폴더 체크
            if (directoryCheck == false)
                dir.Create();

            FileInfo[] checkLogFiles = dir.GetFiles();
            
            count = this.listBoxDataList.Items.Count;
            if (count == 0 && checkLogFiles.Length == 0)
            {
                DialogFormMessage msg = new DialogFormMessage(4, this.ParentForm.ParentForm.SystemConfig1.Language);
                msg.ShowDialog();
                return;
            }

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

            try
            {
                DirectoryInfo dinfo = new DirectoryInfo("하드 디스크\\");
                dinfo.GetDirectories();
            }
            catch
            {
                splash.Finish();

                DialogFormMessage msg = new DialogFormMessage(5, this.ParentForm.ParentForm.SystemConfig1.Language);
                msg.ShowDialog();
                return;
            }

            if (this.listBoxDataList.SelectedIndex == -1)
            {
                #region ListBox 선택되지 않았을 때
                try
                {
                    filePath = "하드 디스크\\";

                    FileInfo[] files = dir.GetFiles();

                    foreach (FileInfo file in files)
                    {
                        if (file.Name.Contains("Data") == true || file.Name.Contains("Stat") == true || file.Name.Contains(".jpg") == true)
                        {
                            this.FileCopy(file, filePath + file.Name);

                            this.listBoxDataList.Items.Remove(file.Name);
                            file.Delete();
                        }
                    }
                    this.labelFileCount.Text = this.listBoxDataList.Items.Count.ToString();

                    splash.Finish();

                    DialogFormMessage msg = new DialogFormMessage(count.ToString(), this.ParentForm.ParentForm.SystemConfig1.Language);
                    msg.ShowDialog();
                }
                catch
                {
                    splash.Finish();

                    DialogFormMessage msg = new DialogFormMessage(6, this.ParentForm.ParentForm.SystemConfig1.Language);
                    msg.ShowDialog();
                    return;
                }
                #endregion
            }
            else
            {
                #region ListBox 선택되었을 때
                try
                {
                    if (this.listBoxDataList.SelectedIndex >= 0)
                    {
                        filePath = "하드 디스크\\";

                        FileInfo[] files = dir.GetFiles();

                        foreach (FileInfo file in files)
                        {
                            if (file.Name.Contains(this.listBoxDataList.SelectedItem.ToString()) == true)
                            {
                                this.FileCopy(file, filePath + file.Name);

                                this.listBoxDataList.Items.Remove(file.Name);
                                file.Delete();
                                break;
                            }
                        }
                        this.labelFileCount.Text = this.listBoxDataList.Items.Count.ToString();

                        splash.Finish();

                        DialogFormMessage msg = new DialogFormMessage("1", this.ParentForm.ParentForm.SystemConfig1.Language);
                        msg.ShowDialog();
                    }
                }
                catch
                {
                    splash.Finish();

                    DialogFormMessage msg = new DialogFormMessage(6, this.ParentForm.ParentForm.SystemConfig1.Language);
                    msg.ShowDialog();
                    return;
                }
                #endregion
            }
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            if (this.listBoxDataList.Items.Count == 0)
                return;

            bool directoryCheck = false;
            string filePath = "";
            DirectoryInfo dir = new DirectoryInfo(this.ParentForm.ParentForm.PathDataBackupFolder);
            directoryCheck = dir.Exists;
            // 폴더 체크
            if (directoryCheck == false)
                dir.Create();

            FileInfo[] checkLogFiles = dir.GetFiles();

            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 28);
            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                if (this.listBoxDataList.SelectedIndex == -1)
                {
                    #region ListBox 선택되지 않았을 때
                    SmartSplash splash = new SmartSplash();
                    splash.CenterPosition = true;
                    splash.AnimationInterval = 200;
                    splash.LoadingImagePathname = "SmartLoading4";
                    splash.Start();

                    try
                    {
                        FileInfo[] files = dir.GetFiles();

                        foreach (FileInfo file in files)
                        {
                            if (file.Name.Contains("Data") == true || file.Name.Contains("Stat") == true || file.Name.Contains(".jpg") == true)
                            {
                                this.listBoxDataList.Items.Remove(file.Name);
                                file.Delete();
                            }
                        }
                        this.labelFileCount.Text = this.listBoxDataList.Items.Count.ToString();

                        splash.Finish();
                    }
                    catch
                    {
                        splash.Finish();

                        DialogFormMessage msg = new DialogFormMessage(6, this.ParentForm.ParentForm.SystemConfig1.Language);
                        msg.ShowDialog();
                        return;
                    }
                    #endregion
                }
                else
                {
                    #region ListBox 선택되었을 때
                    if (this.listBoxDataList.SelectedIndex >= 0)
                    {
                        FileInfo[] files = dir.GetFiles();

                        foreach (FileInfo file in files)
                        {
                            if (file.Name.Contains(this.listBoxDataList.SelectedItem.ToString()) == true)
                            {
                                this.listBoxDataList.Items.Remove(file.Name);
                                file.Delete();
                                break;
                            }
                        }

                        //files = dir.GetFiles();
                        //this.listBoxDataList.Items.Clear();
                        //foreach (FileInfo file in files)
                        //{
                        //    if (file.Name.StartsWith("Data") == true || file.Name.StartsWith("Stat") == true || file.Name.EndsWith(".jpg") == true)
                        //        this.listBoxDataList.Items.Add(file.Name);
                        //}
                        this.labelFileCount.Text = this.listBoxDataList.Items.Count.ToString();
                    }
                    #endregion
                }
            }
        }
        #endregion
    }
}