using FastColoredTextBoxNS;
using HtmlAgilityPack;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
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 FrmXpath : DockContent
    {
        public FrmXpath()
        {
            InitializeComponent();
            CbbXMLType.SelectedIndex = 0;
            fastColoredTextBox1.ReadOnly = true;
            fastColoredTextBox1.ClearStylesBuffer();
            fastColoredTextBox1.Range.ClearStyle(StyleIndex.All);
            fastColoredTextBox1.AddStyle(new MarkerStyle(new SolidBrush(Color.FromArgb(40, Color.Gray))));
            fastColoredTextBox1.AutoIndentNeeded -= fctb_AutoIndentNeeded;
            fastColoredTextBox1.Language = Language.JSON;
            fastColoredTextBox1.OnSyntaxHighlight(new TextChangedEventArgs(fastColoredTextBox1.Range));
        }
        private void fctb_AutoIndentNeeded(object sender, AutoIndentEventArgs args)
        {
            //block {}
            if (Regex.IsMatch(args.LineText, @"^[^""']*\{.*\}[^""']*$"))
                return;
            //start of block {}
            if (Regex.IsMatch(args.LineText, @"^[^""']*\{"))
            {
                args.ShiftNextLines = args.TabLength;
                return;
            }
            //end of block {}
            if (Regex.IsMatch(args.LineText, @"}[^""']*$"))
            {
                args.Shift = -args.TabLength;
                args.ShiftNextLines = -args.TabLength;
                return;
            }
            //label
            if (Regex.IsMatch(args.LineText, @"^\s*\w+\s*:\s*($|//)") &&
                !Regex.IsMatch(args.LineText, @"^\s*default\s*:"))
            {
                args.Shift = -args.TabLength;
                return;
            }
            //some statements: case, default
            if (Regex.IsMatch(args.LineText, @"^\s*(case|default)\b.*:\s*($|//)"))
            {
                args.Shift = -args.TabLength / 2;
                return;
            }
            //is unclosed operator in previous line ?
            if (Regex.IsMatch(args.PrevLineText, @"^\s*(if|for|foreach|while|[\}\s]*else)\b[^{]*$"))
                if (!Regex.IsMatch(args.PrevLineText, @"(;\s*$)|(;\s*//)"))//operator is unclosed
                {
                    args.Shift = args.TabLength;
                    return;
                }
        }

        private void RySearchXpath_OnSearch(object sender, EventArgs e)
        {
            string text = "";
            try
            {
                if (CbbXMLType.SelectedIndex == 0)
                {
                    fastColoredTextBox1.Language = Language.XML;
                    System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
                    xml.LoadXml(TxtXML.Text);
                    var node = xml.SelectNodes(RySearchXpath.Text);
                    if (node != null)
                    {
                        for (int i = 0; i < node.Count; i++)
                        {
                            if (text != "") { text += "\r\n\r\n"; }
                            text += node[i].OuterXml;
                        }
                    }
                }
                else if (CbbXMLType.SelectedIndex == 1)
                {
                    fastColoredTextBox1.Language = Language.HTML;
                    HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                    htmlDoc.LoadHtml(TxtXML.Text);
                    HtmlNodeCollection anchors = htmlDoc.DocumentNode.SelectNodes(RySearchXpath.Text);
                    if (anchors != null)
                    {
                        for (int i = 0; i < anchors.Count; i++)
                        {
                            if (text != "") { text += "\r\n\r\n"; }
                            text += anchors[i].OuterHtml;
                        }
                    }
                }
                else if (CbbXMLType.SelectedIndex == 2)
                {
                    fastColoredTextBox1.Language = Language.JSON;
                    try
                    {
                        JObject jo = (JObject)JsonConvert.DeserializeObject(TxtXML.Text);
                        var list= jo.SelectTokens(RySearchXpath.Text).ToList();
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (text != "") { text += "\r\n\r\n"; }
                            text += list[i].ToString();
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            fastColoredTextBox1.Text = text;
        }

        private void FrmXpath_Load(object sender, EventArgs e)
        {

        }

        private void TxtXML_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;
            }
        }

        private void CbbXMLType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CbbXMLType.SelectedIndex == 2) { label1.Text = "JPath表达式"; }
            else { label1.Text = "XPath表达式"; }
        }
    }
}
