'  
'   Rebex Sample Code License
' 
'   Copyright 2026, REBEX CR s.r.o.
'   All rights reserved.
'   https://www.rebex.net/
' 
'   Permission to use, copy, modify, and/or distribute this software for any
'   purpose with or without fee is hereby granted.
' 
'   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'   OTHER DEALINGS IN THE SOFTWARE.
' 

Imports System.Collections.Generic
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Public NotInheritable Class Colorize
    Private Sub New()
    End Sub
    Private Shared ReadOnly Keywords As Dictionary(Of String, String)
    Private Shared ReadOnly Classes As Dictionary(Of String, String)

    Shared Sub New()
        '#Region "Add keywords and class names"

        Keywords = New Dictionary(Of String, String)()
        Keywords.Add("String", Nothing)
        Keywords.Add("Integer", Nothing)
        Keywords.Add("For", Nothing)
        Keywords.Add("Each", Nothing)
        Keywords.Add("Dim", Nothing)
        Keywords.Add("As", Nothing)
        Keywords.Add("Loop", Nothing)
        Keywords.Add("While", Nothing)
        Keywords.Add("Not", Nothing)
        Keywords.Add("If", Nothing)
        Keywords.Add("Then", Nothing)
        Keywords.Add("Else", Nothing)
        Keywords.Add("End", Nothing)
        Keywords.Add("True", Nothing)
        Keywords.Add("False", Nothing)

        Classes = New Dictionary(Of String, String)()
        Classes.Add("MessageBox", Nothing)
        Classes.Add("ScriptEvent", Nothing)
        Classes.Add("ScriptMatch", Nothing)

            '#End Region
        Classes.Add("Scripting", Nothing)
    End Sub

    Public Shared Sub SetCode(textBox As RichTextBox, code As String)
        textBox.Clear()

        If code Is Nothing Then
            textBox.SelectionColor = Color.Red
            textBox.AppendText("Code not available.")
            Return
        End If

        textBox.SelectionColor = Color.Black

        Dim start As Integer
        start = code.IndexOf("Shared Sub Execute")
        If start < 0 Then
            Return
        End If
        start = code.IndexOf(ControlChars.Lf, start)
        If start < 0 Then
            Return
        End If
        Dim [end] As Integer = code.IndexOf("' end of script", start)
        If [end] < 0 Then
            Return
        End If

        code = code.Substring(start, [end] - start).Trim()
        code = code.Replace(vbTab & vbTab & vbTab, "") ' remove leading 3xTAB
        code = code.Replace("            ", "") ' remove leading 12xSPACE

        Dim pos As Integer = 0
        While pos < code.Length
            Dim isWord As Boolean = Char.IsLetterOrDigit(code(pos))
            Dim isComment As Boolean = False
            Dim isString As Boolean = False

            Dim n As Integer = pos
            While n < code.Length
                Dim c As Char = code(n)
                If Char.IsLetterOrDigit(c) <> isWord Then
                    Exit While
                End If

                If c = "'"c Then
                    isComment = True
                    Exit While
                End If

                If c = """"C Then
                    isString = True
                    Exit While
                End If

                n += 1
            End While

            Dim chunk As String = code.Substring(pos, n - pos)
            If isWord Then
                If Keywords.ContainsKey(chunk) Then
                    textBox.SelectionColor = Color.Blue
                ElseIf Classes.ContainsKey(chunk) Then
                    textBox.SelectionColor = Color.DarkCyan
                Else
                    textBox.SelectionColor = Color.Black
                End If

                textBox.AppendText(chunk)
            Else
                textBox.SelectionColor = Color.Black
                textBox.AppendText(chunk)
            End If

            pos = n

            If isComment Then
                n = code.IndexOf(ControlChars.Lf, pos)
                If n < 0 Then
                    n = code.Length
                End If

                chunk = code.Substring(pos, n - pos + 1)
                textBox.SelectionColor = Color.DarkGreen
                textBox.AppendText(chunk)
                pos = n + 1
            End If

            If isString Then
                n = code.IndexOf(""""C, pos + 1)
                If n < 0 Then
                    n = code.Length
                End If

                chunk = code.Substring(pos, n - pos + 1)
                textBox.SelectionColor = Color.Brown
                textBox.AppendText(chunk)
                pos = n + 1
            End If
        End While
    End Sub
End Class
