'/////////////////////////////////////////////////////////////////////////////////////////////
'//
'// AlertControl figyelmeztető a következő objektumokra
'// HRPersonTask
'//
'//
'////////////////////////////////////////////////////////////////////////////////////////////

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 System.Windows.Forms
Imports DevExpress.XtraBars.Alerter
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.Persistent.BaseImpl
Imports System.Net
Imports System.IO

Public Class AlertWC
    Inherits DevExpress.ExpressApp.WindowController
    Private alertControlCore As AlertControl
    Private alertTimerCore As Timer
    Private alertTimerCoreLoggedOn As Timer

    Private SISPushActivity As Timer
    Public Sub New()
        MyBase.New()

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

        TargetWindowType = WindowType.Main
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        InitAlertTimerCore()
        InitAlertControlCore()
        InitAlertControlCoreLoggedOn()
        InitSISPushActivity()
    End Sub
    Private Sub InitSISPushActivity()
        SISPushActivity = New Timer
        AddHandler SISPushActivity.Tick, AddressOf SISPushActivity_Tick
        SISPushActivity.Interval = 900000
        SISPushActivity.Start()
    End Sub
    Protected Overridable Sub InitAlertControlCore()
        alertControlCore = New AlertControl()
        alertControlCore.AllowHtmlText = True
        alertControlCore.AllowHotTrack = True
        AddHandler alertControlCore.AlertClick, AddressOf alertControlCore_AlertClick
    End Sub
    Protected Overridable Sub InitAlertTimerCore()
        alertTimerCore = New Timer()
        AddHandler alertTimerCore.Tick, AddressOf alertTimerCore_Tick
        alertTimerCore.Interval = 20000
        alertTimerCore.Start()
    End Sub
    Protected Overridable Sub InitAlertControlCoreLoggedOn()
        alertTimerCoreLoggedOn = New Timer()
        AddHandler alertTimerCoreLoggedOn.Tick, AddressOf alertTimerCoreLoggedOn_Tick
        alertTimerCoreLoggedOn.Interval = 10
        alertTimerCoreLoggedOn.Start()
    End Sub

    Private Sub alertTimerCore_Tick(ByVal sender As Object, ByVal e As EventArgs)
        alertTimerCore.Enabled = False
        ActivateAlert_HRPersonTask()
        ActivateAlert_HRPersonTask_Alias()
        ActivateAlert_CRM_Leads_Activity_Task()
        alertTimerCore.Enabled = True
    End Sub
    Private Sub alertTimerCoreLoggedOn_Tick(sender As Object, e As EventArgs)
        alertTimerCoreLoggedOn.Enabled = False

        InitAlartControlLoggedOn()
    End Sub
    Private Sub ActivateAlert_HRPersonTask()
        If Application Is Nothing Then Exit Sub

        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _CurrentUser = _uow.GetObjectByKey(Of User)(_uow.GetKeyValue(SecuritySystem.CurrentUser))
        Dim _HRPerson As HRPerson = _uow.FindObject(Of HRPerson)(CriteriaOperator.Parse("User=?", _CurrentUser))

        Dim _HRPersonTasks_Collection As New XPCollection(Of HRPersonTasks)(_uow, CriteriaOperator.Parse("HRPerson=? and Status in ('InProgress','NotStarted','WaitingForSomeoneElse') and isnull(AlertPeriod)=False", _HRPerson))
        Dim _HRPersonTasks As HRPersonTasks
        Dim _AlertMinute As Long = 0

        If Application Is Nothing Then Exit Sub
        If Application.MainWindow Is Nothing Then Exit Sub
        If Application.MainWindow.Template Is Nothing Then Exit Sub

        Dim mainForm As Form = CType(Application.MainWindow.Template, Form)

        Dim i As Long = 0

        For Each _HRPersonTasks In _HRPersonTasks_Collection
            '// itt kell az ellenőrzés !
            If DateAdd(DateInterval.Day, _HRPersonTasks.AlertPeriod.DateDayBefore, GetSQLTime(_uow)) >= _HRPersonTasks.DueDate Then
                _AlertMinute = (_HRPersonTasks.AlertPeriod.PeriodHour * 60) + _HRPersonTasks.AlertPeriod.PeriodMinute
                If _AlertMinute > 0 Then
                    If GetSQLTime(_uow).Minute Mod _AlertMinute = 0 Then
                        Dim info As New AlertInfo("Figyelem !", _HRPersonTasks.Subject, _HRPersonTasks.Description, ImageLoader.Instance.GetImageInfo("BO_Task").Image, _HRPersonTasks)
                        alertControlCore.Show(mainForm, info)
                        i += 1
                    End If
                End If
            End If

            If i > 50 Then Exit For '// Cask az első ötven jön fel !
        Next

        _HRPersonTasks_Collection = New XPCollection(Of HRPersonTasks)(_uow, CriteriaOperator.Parse("HRPersonAlias=? and Status in ('InProgress','NotStarted','WaitingForSomeoneElse') and isnull(AlertPeriod)=False and DueDate<?", _HRPerson, GetSQLTime(_uow)))
        i = 0
        For Each _HRPersonTasks In _HRPersonTasks_Collection
            _AlertMinute = (_HRPersonTasks.AlertPeriod.PeriodHour * 60) + _HRPersonTasks.AlertPeriod.PeriodMinute
            If _AlertMinute > 0 Then
                If GetSQLTime(_uow).Minute Mod _AlertMinute = 0 Then
                    Dim info As New AlertInfo("Figyelem ! Lejárt kiadott feladat !", _HRPersonTasks.Subject, _HRPersonTasks.Description, ImageLoader.Instance.GetImageInfo("warning").Image, _HRPersonTasks)
                    alertControlCore.Show(mainForm, info)
                    i += 1
                End If
            End If
            If i > 50 Then Exit For
        Next
    End Sub
    Private Sub ActivateAlert_CRM_Leads_Activity_Task()
        If Application Is Nothing Then Exit Sub

        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _CurrentUser = _uow.GetObjectByKey(Of User)(_uow.GetKeyValue(SecuritySystem.CurrentUser))
        Dim _HRPerson As HRPerson = _uow.FindObject(Of HRPerson)(CriteriaOperator.Parse("User=?", _CurrentUser))

        Dim _CRM_Leads_Activity_Task_Collection As New XPCollection(Of CRM_Leads_Activity_Task)(_uow, CriteriaOperator.Parse("HRPerson=? and Status in ('InProgress','NotStarted','WaitingForSomeoneElse') and isnull(AlertPeriod)=False", _HRPerson))
        Dim _CRM_Leads_Activity_Task As CRM_Leads_Activity_Task
        Dim mainForm As Form = CType(Application.MainWindow.Template, Form)

        Dim i As Long = 0

        For Each _CRM_Leads_Activity_Task In _CRM_Leads_Activity_Task_Collection
            '// itt kell az ellenőrzés !
            If DateAdd(DateInterval.Day, _CRM_Leads_Activity_Task.AlertPeriod.DateDayBefore, GetSQLTime(_uow)) >= _CRM_Leads_Activity_Task.DueDate Then
                If _CRM_Leads_Activity_Task.AlertPeriod IsNot Nothing Then
                    If _CRM_Leads_Activity_Task.AlertPeriod.PeriodMinute > 0 Then
                        If GetSQLTime(_uow).Minute Mod _CRM_Leads_Activity_Task.AlertPeriod.PeriodMinute = 0 Then
                            Dim info As New AlertInfo("Figyelem !", _CRM_Leads_Activity_Task.Subject, _CRM_Leads_Activity_Task.Description, ImageLoader.Instance.GetImageInfo("BO_Task").Image, _CRM_Leads_Activity_Task)
                            alertControlCore.Show(mainForm, info)
                            i += 1
                        End If
                    End If
                End If
            End If
            If i > 50 Then Exit For '// Cask az első ötven jön fel !
        Next
    End Sub
    Private Sub alertControlCore_AlertClick(sender As Object, e As AlertClickEventArgs)
        If Application Is Nothing Then Exit Sub

        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _HRPersonTasks As HRPersonTasks
        Dim _ShowViewParameters As New ShowViewParameters
        Dim _ShowViewSource As New ShowViewSource(Frame, Nothing)
        Dim _CRM_Leads_Activity_Task As CRM_Leads_Activity_Task

        _HRPersonTasks = TryCast(e.Info.Tag, HRPersonTasks)
        If _HRPersonTasks IsNot Nothing Then
            If _HRPersonTasks.IsView Then
                If _HRPersonTasks.ViewID <> "" Then

                    _ShowViewParameters.CreatedView = Application.CreateDashboardView(_onew, _HRPersonTasks.ViewID, True)
                    _ShowViewParameters.NewWindowTarget = NewWindowTarget.MdiChild
                    _ShowViewParameters.TargetWindow = TargetWindow.NewWindow
                    Application.ShowViewStrategy.ShowView(_ShowViewParameters, _ShowViewSource)
                End If
            Else
                _ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "HRPersonTasks_DetailView", True, _onew.GetObject(_HRPersonTasks))
                _ShowViewParameters.NewWindowTarget = NewWindowTarget.MdiChild
                _ShowViewParameters.TargetWindow = TargetWindow.NewWindow

                Application.ShowViewStrategy.ShowView(_ShowViewParameters, _ShowViewSource)
            End If
        End If
        _CRM_Leads_Activity_Task = TryCast(e.Info.Tag, CRM_Leads_Activity_Task)
        If _CRM_Leads_Activity_Task IsNot Nothing Then
            _ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "CRM_Leads_Activity_Task_DetailView", True, _onew.GetObject(_CRM_Leads_Activity_Task))
            _ShowViewParameters.NewWindowTarget = NewWindowTarget.MdiChild
            _ShowViewParameters.TargetWindow = TargetWindow.NewWindow

            Application.ShowViewStrategy.ShowView(_ShowViewParameters, _ShowViewSource)
        End If
    End Sub
    Private Sub ActivateAlert_HRPersonTask_Alias()
        If Application Is Nothing Then Exit Sub

        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _CurrentUser = _uow.GetObjectByKey(Of User)(_uow.GetKeyValue(SecuritySystem.CurrentUser))
        Dim _HRPerson As HRPerson = _uow.FindObject(Of HRPerson)(CriteriaOperator.Parse("User=?", _CurrentUser))

        Dim _HRPersonTasks_Collection As New XPCollection(Of HRPersonTasks) _
            (_uow, CriteriaOperator.Parse("DueDate <= ? and HRPersonAlias=? and Status in ('InProgress','NotStarted','WaitingForSomeoneElse') and isnull(AlertPeriodAlias)=False", GetSQLTime(_uow), _HRPerson))
        Dim _HRPersonTasks As HRPersonTasks

        If Application Is Nothing Then Exit Sub
        If Application.MainWindow Is Nothing Then Exit Sub


        Dim mainForm As Form = CType(Application.MainWindow.Template, Form)

        Dim i As Long = 0

        For Each _HRPersonTasks In _HRPersonTasks_Collection

            Dim info As New AlertInfo("Figyelem !", _HRPersonTasks.Subject, _HRPersonTasks.Description, ImageLoader.Instance.GetImageInfo("BO_Task").Image, _HRPersonTasks)
            alertControlCore.Show(mainForm, info)
            i += 1

            If i > 50 Then Exit For '// Cask az első ötven jön fel !
        Next
    End Sub
    Private Sub InitAlartControlLoggedOn()
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _CurrentUser = _uow.GetObjectByKey(Of User)(_uow.GetKeyValue(SecuritySystem.CurrentUser))
        Dim _HRPerson As HRPerson = _uow.FindObject(Of HRPerson)(CriteriaOperator.Parse("User=?", _CurrentUser))
        Dim _HRPersonTasks_Collection As New XPCollection(Of HRPersonTasks)(_uow, CriteriaOperator.Parse("HRPerson=? AND AlertPeriod.AlertType='eOnLoggedOn' AND Status<>4", _HRPerson))

        If Application IsNot Nothing Then
            If Application.MainWindow IsNot Nothing Then
                If Application.MainWindow.Template IsNot Nothing Then
                    Dim mainForm As Form = CType(Application.MainWindow.Template, Form)

                    For Each _HRPersonTasks As HRPersonTasks In _HRPersonTasks_Collection

                        Dim info As New AlertInfo("Figyelem !", _HRPersonTasks.Subject, _HRPersonTasks.Description, ImageLoader.Instance.GetImageInfo("lock_time").Image, _HRPersonTasks)
                        alertControlCore.Show(mainForm, info)
                    Next
                End If
            End If
        End If
        
    End Sub

    Private Sub SISPushActivity_Tick(sender As Object, e As EventArgs)
        Dim _infostring As String = GLOBAL_ActivityID
        Try
            _infostring = AESEncrypt(_infostring, "HrX12!!0Zm7764W2", "vXm657YS+!0@mmT")
            _infostring = Convert.ToBase64String(Encoding.UTF8.GetBytes(_infostring))

            Dim _url = "http://sis-r.hu/SISChat/SISPushActivitySSL.ashx?id=" & _infostring
            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())
            _infostring = _reader.ReadToEnd
        Catch ex As Exception

        End Try
    End Sub



End Class
