'*************************************************************************
'
'      Author       : Esben Laursen (hyber@hyber.dk)
'      
'      Webpage      : http://www.hyber.dk or http://netsmsgw.sf.net
'
'      Notes        :
'
'      License      : This program is free software; you can redistribute
'                     it and/or modify it under the terms of the GNU
'                     General Public License as published by the Free
'                     Software Foundation version 2 of the License.
'
'*************************************************************************

Imports Microsoft.Win32
Imports System.IO
Imports System.IO.Ports

<Serializable()> _
Public Class smsStatistics
    Dim mcolStats As New smsStatCollection
    Dim mstrStatFile As String
    Dim mdatCreateDate As Date
    Dim mdatSaveDate As Date
    Dim mintCount As Integer
    Dim mstrID As String

    Public Property StatFileID() As String
        Get
            Return mstrID
        End Get
        Set(ByVal value As String)
            mstrID = value
        End Set
    End Property

    Public Property CreateDate() As Date
        Get
            Return mdatCreateDate
        End Get
        Set(ByVal value As Date)
            mdatCreateDate = value
        End Set
    End Property

    Public Property SaveDate() As Date
        Get
            Return mdatSaveDate
        End Get
        Set(ByVal value As Date)
            mdatSaveDate = value
        End Set
    End Property

    Public Property smsTotalCount() As Integer
        Get
            Return mintCount
        End Get
        Set(ByVal value As Integer)
            mintCount = value
        End Set
    End Property

    Public Sub AddEntry(ByVal psmsMsg As smsMsg, ByVal pdatDate As Date, ByVal pbolSuccess As Boolean)
        Try

            Dim stat As New smsStat

            With stat
                .MobileNum = psmsMsg.Number
                .Priority = psmsMsg.Priority
                .SentAt = pdatDate
                .SendSuccessFull = pbolSuccess
            End With

            mcolStats.Add(stat)

            mintCount = mcolStats.Count

        Catch ex As Exception
            Call AddToLog(gstrCriticalErr & ex.Message.ToString, netSMSgw.logUtils.LogLevels.Critical)
        End Try

    End Sub

    Public Property Stats() As smsStatCollection
        Get
            Return mcolStats
        End Get
        Set(ByVal value As smsStatCollection)
            mcolStats = value
        End Set
    End Property

    Public Shared Sub Serialize(ByVal filename As String, ByVal item As smsStatistics)
        Try

            If Not Directory.Exists(Path.GetDirectoryName(filename)) Then
                Directory.CreateDirectory(Path.GetDirectoryName(filename))
            End If

            ' Create a new XmlSerializer instance.
            Dim s As New Xml.Serialization.XmlSerializer(GetType(smsStatistics))

            ' Writing the XML file to disk requires a TextWriter.
            Dim writer As New StreamWriter(filename)


            item.SaveDate = Now

            ' Serialize the object, and close the StreamWriter.
            s.Serialize(writer, item)
            writer.Close()


        Catch x As System.InvalidOperationException
            Throw New Exception("Check class cItem, all derrived classes must be listed with the [ XmlInclude(GetType(derrivedClass)) ] attribute!")
        End Try
    End Sub

    Public Shared Function Deserialize(ByVal filename As String) As smsStatistics
        Try
            If IO.File.Exists(filename) Then
                Dim fs As New IO.FileStream(filename, FileMode.Open)
                Dim w As New Xml.Serialization.XmlSerializer(GetType(smsStatistics))
                Dim xmlReader As Xml.XmlReader = New Xml.XmlTextReader(fs)

                If w.CanDeserialize(xmlReader) Then
                    Dim objStats2 As smsStatistics = CType(w.Deserialize(xmlReader), smsStatistics)

                    fs.Close()
                    Return objStats2
                End If
            End If

            Dim objStat As New smsStatistics
            objStat.CreateDate = Now
            Return objStat

        Catch ex As Exception
            Call AddToLog(gstrCriticalErr & ex.Message.ToString, netSMSgw.logUtils.LogLevels.Critical)
            Dim oStats As New smsStatistics
            Return oStats
        End Try

    End Function


    Public Sub New()
        mstrID = RandomString(30)
    End Sub

    Private Function RandomString(ByVal pintLength As Integer) As String
        Dim builder As New System.Text.StringBuilder
        Dim r As New Random()
        Dim ch As Char
        Dim i As Integer
        Dim i2 As Integer


        For i = 0 To pintLength - 1
            i2 = r.Next(45, 125)
            Select Case Char.IsLetterOrDigit(Convert.ToChar(i2))
                Case True
                    ch = Convert.ToChar(i2)
                    builder.Append(ch)
                Case False
                    i -= 1
            End Select
        Next
        Return builder.ToString()
    End Function
End Class

'<Serializable()> _
Public Class smsStat
    Public MobileNum As String
    Public SentAt As Date
    Public Priority As netSMSgw.smsUtils.smsPriority
    Public SendSuccessFull As Boolean
End Class

'<Serializable()> _
'Public Class smsDateCollection
'    Inherits System.Collections.CollectionBase


'    Public Overridable Function Add(ByVal Value As Date) As Integer
'        MyBase.List.Add(Value)
'    End Function

'    Default Public Overridable Property Item(ByVal Index As Integer) As Date
'        Get
'            Return DirectCast(MyBase.List.Item(Index), Date)
'        End Get
'        Set(ByVal Value As Date)
'            MyBase.List.Item(Index) = Value
'        End Set
'    End Property

'End Class

<Serializable()> _
Public Class smsStatCollection
    Inherits System.Collections.CollectionBase


    Public Overridable Function Add(ByVal Value As smsStat) As Integer
        MyBase.List.Add(Value)
    End Function

    Default Public Overridable Property Item(ByVal Index As Integer) As smsStat
        Get
            Return DirectCast(MyBase.List.Item(Index), smsStat)
        End Get
        Set(ByVal Value As smsStat)
            MyBase.List.Item(Index) = Value
        End Set
    End Property

End Class

