Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base

Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Data.Filtering

Imports System.Net
Imports System.IO

Public Class SISChatVC
    Inherits DevExpress.ExpressApp.ViewController
#If DEBUG Then
    Private _SISChatURL As String = "http://192.168.1.102/SISChat/SISChat.ashx?id="
#Else
     Private _SISChatURL As String = "http://sis-r.hu/SISChat/SISChat.ashx?id="
#End If

    Public Event MyAutoscroll(sender As Object, e As EventArgs)

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        If View.Id = "SISChat_DetailView" Then
            'Frame.GetController(Of DialogController).AcceptAction.Active.SetItemValue("", False)
        End If
    End Sub

    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "SISChat_DetailView" Then
            'Frame.GetController(Of DialogController).AcceptAction.Active.SetItemValue("", False)
        End If
    End Sub
    Private Sub aSISChatSubmit_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSISChatSubmit.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _SISChat As SISChat = TryCast(View.SelectedObjects(0), SISChat)
        Dim _JSON As String


        _JSON = _SISChat.CreateJSONSerialize(_SISChat.HTMLBody, eChatDirection.eIn)
        _SISChat.HTMLBody = ""
        View.Refresh()

        Try
            Dim URL As String = _SISChatURL & _JSON
            Dim request As HttpWebRequest = TryCast(WebRequest.Create(URL), HttpWebRequest)

            Dim response As HttpWebResponse = TryCast(request.GetResponse, HttpWebResponse)

            Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
            Dim str As String = reader.ReadToEnd

            _SISChat.HTMLEvents = str
            RaiseEvent MyAutoscroll(sender, TryCast(e, System.EventArgs))
        Catch exp As Exception
            _SISChat.HTMLEvents = exp.Message

        End Try
    End Sub

    Private Sub aSISChat_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSISChat.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _SISChat As SISChat
        Dim _JSON As String

        _SISChat = _onew.CreateObject(Of SISChat)()
        _SISChat.User = _onew.FindObject(Of User)(CriteriaOperator.Parse("Oid=?", SecuritySystem.CurrentUser.oid))

        _JSON = _SISChat.GetLastChat
        Try
            Dim URL As String = _SISChatURL & _JSON
            Dim request As HttpWebRequest = TryCast(WebRequest.Create(URL), HttpWebRequest)

            Dim response As HttpWebResponse = TryCast(request.GetResponse, HttpWebResponse)

            Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
            Dim str As String = reader.ReadToEnd

            _SISChat.HTMLEvents = str
        Catch exp As Exception
            _SISChat.HTMLEvents = exp.Message

        End Try


        Dim _ShowViewParameters As New ShowViewParameters
        Dim _ShowViewSource As New ShowViewSource(Frame, Nothing)

        _ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "SISChat_DetailView", False, _SISChat)
        _ShowViewParameters.NewWindowTarget = NewWindowTarget.Separate
        _ShowViewParameters.TargetWindow = TargetWindow.NewWindow
        Application.ShowViewStrategy.ShowView(_ShowViewParameters, _ShowViewSource)
    End Sub
End Class
