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;

namespace INT69DC_7C.Forms
{
    public partial class FormProgramUpdate : Form
    {
        #region Field
        private FormMain m_ParentForm;

        private int Check;

        private string USBPath = "";
        private string UpdateFilePath = "";
        private string OldFilePath = "";
        private string RemoveFolderPath = "";

        private List<string> DllName = new List<string>();
        private List<long> LongOldDllFileLastWriteTime = new List<long>();
        private List<long> LongNewDllFileLastWriteTime = new List<long>();
        #endregion

        #region Constructor
        public FormProgramUpdate(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.smartForm1.Image = new Bitmap(images.GetImage(ImageDll.ButtonImages.engFormUpdate));
                this.labelTitle.Text = "Program Update";
                this.labelUpdateWait.Text = "Wait a minute...";
                this.labelUpdateWait2.Text = "Do not disconnect the USB from the body";
                this.labelUpdateRestart.Text = "Turn the power off and back on";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "更新";
                this.labelUpdateWait.Text = "请稍等...";
                this.labelUpdateWait2.Text = "请勿断开USB";
                this.labelUpdateRestart.Text = "关掉电源，然后在打开电源";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
                this.labelTitle.Text = "Aktualizace";
                this.labelUpdateWait.Text = "Vyčkejte...";
                this.labelUpdateWait2.Text = "Nevyjímejte USB disk ze zařízení.";
                this.labelUpdateRestart.Text = "Nejprve zařízení vypněte a pak opět zapněte.";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.labelTitle.Text = "Aktualisierung";
                this.labelUpdateWait.Text = "Aktualisierung…";
                this.labelUpdateWait2.Text = "USB nicht vom Gerät trennen";
                this.labelUpdateRestart.Text = "Mit dem Hauptschalter aus- und einschalten";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.labelTitle.Text = "更新";
                this.labelUpdateWait.Text = "しばらくお待ちください。";
                this.labelUpdateWait2.Text = "USBを本体から外さないでください。";
                this.labelUpdateRestart.Text = "電源を入れ直してください。";
            }
            else
            {

            }
        }

        public void DisplayRefresh()
        {
            this.CheckError();
        }

        public void CheckError()
        {
            FileInfo[] newFiles;

            if (Directory.Exists("Hard Disk\\") == true)
                this.USBPath = "Hard Disk\\";
            else if (Directory.Exists("하드 디스크\\") == true)
                this.USBPath = "하드 디스크\\";
            else
                this.USBPath = "하드 디스크\\";

            this.UpdateFilePath = this.USBPath + "UpdateFiles\\";

            DirectoryInfo info = new DirectoryInfo(this.USBPath);
            DirectoryInfo directoryFolderUpdateFiles = new DirectoryInfo(this.UpdateFilePath);

            // USB 장착 유무 확인
            if (info.Exists == true)
            {
                // USB에 UpdateFiles 유무 확인
                if (directoryFolderUpdateFiles.Exists == true)
                {
                    newFiles = directoryFolderUpdateFiles.GetFiles();

                    if (newFiles.Length == 0)
                    {
                        // USB에 업데이트 파일이 없습니다
                        DialogFormMessage message = new DialogFormMessage(7, this.ParentForm.SystemConfig.Language);
                        message.ShowDialog();
                        return;
                    }
                    else
                    {
                        ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormProgramUpdate);
                    }
                }
                else
                {
                    // USB에 업데이트 폴더가 없습니다
                    DialogFormMessage message = new DialogFormMessage(9, this.ParentForm.SystemConfig.Language);
                    message.ShowDialog();
                    return;
                }
            }
            else
            {
                // USB메모리가 장착되지 않았습니다
                DialogFormMessage message = new DialogFormMessage(5, this.ParentForm.SystemConfig.Language);
                message.ShowDialog();
                return;
            }
        }

        private void ProgramUpdate()
        {
            FileInfo[] newFiles;
            DirectoryInfo directoryFolderUpdateFiles = new DirectoryInfo(this.UpdateFilePath);
            newFiles = directoryFolderUpdateFiles.GetFiles();

            this.progressBarUpdateBar.Value += 20;

            this.TreatmentRemoveOldFile();
            this.TreatmentCopyNewFile(newFiles);

            this.progressBarUpdateBar.Value += 100;
        }

        private void TreatmentRemoveOldFile()
        {
            bool removeFolderCheck = false;
            FileInfo[] oldFiles;
            DirectoryInfo directoryFolderRun = new DirectoryInfo(this.OldFilePath);
            DirectoryInfo directoryFolderRemove = new DirectoryInfo(this.RemoveFolderPath);

            removeFolderCheck = directoryFolderRemove.Exists;
            oldFiles = directoryFolderRun.GetFiles();

            if (removeFolderCheck == false)
                directoryFolderRemove.Create();

            foreach (FileInfo subFile in oldFiles)
                subFile.MoveTo(RemoveFolderPath + DateTime.Now.ToString("yyyyMMddHHmmss") + subFile.Name.ToString());

            this.progressBarUpdateBar.Value += 20;
        }

        private void TreatmentCopyNewFile(FileInfo[] newFile)
        {
            // 새 파일 복사
            foreach (FileInfo subFile in newFile)
                subFile.CopyTo(this.OldFilePath + subFile.Name.ToString());

            this.progressBarUpdateBar.Value += 20;

        }
        #endregion

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

            this.InitializeDesign();

            DirectoryInfo directoryFolderUpdateFiles = new DirectoryInfo(this.UpdateFilePath);
            if (directoryFolderUpdateFiles.Exists == false)
                directoryFolderUpdateFiles.Create();

            this.Check = 0;

            this.UpdateFilePath = this.USBPath + "UpdateFiles\\";
            this.OldFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\";
            if (this.OldFilePath.StartsWith("\\F") == true)
                this.RemoveFolderPath = "Flash Disk\\RemoveFile\\";
            else
                this.RemoveFolderPath = "SD Card\\RemoveFile\\";

            this.timerUpdate.Interval = 1000;
            this.timerUpdate.Enabled = true;
        }
        #endregion

        #region Event Handler
        private void timerUpdate_Tick(object sender, EventArgs e)
        {
            this.Check += 1;
            if (this.Check == 3)
            {
                this.ProgramUpdate();
            }

            if (this.progressBarUpdateBar.Value == 100)
            {
                this.labelUpdateWait.Visible = false;
                this.labelUpdateWait2.Visible = false;
                this.labelUpdateRestart.Visible = true;
            }
        }
        #endregion
    }
}