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.XtraEditors
Imports DevExpress.ExpressApp.HtmlPropertyEditor.Win
Imports System.Windows.Forms
Imports System.Net
Imports System.IO

Public Class SISChatVC
    Inherits SISBusiness.Module.SISChatVC
    Private ChatTimerCore As Timer
#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 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
            AddHandler Me.MyAutoscroll, AddressOf MyAutoscroll_Scroll
            Dim _HTMLBody As DevExpress.ExpressApp.Validation.Win.MemoEditStringPropertyEditor = TryCast(View, DetailView).FindItem("HTMLBody")
            If _HTMLBody IsNot Nothing Then
                AddHandler _HTMLBody.ControlCreated, AddressOf HTMLBody_ControlCreated

            End If
            InitChatTimerCore()
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If ChatTimerCore IsNot Nothing Then
            RemoveHandler ChatTimerCore.Tick, AddressOf ChatTimerCore_Tick
        End If
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()

        If View.Id = "SISChat_DetailView" Then
            Dim _HtmlPropertyEditor As HtmlPropertyEditor

            _HtmlPropertyEditor = TryCast(View, DetailView).FindItem("HTMLEvents")
            If _HtmlPropertyEditor IsNot Nothing Then
                If _HtmlPropertyEditor.Editor IsNot Nothing Then
                    If _HtmlPropertyEditor.Editor.TabControl IsNot Nothing Then
                        If _HtmlPropertyEditor.Editor.TabControl.TabPages(1) IsNot Nothing Then
                            _HtmlPropertyEditor.Editor.TabControl.TabPages(1).PageVisible = False
                        End If
                    End If
                    If _HtmlPropertyEditor.Editor.BrowserControl IsNot Nothing Then
                        _HtmlPropertyEditor.Editor.BrowserControl.Document.Window.ScrollTo(0, Int16.MaxValue)
                    End If
                End If
            End If
        End If

    End Sub
    Private Sub MyAutoscroll_Scroll(sender As Object, e As EventArgs)
        Dim _DetailView As DetailView = TryCast(View, DetailView)
        If _DetailView IsNot Nothing Then
            Dim _HtmlPropertyEditor As HtmlPropertyEditor = TryCast(View, DetailView).FindItem("HTMLEvents")
            If _HtmlPropertyEditor IsNot Nothing Then
                If _HtmlPropertyEditor.Editor IsNot Nothing Then
                    _HtmlPropertyEditor.Editor.BrowserControl.Document.Window.ScrollTo(0, Int16.MaxValue)
                End If
            End If
        End If
    End Sub
    Protected Overridable Sub InitChatTimerCore()
        ChatTimerCore = New Timer()
        AddHandler ChatTimerCore.Tick, AddressOf ChatTimerCore_Tick
        ChatTimerCore.Interval = 3000
        ChatTimerCore.Start()
    End Sub

    Private Sub ChatTimerCore_Tick(sender As Object, e As EventArgs)
        '// Itt kell lekérni a cuccokat
        ChatTimerCore.Enabled = False
        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.eOut)

        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

            If _SISChat.HTMLEvents <> str Then
                _SISChat.HTMLEvents = str
                MyAutoscroll_Scroll(sender, e)
            End If
        Catch exp As Exception
            _SISChat.HTMLEvents = exp.Message

        End Try

        ChatTimerCore.Enabled = True
    End Sub

    Private Sub HTMLBody_KeyUp(sender As Object, e As KeyEventArgs)
        If e.KeyCode = 13 Then

            Frame.GetController(Of SISChatVC).aSISChatSubmit.DoExecute()
            e.Handled = True
        End If
    End Sub

    Private Sub HTMLBody_ControlCreated(sender As Object, e As EventArgs)
        Dim _HTMLBody As DevExpress.ExpressApp.Validation.Win.MemoEditStringPropertyEditor = TryCast(sender, DevExpress.ExpressApp.Validation.Win.MemoEditStringPropertyEditor)
        If _HTMLBody IsNot Nothing Then
            AddHandler _HTMLBody.Control.KeyUp, AddressOf HTMLBody_KeyUp
        End If
    End Sub
End Class
