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

namespace s3sr
{
    public partial class frmGUI : Form
    {
        private bool Loaded = false;
        private frmAbout About = null;
        public bool updateAvailable = false;

        public frmGUI()
        {
            InitializeComponent();
            radBASE.CheckedChanged += new EventHandler(radGame_CheckedChanged);
            radEP01.CheckedChanged += new EventHandler(radGame_CheckedChanged);
            radSP01.CheckedChanged += new EventHandler(radGame_CheckedChanged);
            radEP02.CheckedChanged += new EventHandler(radGame_CheckedChanged);
            radSP02.CheckedChanged += new EventHandler(radGame_CheckedChanged);
            radEP03.CheckedChanged += new EventHandler(radGame_CheckedChanged);
            radSP03.CheckedChanged += new EventHandler(radGame_CheckedChanged);
            radStore.CheckedChanged += new EventHandler(radGame_CheckedChanged);
            radCustom.CheckedChanged += new EventHandler(radGame_CheckedChanged);
            cbxUseStoreAlternativePath.CheckedChanged += new EventHandler(Checkboxes_Click);
            cbxStoreAlternativeSubfiles.CheckedChanged += new EventHandler(Checkboxes_Click);
            cbxCustomSubfiles.CheckedChanged += new EventHandler(Checkboxes_Click);
            base.HelpButtonClicked += new CancelEventHandler(frmGUI_HelpRequested);
            base.FormClosing += new FormClosingEventHandler(this.frmGUI_FormClosing);
        }

        private string currentVersionTag;

        private void radGame_CheckedChanged(object sender, EventArgs e)
        {
            radGame_Change((sender as RadioButton).Tag.ToString());
        }

        private void frmGUI_HelpRequested(object sender, CancelEventArgs e)
        {
            About.ShowDialog();
            e.Cancel = true;
        }

        /// <summary>
        /// Change the settings for the current selected Radio
        /// </summary>
        /// <param name="tag">Version as TAG</param>
        private void radGame_Change(string tag)
        {
            grpGamePath.Size = new Size(459, 51);
            cbxCustomSubfiles.Visible = false;
            grpStoreAlternativePath.Location = new Point(156, 71);
            grpStoreAlternativePath.Visible = false;
            cbxUseStoreAlternativePath.Visible = false;
            cbxDecryptFiles.Visible = false;

            switch (tag.ToUpper())
            {
                case "BASE":
                    txtGamePath.Text = Settings.path_BASE;
                    break;
                case "EP01":
                    txtGamePath.Text = Settings.path_EP01;
                    break;
                case "SP01":
                    txtGamePath.Text = Settings.path_SP01;
                    break;
                case "EP02":
                    txtGamePath.Text = Settings.path_EP02;
                    break;
                case "SP02":
                    txtGamePath.Text = Settings.path_SP02;
                    break;
                case "EP03":
                    txtGamePath.Text = Settings.path_EP03;
                    break;
                case "SP03":
                    txtGamePath.Text = Settings.path_SP03;
                    break;
                case "STORE":
                    cbxUseStoreAlternativePath.Visible = true;
                    grpStoreAlternativePath.Visible = true;
                    txtGamePath.Text = Settings.path_Store;
                    txtStoreAlternativePath.Text = Settings.path_StoreAlternative;
                    cbxDecryptFiles.Visible = true;
                    break;
                case "CUSTOM":
                    grpGamePath.Size = new Size(459, 74);
                    cbxCustomSubfiles.Visible = true;
                    txtGamePath.Text = Settings.path_Custom;
                    break;
            }
            currentVersionTag = tag;
        }

        private void frmGUI_Load(object sender, EventArgs e)
        {
            #region Check for Updates
            if (Settings.checkUpdate > 0)
                if ((updateAvailable = Updater.CheckVersion("https://awtmk.svn.sourceforge.net/svnroot/ts3tools/s3sr/s3sr_Version.txt")))
                    (new frmUpdate()).ShowDialog();
            #endregion

            frmAbout.updateAvailable = this.updateAvailable;
            About = new frmAbout();

            this.Loaded = false;
            cbxInfoAlert.Checked = Settings.alertInfo;
            cbxCustomSubfiles.Checked = Settings.searchSubFiles_Custom;
            cbxStoreAlternativeSubfiles.Checked = Settings.searchSubFiles_Store;
            cbxUseStoreAlternativePath.Checked = Settings.useStoreAlternativePath;
            grpStoreAlternativePath.Enabled = Settings.useStoreAlternativePath;
            cbxDecryptFiles.Checked = Settings.decryptStore;
            cbxUseStoreAlternativePath.Location = new Point(168, 70);
            radEP01.Enabled = Settings.hasEP01;
            radSP01.Enabled = Settings.hasSP01;
            radEP02.Enabled = Settings.hasEP02;
            radSP02.Enabled = Settings.hasSP02;
            radEP03.Enabled = Settings.hasEP03;
            radBASE.Checked = true;

            txtOutput.Text = Settings.path_Output;
            this.Loaded = true;
        }

        private void frmGUI_FormClosing(object sender, EventArgs e)
        {
            Settings.alertInfo = cbxInfoAlert.Checked;
            Settings.searchSubFiles_Custom = cbxCustomSubfiles.Checked;
            Settings.searchSubFiles_Store = cbxStoreAlternativeSubfiles.Checked;
            Settings.useStoreAlternativePath = cbxUseStoreAlternativePath.Checked;
            Settings.decryptStore = cbxDecryptFiles.Checked;
            Settings.Save();
        }

