using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace 开发辅助工具.Tools
{
    public partial class FrmRegex : DockContent
    {
        public FrmRegex()
        {
            InitializeComponent();
        }

        private void RySearchRegex_OnSearch(object sender, EventArgs e)
        {
            if (RySearchRegex.Text.Trim() == "")
            {
                MessageBox.Show("正则表达式不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            RichRegexOutput.Text = "";
            table1.TableModel.Rows.Clear();
            tabPage10.Text = "列表展示(" + tableModel1.Rows.Count + ")";
            TabTxtResult.Text = "普通结果(正在读取)";
            bool IgnoreCase = ChkIgnoreCase.Checked;
            bool Multiline = ChkMultiline.Checked;
            string RegexText = RySearchRegex.Text;
            string RegexInput = TxtRegexInput.Text;
            table1.BeginUpdate();
            try
            {
                RegexOptions option = new RegexOptions();
                if (IgnoreCase)
                {
                    option = RegexOptions.IgnoreCase;
                }
                if (Multiline) { option = option | RegexOptions.Multiline; }
                Regex reg = new Regex(RegexText, option);
                //返回一个结果集
                MatchCollection result = reg.Matches(RegexInput);
                string[] groups = reg.GetGroupNames();
                string txt = "";
                //遍历每个结果
                for(int n=0;n<result.Count;n++)
                {
                    Match m = result[n];
                    //输出结果
                     txt += m.ToString() + "\r\n";
                    if (m.Groups.Count > 0)
                    {
                        for (int i = 0; i < groups.Length; i++)
                        {
                            XPTable.Models.Row itemList = new XPTable.Models.Row();
                            //需要修改此处
                            itemList.Cells.Add(new XPTable.Models.Cell(m.Groups[groups[i]].Value));//示例
                            itemList.Cells.Add(new XPTable.Models.Cell(m.Index.ToString()));
                            itemList.Cells.Add(new XPTable.Models.Cell(groups[i]));
                            tableModel1.Rows.Add(itemList);
                        }
                    }
                    else
                    {
                        XPTable.Models.Row itemList = new XPTable.Models.Row();
                        //需要修改此处
                        itemList.Cells.Add(new XPTable.Models.Cell(m.ToString()));//示例
                        itemList.Cells.Add(new XPTable.Models.Cell(m.Index.ToString()));
                        itemList.Cells.Add(new XPTable.Models.Cell(""));
                        tableModel1.Rows.Add(itemList);
                    }
                }
                table1.EndUpdate();
                RichRegexOutput.Text = txt;
                TabTxtResult.Text = "普通结果("+result.Count+")";
                tabPage10.Text = "列表展示(" + tableModel1.Rows.Count + ")";
            }
            catch (Exception ex)
            {
                TabTxtResult.Text = "普通结果(" + 0 + ")";
                tabPage10.Text = "列表展示(" + tableModel1.Rows.Count + ")";
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void LnkRegexHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ryCommon.RyFiles.OpenUrl("http://www.runoob.com/regexp/regexp-metachar.html");
        }

        private void TxtRegexInput_DoubleClick(object sender, EventArgs e)
        {
            ryControls.Controls.RichTextBox2 txt = (ryControls.Controls.RichTextBox2)sender;
            开发辅助工具.Controls.FrmText frm = new Controls.FrmText
            {
                Icon = Icon
            };
            frm.richTextBox1.Text = txt.Text;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txt.Text = frm.richTextBox1.Text;
            }
        }
    }
}
