using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtensionsDev
{
    public static class MemoEditExtension
    {
        private readonly static string codeKeyWord = "public;private;protect;internal;protected;readonly;virtual;override;abstract;sealed;base;this;break;continue;foreach;static;class;interface;bool;string;double;float;decimal;byte;shout;int;long;ushort;uint;ulong;void;null;extern;const;ref;out;namespace;new;goto;as;case;catch;char;checked;default;delegate;do;else;enum;event;explicit;false;finally;fixed;for;if;implicit;in;is;lock;object;operator;params;return;sbyte;short;sizeof;stackalloc;struct;switch;throw;true;try;typeof;unchecked;unsafe;using;volatile;while;add;and;alias;ascending;args;async;await;by;descending;dynamic;equals;from;get;global;group;init;into;join;let;nameof;nint;not;notnull;nuint;on;or;orderby;partial;record;remove;select;set;value;var;when;where;where;with;yield";
        private readonly static string[] keyWords = codeKeyWord.Split(';');

        private static Color KeywordColor = Color.Blue;

        public static Color StringColor = Color.FromArgb(163, 21, 21);

        private static Color CommentColor = Color.Gray;

        /// <summary>
        /// 代码展示模式
        /// </summary>
        /// <param name="ctrl"></param>
        public static void UseCodeShowMode(this MemoEdit ctrl)
        {
            ctrl.Properties.Appearance.Options.UseFont = true;
            ctrl.Properties.UseAdvancedMode = DevExpress.Utils.DefaultBoolean.True;
            ctrl.Properties.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            ctrl.CustomHighlightText += Ctrl_CustomHighlightText;
            ctrl.UpdateTextHighlight();
        }

        private static void Ctrl_CustomHighlightText(object sender, TextEditCustomHighlightTextEventArgs e)
        {
            HighlightKeyword(e);
            HighlightStrings(e);
            HighlightComment(e);
        }
        private static void HighlightKeyword(TextEditCustomHighlightTextEventArgs e)
        {
            foreach (var item in keyWords)
            {
                e.HighlightWords(item, KeywordColor);
            }
        }
        private static void HighlightComment(TextEditCustomHighlightTextEventArgs e)
        {
            string text = e.Text;
            int index = text.IndexOf("//");
            if (index != -1)
                e.HighlightRange(index, text.Length - index, CommentColor);
            else if (text.StartsWith("/*")||text.StartsWith("*"))
            {
                int endIdx = text.IndexOf("*/");
                if (endIdx == -1)
                    endIdx = text.Length - index;
                e.HighlightRange(0, text.Length, CommentColor);
            }
        }
        private static void HighlightStrings(TextEditCustomHighlightTextEventArgs e)
        {
            string text = e.Text;
            int length = text.Length;
            int startTextIndex = -1;
            while (startTextIndex < length)
            {
                startTextIndex = text.IndexOf('\'', startTextIndex + 1);
                if (startTextIndex == -1) break;
                int endTextIndex = text.IndexOf('\'', startTextIndex + 1);
                if (endTextIndex == -1)
                    endTextIndex = length;
                e.HighlightRange(startTextIndex, endTextIndex - startTextIndex + 1, StringColor);
                startTextIndex = endTextIndex;
            }
            startTextIndex = -1;
            while (startTextIndex < length)
            {
                startTextIndex = text.IndexOf('"', startTextIndex + 1);
                if (startTextIndex == -1) break;
                int endTextIndex = text.IndexOf('"', startTextIndex + 1);
                if (endTextIndex == -1)
                    endTextIndex = length;
                e.HighlightRange(startTextIndex, endTextIndex - startTextIndex + 1, StringColor);
                startTextIndex = endTextIndex;
            }
        }
    }
}
