using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using PXP.Crawler.API.Model;

namespace PXP.Crawler.API.Controls
{
    public partial class RegexToolControl : UserControl
    {
        public RegexToolControl()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(exprTextEdit.Text);
        }

        private int csColInd = 0;


        private void runSimpleButton_Click(object sender, EventArgs e)
        {
            csColInd = 0;
            srcRichTextBox.SuspendLayout();

            srcRichTextBox.SelectAll();
            srcRichTextBox.SelectionColor = srcRichTextBox.ForeColor;
            srcRichTextBox.SelectionBackColor = srcRichTextBox.BackColor;

            resTreeList.BeginUnboundLoad();
            resTreeList.ClearNodes();

            RegexOptions ropt = RegexOptions.Multiline;
            if (multilineCheckEdit.Checked)
                ropt = RegexOptions.Singleline;

            Regex rx = new Regex(exprTextEdit.Text, ropt);
            foreach (Match cm in rx.Matches(srcRichTextBox.Text))
            {
                string nText = "Match: " + cm.Index;
                TreeListNode mNode = resTreeList.AppendNode(new object[] { nText }, null);
                
                    for (int cgi = 0; cgi < cm.Groups.Count; cgi++)
                    {
                        Group cg = cm.Groups[cgi];
                        TreeListNode gNode = resTreeList.AppendNode(new object[] { "Group (" + cgi + "): " + cg.Index + "-" + cg.Length }, mNode);

                        string cgv = cg.Value;
                        cgv = cgv.Replace("\n", "\\n");
                        cgv = cgv.Replace("\r", "\\r");
                        cgv = cgv.Replace("\t", "\\t");

                        resTreeList.AppendNode(new object[] { "Value: " + cgv }, gNode);

                        if (cgi > 0 && cm.Groups.Count > 1)
                        {
                            if (csColInd == 0)
                            {
                                srcRichTextBox.Select(cg.Index, cg.Length);
                                srcRichTextBox.SelectionColor = Color.DarkBlue;
                                srcRichTextBox.SelectionBackColor = Color.LightBlue;
                            }
                            else
                            {
                                srcRichTextBox.Select(cg.Index, cg.Length);
                                srcRichTextBox.SelectionColor = Color.DarkGreen;
                                srcRichTextBox.SelectionBackColor = Color.LightGreen;
                            }
                        }
                        else if (cm.Groups.Count == 1)
                        {
                            if (csColInd == 0)
                            {
                                srcRichTextBox.Select(cg.Index, cg.Length);
                                srcRichTextBox.SelectionColor = Color.DarkBlue;
                                srcRichTextBox.SelectionBackColor = Color.LightBlue;
                            }
                            else
                            {
                                srcRichTextBox.Select(cg.Index, cg.Length);
                                srcRichTextBox.SelectionColor = Color.DarkGreen;
                                srcRichTextBox.SelectionBackColor = Color.LightGreen;
                            }
                        }
                        else if (cm.Groups.Count > 1)
                        {
                            srcRichTextBox.Select(cg.Index, cg.Length);
                            srcRichTextBox.SelectionColor = Color.Black;
                            srcRichTextBox.SelectionBackColor = Color.LightGray;
                            csColInd--;
                        }
                        csColInd++;
                        if (csColInd == 2)
                            csColInd = 0;
                }
            }

            srcRichTextBox.ResumeLayout();

            if (Run != null)
                Run(this, EventArgs.Empty);

            resTreeList.EndUnboundLoad();
            resTreeList.ExpandAll();
        }

        private void srcRichTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && !e.Alt && !e.Shift && e.KeyCode == Keys.V)
            {
                e.Handled = true;
                srcRichTextBox.SelectedText = Clipboard.GetText();                
                runSimpleButton.PerformClick();
            }
        }

        private void exprTextEdit_EditValueChanged(object sender, EventArgs e)
        {

        }

        public event EventHandler Run;

        private void exprTextEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                runSimpleButton.PerformClick();
            }
        }
    }
}
