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

namespace Dialect
{
    /// <summary>
    /// The main options dialog for the application.
    /// </summary>
    public partial class Options : Form
    {
        public Options()
        {
            InitializeComponent();

            // Set the icon and settings.
            Icon = Properties.Resources.Icon;
            Settings = Properties.Settings.Default;
            
            SetSettings();
        }

        /// <summary>
        /// The local settings object.
        /// </summary>
        Properties.Settings Settings;

        /// <summary>
        /// Applies the values in the settings object to the interface.
        /// </summary>
        private void SetSettings()
        {
            txtMosaic.Text = Settings.MosaicDirectory;

            chkFullscreen.Checked = Settings.PreviewFullscreen;
            chkAntiAliasing.Checked = Settings.ShowAntiAliasing;
            chkAnimations.Checked = Settings.ShowAnimations;

            if (Settings.PreviewWidth > numResX.Maximum)
                Settings.PreviewWidth = (int)numResX.Maximum;

            if (Settings.PreviewHeight > numResY.Maximum)
                Settings.PreviewHeight = (int)numResY.Maximum;

            numResX.Value = (decimal)Settings.PreviewWidth;
            numResY.Value = (decimal)Settings.PreviewHeight;

            pnlManifestColor.BackColor = Settings.BackgroundColor;

        }

        /// <summary>
        /// Applies the interface to the settings object.
        /// </summary>
        /// <returns>If the process was a success.</returns>
        private bool ApplySettings()
        {
            // If the directory is invalid, bail.
            if (!Utils.DirectoryChecker(txtMosaic.Text))
                return false;

            Settings.MosaicDirectory = txtMosaic.Text;

            Settings.PreviewFullscreen = chkFullscreen.Checked;
            Settings.ShowAntiAliasing = chkAntiAliasing.Checked;
            Settings.ShowAnimations = chkAnimations.Checked;

            Settings.PreviewWidth = (int)numResX.Value;
            Settings.PreviewHeight = (int)numResY.Value;

            Settings.BackgroundColor = pnlManifestColor.BackColor;
            Settings.Save();

            return true;
        }

        /// <summary>
        /// Set the manifest color.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnManifestSetColor_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();

            if (colorDialog.ShowDialog(this) != DialogResult.OK)
                return;

            pnlManifestColor.BackColor = colorDialog.Color;
        }

        /// <summary>
        /// Browse for a new Mosaic directory.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event arguments.</param>
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            String appLocation = Utils.FolderDialog(Messages.Folder_FindAppDirectory);

            // If the directory is invalid, bail.
            if (!Utils.DirectoryChecker(appLocation))
                return;

            txtMosaic.Text = appLocation;
        }

        /// <summary>
        /// Apply the settings and quit.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event arguments.</param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (ApplySettings()) this.Close();
        }

        /// <summary>
        /// Apply the settings.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event arguments.</param>
        private void btnApply_Click(object sender, EventArgs e)
        {
            ApplySettings();
        }
    }
}
