Imports System.Text

Public Class ChatController
    Dim rc As New RenderController
    Dim apppath As String = Application.StartupPath() & "\"
    Dim config As String = apppath & "Config\"

    'This library is designed for handling all chat data and random text from chat.aro folder
    Public Function GetRandomSentence(SentenceFileName As String)
        Dim sb As New StringBuilder
        If SentenceFileName.Contains(".aro") = False Then
            SentenceFileName = SentenceFileName & ".aro"
        End If
        If My.Computer.FileSystem.FileExists(config & SentenceFileName) Then
            Dim objReader As New System.IO.StreamReader(config & SentenceFileName, System.Text.Encoding.GetEncoding(950))
            Dim code As String = ""
            Do While objReader.Peek() <> -1
                code = objReader.ReadLine()
                sb.Append(code) 'Read File Line by line and combine them together
            Loop
            Dim textread As String() = sb.ToString.Split("}")
            'MsgBox(textread.Length) 
            Dim i As Integer = 0
            Dim rndNum As New Random()
            Dim l As Integer = rndNum.Next(1, textread.Length) 'Random to choose a line where ArOZ read
            For Each cmdunit As String In textread
                'DebugWindow.log(cmdunit)
                If i < l Then
                    i += 1
                ElseIf i = l Then
                    DebugWindow.log(i)
                    DebugWindow.log(cmdunit)
                    Return cmdunit.ToString
                End If
                Application.DoEvents()
            Next


        Else
            Return Nothing
        End If
        Return Nothing
    End Function
    Public Function RandomChat(SentenceBank As String)
        Dim sentence As String = ""
        sentence = GetRandomSentence(SentenceBank)
        'Clear up the sentence return from the raw data
        If sentence.Contains("{") Then
            sentence.Replace("{", "")
        End If
        Dim slength As Integer = sentence.Length
        If sentence.Substring(slength - 1, 1) = "|" Then
            sentence.Substring(0, slength - 1)
        End If
        'The following part is the aro language decode
        Dim datachunk As String()
        datachunk = sentence.Split("|")
        For Each data As String In datachunk
            DebugWindow.log("Chat Controller Applied CMD: " & data)
            If data.Contains("interface=") Then
                If data.Contains("{") Then
                    data = data.Replace("{", "")
                End If
                Try
                    Dim expressid As Integer = CInt(data.Replace("interface=", ""))
                    'MsgBox(expressid)
                    ghost.expressid = expressid
                    ghost.UpdateRender()
                Catch ex As Exception

                End Try
            ElseIf data.Contains("delay(") Then
                Try
                    data = data.Replace("delay(", "")
                    data = data.Replace(")", "")
                    Dim time As Integer = CInt(data)
                    rc.delay(time)
                Catch ex As Exception

                End Try
            Else
                If data.Length > 0 Then
                    balloon.DisplayText(data)
                End If

            End If
        Next
        ghost.StartCountDownReset()
        Return Nothing
    End Function



    Public Function ReadConfig(item As String)
        Try
            Dim arc As New ArConfig
            Dim al As String = Application.StartupPath & "\config\"
            Return arc.ReadConfig(al, item).ToString.Trim
        Catch ex As Exception
            Return Nothing
        End Try

    End Function
End Class
