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

namespace 开发辅助工具.Tools
{
    public partial class FrmStrToCode : DockContent
    {
        public FrmStrToCode()
        {
            InitializeComponent();
            textEditorControl1.IsReadOnly = true;
        }
        private void Convert()
        {
            StringBuilder builder = new StringBuilder();
            string format = "C#";
            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(\"") + "\");");
                format = "C#";
            }
            else if (RbStrToJS.Checked)
            {
                builder.Append("var sb=\"" + this.TxtFromCode.Text.Trim().Replace("\"", "\\\"").Replace("\r\n", "\n").Replace("\n", "\";\r\n sb=sb+\"") + "\";");
                format = "JavaScript";
            }
            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>");
                format = "HTML";
            }
            else if (RbStrToASP.Checked)
            {
                builder.Append("sb=\"" + this.TxtFromCode.Text.Trim().Replace("\"", "\"\"").Replace("\r\n", "\n").Replace("\n", "\" & vbCrLf \r\nsb=sb & \"") + "\"");
                format = "ASP/XHTML";
            }
            textEditorControl1.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy(format);
            textEditorControl1.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();
        }
    }
}