        private void cmdGamePath_Click(object sender, EventArgs e)
        {
            #region Switch Selected Version Tag for Description
            switch (currentVersionTag.ToUpper())
            {
                case "BASE":
                    dlgFolderBrowser.Description = "Location of Base Game.";
                    if (!string.IsNullOrEmpty(Settings.path_BASE))
                        dlgFolderBrowser.SelectedPath = Settings.path_BASE;
                    break;
                case "EP01":
                    dlgFolderBrowser.Description = "Location of World Adventures.";
                    if (!string.IsNullOrEmpty(Settings.path_EP01))
                        dlgFolderBrowser.SelectedPath = Settings.path_EP01;
                    break;
                case "SP01":
                    dlgFolderBrowser.Description = "Location of High-End Loft Stuff.";
                    if (!string.IsNullOrEmpty(Settings.path_SP01))
                        dlgFolderBrowser.SelectedPath = Settings.path_SP01;
                    break;
                case "EP02":
                    dlgFolderBrowser.Description = "Location of Ambitions.";
                    if (!string.IsNullOrEmpty(Settings.path_EP02))
                        dlgFolderBrowser.SelectedPath = Settings.path_EP02;
                    break;
                case "SP02":
                    dlgFolderBrowser.Description = "Location of Fast Lane Stuff.";
                    if (!string.IsNullOrEmpty(Settings.path_SP02))
                        dlgFolderBrowser.SelectedPath = Settings.path_SP02;
                    break;
                case "EP03":
                    dlgFolderBrowser.Description = "Location of Late Night.";
                    if (!string.IsNullOrEmpty(Settings.path_EP03))
                        dlgFolderBrowser.SelectedPath = Settings.path_EP03;
                    break;
                case "SP03":
                    dlgFolderBrowser.Description = "Location of Outdoor Living Stuff.";
                    if (!string.IsNullOrEmpty(Settings.path_SP03))
                        dlgFolderBrowser.SelectedPath = Settings.path_SP03;
                    break;
                case "STORE":
                    dlgFolderBrowser.Description = "Location of Store folder.";
                    if (!string.IsNullOrEmpty(Settings.path_Store))
                        dlgFolderBrowser.SelectedPath = Settings.path_Store;
                    break;
                case "CUSTOM":
                    dlgFolderBrowser.Description = "Location of Custom folder (Mods).";
                    if (!string.IsNullOrEmpty(Settings.path_Custom))
                        dlgFolderBrowser.SelectedPath = Settings.path_Custom;
                    break;
            }
            #endregion

            dlgFolderBrowser.ShowDialog();

            if (!string.IsNullOrEmpty(dlgFolderBrowser.SelectedPath))
            {
                #region Save Selected Game Path
                switch (currentVersionTag.ToUpper())
                {
                    case "BASE":
                        Settings.path_BASE = dlgFolderBrowser.SelectedPath;
                        break;
                    case "EP01":
                        Settings.path_EP01 = dlgFolderBrowser.SelectedPath;
                        break;
                    case "SP01":
                        Settings.path_SP01 = dlgFolderBrowser.SelectedPath;
                        break;
                    case "EP02":
                        Settings.path_EP02 = dlgFolderBrowser.SelectedPath;
                        break;
                    case "SP02":
                        Settings.path_SP02 = dlgFolderBrowser.SelectedPath;
                        break;
                    case "EP03":
                        Settings.path_EP03 = dlgFolderBrowser.SelectedPath;
                        break;
                    case "SP03":
                        Settings.path_SP03 = dlgFolderBrowser.SelectedPath;
                        break;
                    case "STORE":
                        Settings.path_Store = dlgFolderBrowser.SelectedPath;
                        break;
                    case "CUSTOM":
                        Settings.path_Custom = dlgFolderBrowser.SelectedPath;
                        break;
                }
                #endregion
                
                txtGamePath.Text = dlgFolderBrowser.SelectedPath;
            }
        }

        private void cmdOutputPath_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Settings.path_Output))
                dlgFolderBrowser.SelectedPath = Settings.path_Output;
            
            dlgFolderBrowser.Description = "Location of the Output Package";
            dlgFolderBrowser.ShowDialog();

            if (!string.IsNullOrEmpty(dlgFolderBrowser.SelectedPath))
            {
                Settings.path_Output = dlgFolderBrowser.SelectedPath;
                txtOutput.Text = dlgFolderBrowser.SelectedPath;
            }
        }

        private void cbxUseStoreAlternativePath_CheckedChanged(object sender, EventArgs e)
        {
            grpStoreAlternativePath.Enabled = cbxUseStoreAlternativePath.Checked;
        }

        private void cmdStoreAlternativePath_Click(object sender, EventArgs e)
        {
            dlgFolderBrowser.Description = "Location of Alternative Store folder.";
            if (!string.IsNullOrEmpty(Settings.path_StoreAlternative))
                dlgFolderBrowser.SelectedPath = Settings.path_StoreAlternative;

            dlgFolderBrowser.ShowDialog();

            if (!string.IsNullOrEmpty(dlgFolderBrowser.SelectedPath))
            {
                Settings.path_StoreAlternative = dlgFolderBrowser.SelectedPath;
                txtStoreAlternativePath.Text = dlgFolderBrowser.SelectedPath;
            }
        }

        private void Checkboxes_Click(object sender, EventArgs e)
        {
            if (!this.Loaded) return;
            Settings.useStoreAlternativePath = cbxUseStoreAlternativePath.Checked;
            Settings.searchSubFiles_Store = cbxStoreAlternativeSubfiles.Checked;
            Settings.searchSubFiles_Custom = cbxCustomSubfiles.Checked;
        }
    }
}