Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Data
Imports CharterDotCom.WebSite.Library
Imports CharterDotCom.CommonEntities.Library



Public Class ErrorLogCollection
    Dim _list As List(Of ErrorLog)
    Dim dt As DataTable = New System.Data.DataTable("ErrorTable")

    Public Sub New()
        _list = New List(Of ErrorLog)
        BuildTable()

    End Sub

    Public Sub Add(ByVal _err As ErrorLog)
        _list.Add(_err)
    End Sub

    Public Function GetAll() As ErrorLog()
        Return _list.ToArray()
    End Function

    Public Sub Remove(ByVal index As Int32)
        _list.RemoveAt(index)
    End Sub

    Public Function Find(ByVal index As Int32) As ErrorLog
        Return _list(index)
    End Function

    Public Function SelectTable() As DataTable
        Return dt
    End Function

    Public Sub Update(ByVal index As Int32, ByVal _err As ErrorLog)
        _list(index) = _err
    End Sub

    Private Sub BuildTable()
        Try
            '###########################################
            'LOGG THE ERROR MESSAGE TO THE ERROR QUEUE
            '###########################################
            Dim tempErrorQLoc As String = ConfigurationManager.AppSettings("ErrorQueueLocation")
            Dim ErrorQueueLocation As String = Environment.MachineName & tempErrorQLoc
            Dim lMessageQComponent As New MessageQComponent
            Dim lmessages As System.Messaging.Message() = lMessageQComponent.GetAllMessages(ErrorQueueLocation)


            Dim errMessageObject As ErrorLog
            Dim _errorMsg As String
            Dim _innerMsg As String
            Dim _date As String



            Dim colError As DataColumn = New DataColumn("Error")
            colError.DataType = System.Type.GetType("System.String")

            Dim colInnerError As DataColumn = New DataColumn("InnerError")
            colInnerError.DataType = System.Type.GetType("System.String")

            Dim colDate As DataColumn = New DataColumn("Date")
            colDate.DataType = System.Type.GetType("System.String")

            dt.Columns.Add(colError)
            dt.Columns.Add(colInnerError)
            dt.Columns.Add(colDate)

            Dim drnew As DataRow

            If Not lmessages Is Nothing Then
                If lmessages.Length > 0 Then

                    For Each lmessage As System.Messaging.Message In lmessages

                        Dim buffIn(lmessage.BodyStream.Length) As Byte
                        lmessage.BodyStream.Position = 0
                        lmessage.BodyStream.Read(buffIn, 0, lmessage.BodyStream.Length)
                        Dim data = System.Text.Encoding.ASCII.GetString(buffIn)
                        'Response.Write(data)
                        errMessageObject = New ErrorLog(data)
                        _errorMsg = errMessageObject.Err
                        _innerMsg = errMessageObject.InnerError
                        _date = errMessageObject.ErrorDate




                        drnew = dt.NewRow()
                        drnew("Error") = _errorMsg
                        drnew("InnerError") = _innerMsg
                        drnew("Date") = _date
                        dt.Rows.Add(drnew)

                    Next



                    'Dim ltextbox As New TextBox
                    'ltextbox.Width = "600"
                    'ltextbox.Rows = "3"
                    'ltextbox.TextMode = TextBoxMode.MultiLine
                    'ltextbox.Text = 
                    'Panel1.Controls.Add(ltextbox)
                    'Next
                Else
                    '############
                    'NO MESSAGES
                    '############

                End If
            Else
                '############
                'NO MESSAGES
                '############


            End If



        Catch ex As Exception
            '##########################################
            'IF THIS FAILS, JUST SKIP THE LOGGING PART
            '##########################################


        End Try
    End Sub





End Class
