using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight;
using C1.Silverlight.RichTextBox;
using C1.Silverlight.RichTextBox.Documents;
using System.Text.RegularExpressions;
using System.IO;

namespace EsriMapSamples.SyntaxHighlighter
{
    public partial class CodeSyntaxHighlighter : UserControl
    {
        C1RangeStyleCollection _rangeStyles = new C1RangeStyleCollection();

        // initialize regular expression used to parse code syntax
        string stringPattern = @"""(?:\\""|.)*?""";
        string commentPattern = @"//[^\n]*\n|/\*(.|[\r\n])*?\*/";

        // reserved words kept to a minimum (not complete)
        string[] reservedWords = new string[] { "if", "public", "int", "double", "string", "bool", "using", "class", "object", "public", "private", "partial", "void", "namespace", "for", "foreach" };

        // initialize styles used to color the document
        C1TextElementStyle brBlue = new C1TextElementStyle 
        { 
            { C1TextElement.ForegroundProperty, new SolidColorBrush(Color.FromArgb(255, 0, 0, 255)) }
        };
        C1TextElementStyle brGreen = new C1TextElementStyle 
        { 
            { C1TextElement.ForegroundProperty, new SolidColorBrush(Color.FromArgb(255, 0, 160, 0)) }
        };
        C1TextElementStyle brDarkRed = new C1TextElementStyle 
        { 
            { C1TextElement.ForegroundProperty, new SolidColorBrush(Color.FromArgb(255, 180, 0, 0)) }
        };

        public CodeSyntaxHighlighter()
        {
            InitializeComponent();
        }

        // perform syntax coloring
        void UpdateSyntaxColoring()
        {
            C1TextRange range = c1RichTextBox1.Document.ContentRange;

            // reset coloring
            _rangeStyles = new C1RangeStyleCollection();
            c1RichTextBox1.StyleOverrides.Clear();
            c1RichTextBox1.StyleOverrides.Add(_rangeStyles);

            var input = range.Text;

            // highlight reserved words first
            foreach (string keyword in reservedWords)
            {
                foreach (Match m in Regex.Matches(input, keyword + " "))
                {
                    // select reserved word, make it blue
                    _rangeStyles.Add(new C1RangeStyle(GetRangeAtTextOffset(range.Start, m), brBlue));
                }
            }

            // highlight strings            
            foreach (Match m in Regex.Matches(input, stringPattern))
            {
                // select string, make it red
                _rangeStyles.Add(new C1RangeStyle(GetRangeAtTextOffset(range.Start, m), brDarkRed));
            }

            // highlight comments           
            foreach (Match m in Regex.Matches(input, commentPattern))
            {
                // select comment, make it green
                _rangeStyles.Add(new C1RangeStyle(GetRangeAtTextOffset(range.Start, m), brGreen));  
            }
        }

        C1TextRange GetRangeAtTextOffset(C1TextPointer pos, Capture capture)
        {
            var start = pos.GetPositionAtOffset(capture.Index, C1TextRange.TextTagFilter);
            var end = start.GetPositionAtOffset(capture.Length, C1TextRange.TextTagFilter);
            return new C1TextRange(start, end);
        }

        // gets and sets the Text of the control
        public string Text
        {
            get
            {
                return c1RichTextBox1.Text;
            }
            set
            {
                c1RichTextBox1.Text = value;
                UpdateSyntaxColoring();
            }
        }
    }
}
