using FastColoredTextBoxNS;
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 FrmStrToCode : DockContent
    {
        public FrmStrToCode()
        {
            InitializeComponent();
            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 Convert()
        {
            StringBuilder builder = new StringBuilder();
            if (RbStrToC.Checked)
            {
                builder.Append(" StringBuilder sb = new StringBuilder();\r\n");
                builder.Append(" sb.Append(\"" + TxtFromCode.Text.Trim().Replace("\"", "\\\"").Replace("\r\n", "\n").Replace("\n", "\");\r\n sb.Append(\"") + "\");");
                fastColoredTextBox1.Language = FastColoredTextBoxNS.Language.CSharp;
            }
            else if (RbStrToJS.Checked)
            {
                builder.Append("var sb=\"" + this.TxtFromCode.Text.Trim().Replace("\"", "\\\"").Replace("\r\n", "\n").Replace("\n", "\";\r\n sb=sb+\"") + "\";");
                fastColoredTextBox1.Language = FastColoredTextBoxNS.Language.JS;
            }
            else if (RbStrToQuickJS.Checked)
            {
                builder.Append("var sb=\"" + this.TxtFromCode.Text.Trim().Replace("\"", "\\\"").Replace("\r\n", "\n").Replace("\n", "\";\r\n sb=sb+\"") + "\";");
                builder = new StringBuilder(builder.ToString().Replace("script", "scr\"+\"ipt") + " document.write(sb);");
                builder.Insert(0, "<script>\r\n");
                builder.Append ("\r\n</script>");
                fastColoredTextBox1.Language = FastColoredTextBoxNS.Language.HTML;
            }
            else if (RbStrToASP.Checked)
            {
                builder.Append("sb=\"" + this.TxtFromCode.Text.Trim().Replace("\"", "\"\"").Replace("\r\n", "\n").Replace("\n", "\" & vbCrLf \r\nsb=sb & \"") + "\"");
                fastColoredTextBox1.Language = FastColoredTextBoxNS.Language.VB;
            }
            fastColoredTextBox1.Text = builder.ToString();
        }

        private void RbStrToC_CheckedChanged(object sender, EventArgs e)
        {
            Convert();
        }

        private void RbStrToJS_CheckedChanged(object sender, EventArgs e)
        {
            Convert();
        }

        private void RbStrToQuickJS_CheckedChanged(object sender, EventArgs e)
        {
            Convert();
        }

        private void TxtFromCode_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 TxtFromCode_TextChanged(object sender, EventArgs e)
        {
            Convert();
        }
    }
}
