Imports DevExpress.XtraGrid.Views.Tile
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging

Public Class ChatNewConv
    Public Delegate Sub ConversationCreated()
    Public Event Conversation_Created As ConversationCreated
    Private _Database As MSSQLServer
    Private LogConfig As LogConfig
    Private Logger As Logger

    Public IDBObjectId As Long
    Public ConnStringIDB As String
    Public Username As String
    Public UserID As Long
    Public User_Language As String
    Public NewConversation As Long

    Public Sub New()
        InitializeComponent()
    End Sub

    Public Sub Init(LogConfig As LogConfig, ConnectionStringIDB As String, IDB_OBJ_ID As Long, UserID As Long, Username As String, UserLanguage As String, DTUsers As DataTable, DTGroups As DataTable)
        Me.LogConfig = LogConfig
        Me.Logger = LogConfig.GetLogger()
        Me.ConnStringIDB = ConnectionStringIDB
        IDBObjectId = IDB_OBJ_ID
        Me.Username = Username
        Me.UserID = UserID
        Me.User_Language = UserLanguage
        Me._Database = New MSSQLServer(LogConfig, ConnectionStringIDB)
        If Not IsNothing(DTUsers) Then
            If DTUsers.Rows.Count > 1 Then
                AccordionControlElementUser.Visible = True
                GridControlUsers.DataSource = DTUsers
            Else
                AccordionControlElementUser.Visible = False
            End If
        Else
            AccordionControlElementUser.Visible = False
        End If
        If Not IsNothing(DTGroups) Then
            If DTGroups.Rows.Count > 1 Then
                AccordionControlElementGroups.Visible = True
                GridControlGroups.DataSource = DTUsers
                Try
                    GridViewUsers.Columns("UserID").Visible = False
                Catch ex As Exception
                    GridViewUsers.Columns("USER_ID").Visible = False
                End Try

            Else
                AccordionControlElementGroups.Visible = False
            End If
        Else
            AccordionControlElementGroups.Visible = False
        End If

    End Sub

    Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles SimpleButton1.Click
        Try
            If txtConv_Title.Text = String.Empty Then
                MsgBox("Please add a conversation-title!", MsgBoxStyle.Information)
                Exit Sub
            End If
            Dim oSQL = $"EXEC PRIDB_NEW_CONVERSATION {IDBObjectId},'{txtConv_Title.Text}','{Username}','{User_Language}'"
            If _Database.ExecuteNonQuery(oSQL) = True Then
                Dim oError As Boolean = False
                oSQL = $"SELECT MAX(CONVERSATION_ID) FROM VWIDB_CONVERSATION WHERE IDB_OBJ_ID = {IDBObjectId}"
                Dim oCONV_ID = _Database.GetScalarValue(oSQL)
                If Not IsNothing(oCONV_ID) Then
                    NewConversation = oCONV_ID
                    oSQL = $"EXEC PRIDB_ADD_USER_2_CONVERSATION {NewConversation},{UserID},'{Username}'"
                    If _Database.ExecuteNonQuery(oSQL) = True Then
                        Dim oSelectedUsers As Integer() = GridViewUsers.GetSelectedRows()
                        If oSelectedUsers.Count > 0 Then
                            For Each oRowHandle As Integer In oSelectedUsers
                                Dim oUserID
                                Try
                                    oUserID = GridViewUsers.GetRowCellValue(oRowHandle, "UserID")
                                Catch ex As Exception
                                    Try
                                        oUserID = GridViewUsers.GetRowCellValue(oRowHandle, "USER_ID")
                                    Catch ex1 As Exception
                                        Logger.Warn("Error Selecting UserID/USER_ID: " & ex.Message)
                                        oError = True
                                        Exit Sub
                                    End Try


                                End Try

                                oSQL = $"EXEC PRIDB_ADD_USER_2_CONVERSATION {NewConversation},{oUserID},'{Username}'"
                                If _Database.ExecuteNonQuery(oSQL) = False Then
                                    oError = True
                                    Exit For
                                End If
                            Next
                        End If
                        If oError = False Then
                            RaiseEvent Conversation_Created()
                        End If
                    End If

                End If
            Else
                MsgBox("Error running create procedure. Check Your log!", MsgBoxStyle.Information)
            End If

        Catch ex As Exception
            MsgBox($"Unexpected error in Add Conversation: {ex.Message}")
        End Try
    End Sub
End Class
