using System;
using System.Linq;
using System.Text.RegularExpressions;
using NotepadEnhanced.AppState;
using NotepadEnhanced.GUI.Controls;
using System.Windows.Forms;
using NotepadEnhanced.Helpers;
using NotepadEnhanced.Localization;
using NotepadEnhanced.Properties;

namespace NotepadEnhanced.Refactexing
{
    public partial class RegexRefactoringForm : Form
    {
        private readonly PlainTextBox _textBox;

        public RegexRefactoringForm(PlainTextBox textBox)
        {
            _textBox = textBox;
            InitializeComponent();
            PopulateList();
        }

        private void PopulateList()
        {
            listBox.Items.Clear();
            listBox.Items.AddRange(UserSettings.Instance.Refactexes.ToArray());
        }


        /// <summary>
        /// Checks to see if a Refactex is selected. If one is then show a messageBox
        /// </summary>
        /// <returns>Returns true, if an item is selected. Otherwise false</returns>
        private bool CheckForSelected()
        {
            if (listBox.SelectedIndex == -1)
            {
                MsgBox.Show(Strings.NoSelRefactex);
                return false;
            }

            return true;
        }

        private void btnRefactor_Click(object sender, EventArgs e)
        {
            foreach (Refactex r in listBox.SelectedItems.Cast<Refactex>())
            {
                try
                {
                    if (_textBox.SelectionLength > 0)
                    {
                        _textBox.SelectedText = Regex.Replace(_textBox.SelectedText, 
                            r.Pattern, r.Replacement, r.Options);
                    }
                    else
                    {
                        _textBox.Text = Regex.Replace(_textBox.Text, r.Pattern, r.Replacement, r.Options);
                    }
                }
                catch (Exception ex)
                {
                    string firstMsg = String.Format(Strings.RefactexErrorFormat);
                    MsgBox.Show(firstMsg + "\n\n" + ex.Message);
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            bool sel = CheckForSelected();

            if (sel)
            {
                var selRefact = (Refactex)listBox.SelectedItem;

                using (var frmEditRefactex = new EditRefactexForm(selRefact))
                {
                    if (frmEditRefactex.ShowDialog() == DialogResult.OK)
                    {
                        UserSettings.Instance.Refactexes[listBox.SelectedIndex] = frmEditRefactex.Refactex;
                        int selIndex = listBox.SelectedIndex;
                        PopulateList();
                        listBox.SelectedIndex = selIndex;
                    }
                }
            }
        }

        private void UpdateDescription()
        {
            if (listBox.SelectedItem != null)
            {
                lblDescription.Text = ((Refactex)listBox.SelectedItem).Description;
            }
            else
            {
                lblDescription.Text = string.Empty;
            }
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            bool sel = CheckForSelected();

            if (sel)
            {
                for (int i = listBox.SelectedIndices.Count - 1; i >= 0; i--)
                {
                    UserSettings.Instance.Refactexes.RemoveAt(listBox.SelectedIndices[i]);
                }

                PopulateList();
                UpdateDescription();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (var dlgAddRefactex = new AddRefactexForm())
            {
                dlgAddRefactex.ShowDialog();
                
                while (dlgAddRefactex.ToBeAdded.Count > 0)
                {
                    UserSettings.Instance.Refactexes.Add(dlgAddRefactex.ToBeAdded.Pop());
                }

                PopulateList();
            }
        }

        private void listBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRefactor.Enabled = (listBox.SelectedItems.Count > 0);
        }
    }
}
