using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace DataOrganizer
{
    public partial class NamingConventionManager : Form
    {
        private int ActivePatternId { get; set; }

        public NamingConventionManager()
        {
            InitializeComponent();

            RefreshNamingConventions();

            NameTextBox.TextChanged += TextChanged;
            RegexTextBox.TextChanged += TextChanged;
            ExampleTextBox.TextChanged += TextChanged;
            RegexTestTextBox.TextChanged += RegexTestTextBox_TextChanged;
            ActivePatternId = 0;
        }

        private void RegexTestTextBox_TextChanged(object sender, EventArgs e)
        {
            if (RegexTestTextBox.Text.Length > 0 && RegexTextBox.Text.Length > 0) TestRegexButton.Enabled = true;
            else TestRegexButton.Enabled = false;
        }

        private new void TextChanged(object sender, EventArgs e)
        {
            if (RegexTestTextBox.Text.Length > 0 && RegexTextBox.Text.Length > 0) TestRegexButton.Enabled = true;
            else TestRegexButton.Enabled = false;

            if (NameTextBox.Text.Length > 0 && RegexTextBox.Text.Length > 0 && ExampleTextBox.Text.Length > 0)
            {
                AddUpdateButton.Enabled = true;
            }
            else
            {
                AddUpdateButton.Enabled = false;
            }
        }

        private void NamingConventionPatternsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (NamingConventionPatternsComboBox.SelectedIndex == 0)
            {
                ClearForm();
                return;
            }
            else AddUpdateButton.Text = $"Update Convention";

            var db = new DB();
            var convention = db.GetNamingConventionByName(NamingConventionPatternsComboBox.Text);

            NameTextBox.Text = convention.Name;
            RegexTextBox.Text = convention.Regex;
            ExampleTextBox.Text = convention.ExampleText;
            ActivePatternId = convention.Id;

            PatternIdLabel.Text = $"Pattern ID: {ActivePatternId}";
            DeleteButton.Enabled = true;
        }

        private void ClearForm()
        {
            NameTextBox.Text = string.Empty;
            RegexTextBox.Text = string.Empty;
            ExampleTextBox.Text = string.Empty;
            ActivePatternId = 0;
            AddUpdateButton.Enabled = false;
            AddUpdateButton.Text = "Insert Pattern";
            PatternIdLabel.Text = "Pattern ID: N/A";
            DeleteButton.Enabled = false;
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show($"Are you sure you want to delete the '{NamingConventionPatternsComboBox.Text}'? This will reset all folder groups and files that use this pattern to 'None'.", 
                                         $"Delete '{NamingConventionPatternsComboBox.Text}'", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if(result == DialogResult.Yes)
            {
                var db = new DB();
                db.DeleteNamingConvention(ActivePatternId);

                ClearForm();

                RefreshNamingConventions();
            }
        }

        private void TestRegexButton_Click(object sender, EventArgs e)
        {
            try
            {
                var regex = new Regex(RegexTextBox.Text);
                var testString = RegexTestTextBox.Text;

                var match = regex.Match(testString);

                if (!match.Success)
                {
                    RegexResultsTextBox.Text = $"Input: {testString}{Environment.NewLine}No matches found.";
                    return;
                }

                RegexResultsTextBox.Text = string.Empty;

                foreach (Group m in match.Groups)
                {
                    if(m.Name == "0")
                    {
                        RegexResultsTextBox.Text += $"Input: {m.Value}{Environment.NewLine}";
                        continue;
                    }
                    RegexResultsTextBox.Text += $"{m.Name}: {m.Value}{Environment.NewLine}";
                }
            }
            catch(Exception ex)
            {
                RegexResultsTextBox.Text = $"Error:{Environment.NewLine}{ex.Message}";
            }
        }

        private void AddUpdateButton_Click(object sender, EventArgs e)
        {
            var db = new DB();

            if(ActivePatternId == 0)
            {
                db.InsertNamingConvention(NameTextBox.Text, RegexTextBox.Text, ExampleTextBox.Text);
                ClearForm();
                RefreshNamingConventions();
            }
            else
            {
                db.UpdateNamingConvention(ActivePatternId, NameTextBox.Text, RegexTextBox.Text, ExampleTextBox.Text);
                PatternIdLabel.Text = $"Pattern ID: {ActivePatternId} (Updated)";
            }
        }

        private void RefreshNamingConventions()
        {
            NamingConventionPatternsComboBox.SelectedIndexChanged -= NamingConventionPatternsComboBox_SelectedIndexChanged;
            NamingConventionPatternsComboBox.Items.Clear();

            var db = new DB();
            NamingConventionPatternsComboBox.Items.Add("Add New Pattern");
            NamingConventionPatternsComboBox.SelectedIndex = 0;

            foreach (var pattern in db.GetNamingConventions())
            {
                NamingConventionPatternsComboBox.Items.Add(pattern.Name);
            }

            NamingConventionPatternsComboBox.SelectedIndexChanged += NamingConventionPatternsComboBox_SelectedIndexChanged;
        }
    }
}
