using System;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AdvertsingProfitControl
{
    public partial class FrmReportSettings : Form
    {
        public FrmReportSettings()
        {
            InitializeComponent();
        }

        private void UpdateFrontPageFontSize(object sender, EventArgs e)
        {
            Properties.Settings.Default.ApcFontSize = apcFontSizeNumericUpDown.Text + @"px";
        }

        private void UpdateBackPageFontSize(object sender, EventArgs e)
        {
            Properties.Settings.Default.WicFontSize = wicFontSizeNumericUpdown.Text + @"px";
        }

        private void UpdateApcReportWidth(object sender, EventArgs e)
        {
            string unit;
            if (apcPercentagesRadioButton.Checked)
            {
                unit = "%";
            }
            else if (apcInchesRadioButton.Checked)
            {
                unit = "in";
            }
            else
            {
                unit = "cm";
            }

            Properties.Settings.Default.ApcReportWidth = apcReportWidthNumericUpDown.Value + unit;
            Properties.Settings.Default.Save();
        }

        private void UpdateApcReportHeight(object sender, EventArgs e)
        {
            string unit;
            if (apcPercentagesRadioButton.Checked)
            {
                unit = "%";
            }
            else if (apcInchesRadioButton.Checked)
            {
                unit = "in";
            }
            else
            {
                unit = "cm";
            }

            Properties.Settings.Default.ApcReportHeight = apcReportHeightNumericUpDown.Value + unit;
            Properties.Settings.Default.Save();
        }

        private void ApcRadioButtonChecked(object sender, EventArgs e)
        {
            if (!((RadioButton) sender).Checked) return;
            SetApcNumericUpDowns((Control) sender);
        }

        private void FrmReportSettings_Load(object sender, EventArgs e)
        {
            //Prevent the loading from updating the saved values.
            apcFontSizeNumericUpDown.ValueChanged -= UpdateFrontPageFontSize;
            wicFontSizeNumericUpdown.ValueChanged -= UpdateBackPageFontSize;
            apcReportWidthNumericUpDown.ValueChanged -= UpdateApcReportWidth;
            apcReportHeightNumericUpDown.ValueChanged -= UpdateApcReportHeight;
            wicReportWidthNumericUpDown.ValueChanged -= UpdateWicReportWidth;
            wicReportHeightNumericUpDown.ValueChanged -= UpdateWicHeight;
            //Load the current settings for font sizes
            apcFontSizeNumericUpDown.Text = Properties.Settings.Default.ApcFontSize.Replace("px", "");
            wicFontSizeNumericUpdown.Text = Properties.Settings.Default.WicFontSize.Replace("px", "");
            //Load the width and height settings for the front page.           
            LoadApcSettings();
            LoadWicSettings();
            apcFontSizeNumericUpDown.ValueChanged += UpdateFrontPageFontSize;
            wicFontSizeNumericUpdown.ValueChanged += UpdateBackPageFontSize;
            wicReportWidthNumericUpDown.ValueChanged += UpdateWicReportWidth;
            wicReportHeightNumericUpDown.ValueChanged += UpdateWicHeight;
            apcReportWidthNumericUpDown.ValueChanged += UpdateApcReportWidth;
            apcReportHeightNumericUpDown.ValueChanged += UpdateApcReportHeight;
            //Grab the paper kinds
            var paperKinds = Enum.GetNames(typeof(PaperKind)).ToList();
            for (var i = 0; i < paperKinds.Count; i ++)
            {
                if(paperKinds[i] == "Custom") continue;
                paperKindComboBox.Items.Add(paperKinds[i]);
            }
            //Get the paper kind setting.
            if (paperKindComboBox.Items.Contains(Properties.Settings.Default.ReportPaperKind))
            {
                var index = paperKindComboBox.FindString(Properties.Settings.Default.ReportPaperKind);
                paperKindComboBox.SelectedIndex = index;
            }
            else
            {
                var index = paperKindComboBox.FindString(PaperKind.A4.ToString());
                paperKindComboBox.SelectedIndex = index;
            }
        }

        private void LoadApcSettings()
        {
            var splitSetting = ParseSetting(Properties.Settings.Default.ApcReportWidth);
            switch (splitSetting[1])
            {
                case "%":
                    apcPercentagesRadioButton.Checked = true;
                    SetApcNumericUpDowns(apcPercentagesRadioButton);
                    break;
                case "in":
                    apcInchesRadioButton.Checked = true;
                    SetApcNumericUpDowns(apcInchesRadioButton);
                    break;
                case "cm":
                    apcCentimetersRadioButton.Checked = true;
                    SetApcNumericUpDowns(apcCentimetersRadioButton);
                    break;
            }
            apcReportWidthNumericUpDown.Text = splitSetting[0];
            splitSetting = ParseSetting(Properties.Settings.Default.ApcReportHeight);
            apcReportHeightNumericUpDown.Text = splitSetting[0];
        }

        private void LoadWicSettings()
        {
            var splitSetting = ParseSetting(Properties.Settings.Default.WicReportWidth);
            switch (splitSetting[1])
            {
                case "%":
                    wicPercentageRadioButton.Checked = true;
                    SetWicNumericUpDowns(wicPercentageRadioButton);
                    break;
                case "in":
                    wicInchesRadioButton.Checked = true;
                    SetWicNumericUpDowns(wicInchesRadioButton);
                    break;
                case "cm":
                    wicCentimetersRadioButton.Checked = true;
                    SetWicNumericUpDowns(wicCentimetersRadioButton);
                    break;
            }
            wicReportWidthNumericUpDown.Text = splitSetting[0];
            splitSetting = ParseSetting(Properties.Settings.Default.WicReportHeight);
            wicReportHeightNumericUpDown.Text = splitSetting[0];
        }

        private void SetApcNumericUpDowns(Control radioButton)
        {
            if (radioButton.Name == apcPercentagesRadioButton.Name)
            {
                apcInchesRadioButton.Checked = false;
                apcCentimetersRadioButton.Checked = false;
                apcReportWidthNumericUpDown.Minimum = 1;
                apcReportWidthNumericUpDown.Maximum = 100;
                apcReportWidthNumericUpDown.Text = @"100";
                apcReportHeightNumericUpDown.Minimum = 1;
                apcReportHeightNumericUpDown.Maximum = 100;
                apcReportHeightNumericUpDown.Text = @"100";
                frontPageUnitDetails.Text = @"Units set to percent of page size (1 to 100%)";
            }
            else if (radioButton.Name == apcInchesRadioButton.Name)
            {
                apcPercentagesRadioButton.Checked = false;
                apcCentimetersRadioButton.Checked = false;
                apcReportWidthNumericUpDown.Minimum = 1;
                apcReportWidthNumericUpDown.Maximum = 25;
                apcReportWidthNumericUpDown.Text = @"11";
                apcReportHeightNumericUpDown.Minimum = 1;
                apcReportHeightNumericUpDown.Maximum = 25;
                apcReportHeightNumericUpDown.Text = @"8";
                frontPageUnitDetails.Text = @"Units set to inches (" + apcReportWidthNumericUpDown.Minimum + @" to "+ apcReportWidthNumericUpDown.Maximum + @" inches)";
            }
            else if (radioButton.Name == apcCentimetersRadioButton.Name)
            {
                apcPercentagesRadioButton.Checked = false;
                apcInchesRadioButton.Checked = false;
                apcReportWidthNumericUpDown.Minimum = (decimal) 2.54;
                apcReportWidthNumericUpDown.Maximum = (decimal) 63.5;
                apcReportWidthNumericUpDown.Text = @"27.94";
                apcReportHeightNumericUpDown.Minimum = (decimal) 2.54;
                apcReportHeightNumericUpDown.Maximum = (decimal) 63.5;
                apcReportHeightNumericUpDown.Text = @"20.32";
                frontPageUnitDetails.Text = @"Units set to centimeters (" + apcReportWidthNumericUpDown.Minimum + @" to " + apcReportWidthNumericUpDown.Maximum + @" centimeters)";
            }
        }

        private void SetWicNumericUpDowns(Control radioButton)
        {
            if (radioButton.Name == wicPercentageRadioButton.Name)
            {
                wicInchesRadioButton.Checked = false;
                wicCentimetersRadioButton.Checked = false;
                wicReportWidthNumericUpDown.Minimum = 1;
                wicReportWidthNumericUpDown.Maximum = 100;
                wicReportWidthNumericUpDown.Text = @"100";
                wicReportHeightNumericUpDown.Minimum = 1;
                wicReportHeightNumericUpDown.Maximum = 100;
                wicReportHeightNumericUpDown.Text = @"100";
                backPageUnitsDetailLabel.Text = @"Units set to percent of page size (1 to 100%)";
            }
            else if (radioButton.Name == wicInchesRadioButton.Name)
            {
                wicPercentageRadioButton.Checked = false;
                wicCentimetersRadioButton.Checked = false;
                wicReportWidthNumericUpDown.Minimum = 1;
                wicReportWidthNumericUpDown.Maximum = 25;
                wicReportWidthNumericUpDown.Text = @"8";
                wicReportHeightNumericUpDown.Minimum = 1;
                wicReportHeightNumericUpDown.Maximum = 25;
                wicReportHeightNumericUpDown.Text = @"11";
                backPageUnitsDetailLabel.Text = @"Units set to inches (" + wicReportWidthNumericUpDown.Minimum + @" to " + wicReportWidthNumericUpDown.Maximum + @" inches)";
            }
            else if (radioButton.Name == wicCentimetersRadioButton.Name)
            {
                wicPercentageRadioButton.Checked = false;
                wicInchesRadioButton.Checked = false;
                wicReportWidthNumericUpDown.Minimum = (decimal)2.54;
                wicReportWidthNumericUpDown.Maximum = (decimal)63.5;
                wicReportWidthNumericUpDown.Text = @"20.32";
                wicReportHeightNumericUpDown.Minimum = (decimal)2.54;
                wicReportHeightNumericUpDown.Maximum = (decimal)63.5;
                wicReportHeightNumericUpDown.Text = @"27.94";
                backPageUnitsDetailLabel.Text = @"Units set to centimeters (" + wicReportWidthNumericUpDown.Minimum + @" to " + wicReportWidthNumericUpDown.Maximum + @" centimeters)";
            }
        }

        private string[] ParseSetting(string setting)
        {
            var splitString = new string[2];
            var decimalPointFound = false;
            var numberEnd = false;
            var number = new StringBuilder();
            var unit = new StringBuilder();
            foreach(var character in setting)
            {
                if (char.IsNumber(character) && !numberEnd)
                {
                    number.Append(character);
                    continue;
                }

                if(char.IsLetter(character))
                {
                    unit.Append(character);
                }

                if (character == '%')
                {
                    unit.Append("%");
                    break;
                }

                if (character == '.' && !decimalPointFound)
                {
                    decimalPointFound = true;
                    number.Append('.');
                    continue;
                }

                numberEnd = true;
            }
            splitString[0] = number.ToString();
            splitString[1] = unit.ToString();
            return splitString;
        }

        private void UpdateWicReportWidth(object sender, EventArgs e)
        {
            string unit;
            if (wicPercentageRadioButton.Checked)
            {
                unit = "%";
            }
            else if (wicInchesRadioButton.Checked)
            {
                unit = "in";
            }
            else
            {
                unit = "cm";
            }

            Properties.Settings.Default.WicReportWidth = wicReportWidthNumericUpDown.Value + unit;
            Properties.Settings.Default.Save();
        }

        private void UpdateWicHeight(object sender, EventArgs e)
        {
            string unit;
            if (wicPercentageRadioButton.Checked)
            {
                unit = "%";
            }
            else if (wicInchesRadioButton.Checked)
            {
                unit = "in";
            }
            else
            {
                unit = "cm";
            }

            Properties.Settings.Default.WicReportHeight = wicReportHeightNumericUpDown.Value + unit;
            Properties.Settings.Default.Save();
        }

        private void WicRadioButtonCheckedChanged(object sender, EventArgs e)
        {
            if (!((RadioButton)sender).Checked) return;
            SetWicNumericUpDowns((Control)sender);
        }

        private void UpdatePaperKind(object sender, EventArgs e)
        {
            Properties.Settings.Default.ReportPaperKind = paperKindComboBox.SelectedItem.ToString();
            Properties.Settings.Default.Save();
        }
    }
}
