Imports System
Imports System.ComponentModel

Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation
Imports Newtonsoft.Json
Public Enum eChatDirection
    eIn = 0
    eOut = 1
End Enum
<DefaultClassOptions(), NavigationItem(False), CreatableItem(False), NonPersistent()> _
Public Class SISChat
    Inherits BaseObject

    Private _User As User
    Private _HTMLBody As String
    Private _HTMLEvents As String
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
        ' This constructor is used when an object is loaded from a persistent storage.
        ' Do not place any code here or place it only when the IsLoading property is false:
        ' if (!IsLoading){
        '   It is now OK to place your initialization code here.
        ' }
        ' or as an alternative, move your initialization code into the AfterConstruction method.
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        ' Place here your initialization code.
    End Sub

    Property User As User
        Get
            Return _User
        End Get
        Set(value As User)
            SetPropertyValue("User", _User, value)
        End Set
    End Property
    <Size(-1), ImmediatePostData()> _
    Property HTMLBody As String
        Get
            Return _HTMLBody
        End Get
        Set(value As String)
            SetPropertyValue("HTMLBody", _HTMLBody, value)
        End Set
    End Property
    <Size(-1)> _
    Property HTMLEvents As String
        Get
            Return _HTMLEvents
        End Get
        Set(value As String)
            SetPropertyValue("HTMLEvents", _HTMLEvents, value)
        End Set
    End Property

    Public Function CreateCompressedChat(_InputString As String) As String
        Dim mem As New IO.MemoryStream
        Dim gz As New System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Compress)
        Dim sw As New IO.StreamWriter(gz)
        Dim _OutputString As String = ""
        sw.Write(_InputString)
        sw.Flush()
        mem.Seek(0, IO.SeekOrigin.Begin)
        Dim sr As New IO.BinaryReader(mem)

        Dim _Bytes As Byte()
        Dim _Byte As Byte
        _Bytes = sr.ReadBytes(CInt(mem.Length))

        For Each _Byte In _Bytes
            _OutputString += Chr(_Byte)
        Next

        sw.Close()
        Return _OutputString
    End Function
    Function CreateJSONSerialize(_InputString As String, _ChatDirection As eChatDirection) As String
        Dim _JSON As String = ""
        Dim _JSNOChat As New JSONChat

        _JSNOChat.ChatDirection = _ChatDirection
        _JSNOChat.EMail = _User.Email
        _JSNOChat.HTMLBody = _InputString
        _JSNOChat.IsReaded = False
        _JSON = JsonConvert.SerializeObject(_JSNOChat)

        Return _JSON
    End Function
    Function GetLastChat() As String
        Dim _JSON As String = ""
        Dim _JSNOChat As New JSONChat

        _JSNOChat.ChatDirection = eChatDirection.eOut
        _JSNOChat.EMail = _User.Email
        _JSNOChat.HTMLBody = "*"
        _JSON = JsonConvert.SerializeObject(_JSNOChat)

        Return _JSON
    End Function
    Function CreateHTMLEvents(_JSON As String) As String
        Return "Ready"
    End Function
End Class
Public Class JSONChat
    Property ChatDirection As eChatDirection
    Property EMail As String
    Property HTMLBody As String
    Property IsReaded As Boolean
End Class