using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace MicronSync.Components.Forms
{
    public partial class DropUI : Form
    {
        public static string dropData { get; set; }
        public DropAction DropResult = DropAction.Cancelled; // Default behaviour
        public enum DropAction
        {
            SzUseRestore,
            SzUseBackup,
            DirUseBackup,
            DirUseRestore,
            LoadConfig,
            UnsupportedData,
            Cancelled
        }

        public DropUI() { InitializeComponent(); }

        private void DropUI_Shown(object sender, EventArgs e)
        {
            BringToFront();

            // Idenfify dropped data
            FileAttributes attr = File.GetAttributes(DropUI.dropData);
            if (dropData.EndsWith(".ini") || dropData.EndsWith(".mini"))
            {
                DropResult = DropAction.LoadConfig;
                Close();
            }
            else if (dropData.EndsWith(".7z"))
                LoadSzHandler();
            else if (attr == FileAttributes.Directory)
                LoadDirHandler();
            else
            {
                DropResult = DropAction.UnsupportedData;
                MessageHandler.errorMessage(MessageHandler.errCodes.MainWindow_BadConfigFile, null);
                Close();
            }
        }

        private void LoadDirHandler()
        {
            textReadPath.Text = dropData;
            tabControl.TabPages.Remove(tabProcess);
            tabControl.TabPages.Remove(tabSz);
        }

        private void LoadSzHandler()
        {
            textReadSz.Text = dropData;
            tabControl.TabPages.Remove(tabProcess);
            tabControl.TabPages.Remove(tabDir);
        }

        #region Form UI

        private void btnDirBackup_Click(object sender, EventArgs e)
        {
            DropResult = DropAction.DirUseBackup;
            Close();
        }

        private void btnDirRestore_Click(object sender, EventArgs e)
        {
            DropResult = DropAction.DirUseRestore;
            Close();
        }

        private void btnSzUseRestore_Click(object sender, EventArgs e)
        {
            DropResult = DropAction.SzUseRestore;
            Close();
        }

        private void btnSzUseBackup_Click(object sender, EventArgs e)
        {
            DropResult = DropAction.SzUseBackup;
            Close();
        }

        #endregion
    }
}
