Imports System.Drawing
Imports DigitalData.Modules.EDMI.API
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.ZooFlow
Imports DigitalData.Modules.ZooFlow.Constants
Public Class frmNotification_Startup
    Private ReadOnly NotificationItems As New List(Of NotificationItem)
    Private oDTNotifications As DataTable
    Private ReadOnly Property LogConfig As LogConfig
    Private ReadOnly Property Logger As Logger
    Private ReadOnly Environment As Environment
    Private GDPictureKey As String
    Private MyCaption As String
    Private NOTIFY_ID As Integer
    Private Property OperationMode As OperationMode
    Private Client As Client
    Friend Class NotificationItem
        Public Id As Integer
        Public Title As String
        Public MSGSTRING As String

        Public Overrides Function ToString() As String
            Return Title
        End Function
    End Class
    Public Sub New(DTNotifications As DataTable, pLogConfig As LogConfig, pGDPictureKey As String, pCaption As String, pEnvironment As Environment)
        oDTNotifications = DTNotifications
        LogConfig = pLogConfig
        Logger = pLogConfig.GetLogger()
        GDPictureKey = pGDPictureKey
        MyCaption = pCaption
        Environment = pEnvironment

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub

    Private Sub frmNotification_Startup_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            DocumentViewer1.Init(LogConfig, GDPictureKey)
            OperationMode = GetOperationMode()
            Client = Environment.Service.Client
            Me.Text = MyCaption
            If oDTNotifications.Rows.Count = 1 Then
                Me.ListBoxControl1.Visible = False
                Load_Profile(oDTNotifications.Rows(0).Item("GUID"))
            Else


                For Each oRow As DataRow In oDTNotifications.Rows
                    NotificationItems.Add(New NotificationItem With {
                    .Id = CInt(oRow.Item("GUID")),
                    .Title = oRow.Item("TITLE").ToString
                })
                Next

                ' ListBoxControl1.Items.Clear()
                'ListBoxControl1.Items.AddRange(NotificationItems)



                ListBoxControl1.DataSource = oDTNotifications
                ListBoxControl1.ValueMember = "GUID"
                ListBoxControl1.DisplayMember = "TITLE"
            End If
        Catch ex As Exception
            Dim oMsgBox As New frmDialog(ex.Message, "Unexpected Error loading form:", frmDialog.DialogType.Error)
            oMsgBox.ShowDialog()
        End Try
    End Sub
    Sub Load_Profile(oIndex As Integer)
        Try
            DocumentViewer1.Visible = False
            RichEditControl1.Visible = False
            Dim oType As String, oMSG_STRING As String, oMarkasRead As Boolean

            If oDTNotifications.Rows.Count = 1 Then
                oType = oDTNotifications.Rows(0).Item("DISPLAY_TYPE")
                oMSG_STRING = oDTNotifications.Rows(0).Item("MSG_STRING")
                oMarkasRead = oDTNotifications.Rows(0).Item("VALIDATE_READ")
                NOTIFY_ID = oDTNotifications.Rows(0).Item("GUID")
            Else
                Dim oCount As Integer = 0
                For Each orow As DataRow In oDTNotifications.Rows
                    If oIndex = oCount Then
                        oType = orow.Item("DISPLAY_TYPE")
                        oMSG_STRING = orow.Item("MSG_STRING")
                        oMarkasRead = orow.Item("VALIDATE_READ")
                        NOTIFY_ID = orow.Item("GUID")
                        Exit For
                    Else
                        oCount += 1
                    End If

                Next
            End If

            If oType = "MESSAGE" Then
                RichEditControl1.Text = oMSG_STRING
                RichEditControl1.Visible = True
                RichEditControl1.Dock = Windows.Forms.DockStyle.Fill
            Else
                If IO.File.Exists(oMSG_STRING) Then
                    DocumentViewer1.LoadFile(oMSG_STRING)
                    DocumentViewer1.Visible = True
                    DocumentViewer1.Dock = Windows.Forms.DockStyle.Fill
                Else
                    Dim oMsgBox As New frmDialog("File not existing!", "Load Notification File", frmDialog.DialogType.Warning)
                    oMsgBox.ShowDialog()
                End If
            End If
            If oMarkasRead = True Then
                bbtnitmMarkRead.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
            Else
                bbtnitmMarkRead.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
            End If

        Catch ex As Exception
            Dim oMsgBox As New frmDialog(ex.Message, "Unexpected Error loading Message:", frmDialog.DialogType.Error)
            oMsgBox.ShowDialog()
        End Try
    End Sub

    Private Sub ListBoxControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBoxControl1.SelectedValueChanged
        If ListBoxControl1.SelectedIndex <> -1 Then
            Try
                Load_Profile(ListBoxControl1.SelectedIndex)
            Catch ex As Exception
                Dim oMsgBox As New frmDialog(ex.Message, "Unexpected Error switching Notifications:", frmDialog.DialogType.Error)
                oMsgBox.ShowDialog()
            End Try
        End If
    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        Me.Close()
    End Sub

    Private Sub bbtnitmMarkRead_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbtnitmMarkRead.ItemClick
        Dim oSuccess As Boolean = True
        Try

            Dim oInsert = $"INSERT INTO [dbo].[TBDD_NOTIFICATIONS_USER_HISTORY]
           ([NOTIFY_ID]
           ,[USR_ID])
     VALUES
           ({NOTIFY_ID}
           ,{Environment.User.UserId})"
            If OperationMode = OperationMode.ZooFlow Or OperationMode = OperationMode.WithAppServer Then
                Dim oResponse = Client.ExecuteNonQueryFromECM(oInsert)
                If Not oResponse.OK Then
                    oSuccess = False
                End If
            Else
                oSuccess = Environment.Database.ExecuteNonQuery(oInsert)

            End If
            If oSuccess = False Then
                BarStaticItem1.Caption = "Error while setting History!"
                BarStaticItem1.ItemAppearance.Normal.BackColor = Color.Red
            Else
                BarStaticItem1.Caption = "History successfully saved!"
                BarStaticItem1.ItemAppearance.Normal.BackColor = Color.Green
                If oDTNotifications.Rows.Count = 1 Then
                    Me.Close()
                End If
            End If
        Catch ex As Exception
            Dim oMsgBox As New frmDialog(ex.Message, "Unexpected Error HistoryInsert:", frmDialog.DialogType.Error)
            oMsgBox.ShowDialog()
            oSuccess = False
        End Try

    End Sub
    Private Function GetOperationMode() As OperationMode
        Dim oOperationMode As OperationMode

        If Environment.Service.Client.IsOnline AndAlso Environment.Service.Address <> String.Empty Then
            oOperationMode = OperationMode.WithAppServer
        Else
            oOperationMode = OperationMode.NoAppServer
        End If

        Return oOperationMode
    End Function
End Class