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_2.Forms;
using ITC81DB_2.DialogForms;
using ITC81DB_2_ImageDll;

namespace ITC81DB_2.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 == Define.E_LanguageID.English)
            {
                this.labelTitleUSBID.Text = "USB ID";
                this.labelTitleBackup.Text = "Backup";
                this.labelTitleFile.Text = "Files";
                this.labelTitleWarning.Text = "※ Caution  :  data in system are deleted when data is back up to USB memory";

                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.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.labelTitleUSBID.Text = "USB ID";
                this.labelTitleBackup.Text = "备份";
                this.labelTitleFile.Text = "文件";
                this.labelTitleWarning.Text = "※ 注意：当数据保存到USB中后，系统数据被删除";

                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.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.labelTitleUSBID.Text = "USB ID";
                this.labelTitleBackup.Text = "Respaldo";
                this.labelTitleFile.Text = "Archivos";
                this.labelTitleWarning.Text = "※ PRECAUCIÓN: se eliminan los datos en el sistemaCuando los datos están de vuelta en la memoria USB.";

                this.buttonDataBackupEnable.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnON_Black));
                this.buttonDataBackupEnable.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnOFF_Black));
            }
            else
            {
                this.labelTitleUSBID.Text = "USB ID";
                this.labelTitleBackup.Text = "백업여부";
                this.labelTitleFile.Text = "저장파일";
                this.labelTitleWarning.Text = "※ 주의 : USB 메모리 장치로 데이터를 백업할 경우, 시스템에 저장된 데이터는 삭제됩니다.";

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

        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 = Define.E_DisplayStore.BasicDataBackup;
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_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.StartsWith("20") == true || file.Name.StartsWith("Communicationlog") == true || file.Name.EndsWith(".jpg") == 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.SaveSystemConfigurationFile1(this.ParentForm.ParentForm.SystemConfig);
        }

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 99)
                {
                    // 입력범위를 확인하세요
                    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.SaveSystemConfigurationFile1(this.ParentForm.ParentForm.SystemConfig);
                }
            }
        }

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

            this.ParentForm.ParentForm.smartFileCommunicationLog.Close();
            this.ParentForm.ParentForm.IsCommunicationLogOpen = false;
            
            count = this.listBoxDataList.Items.Count;
            if (count == 0)
            {
                DialogFormMessage msg = new DialogFormMessage(4, this.ParentForm.ParentForm.SystemConfig.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.SystemConfig.Language);
                msg.ShowDialog();
                return;
            }

            try
            {
                filePath = "하드 디스크\\";

                FileInfo[] files = dir.GetFiles();

                foreach (FileInfo file in files)
                {
                    if (file.Name.StartsWith("20") == true || file.Name.StartsWith("Communicationlog") == true
                        || file.Name.EndsWith(".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.SystemConfig.Language);
                msg.ShowDialog();
            }
            catch
            {
                splash.Finish();

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