Imports DigitalData.GUIs.ZooFlow.Administration.ClassConstants
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Language
Imports DigitalData.Modules.Logging

Public Class ClassDetailForm
    Inherits BaseClass

    Public Event DetailFormClosed As EventHandler(Of Form)

    Public ReadOnly Property DetailDataList As New Dictionary(Of String, DetailData)
    Public ReadOnly Property DetailSettingsList As New Dictionary(Of String, DetailSettings) From {
        {PAGE_IDB_ATTRIBUTES, New DetailSettings With {
            .GridTitle = "Attribute",       ' This will be shown above the Data Grid
            .NewRecordTitle = "New Attribute", ' This will be shown on the "New" Button
            .[Module] = MODULE_IDB,
            .Entity = PAGE_IDB_ATTRIBUTES  ' This Key will be matched with the Entity Id from the Database
        }},
        {PAGE_IDB_BUSINESS_ENTITIES, New DetailSettings With {
            .GridTitle = "BusinessEntity",
            .NewRecordTitle = "New BusinessEntity",
            .[Module] = MODULE_IDB,
            .Entity = PAGE_IDB_BUSINESS_ENTITIES
        }},
        {PAGE_IDB_OBJECT_STORES, New DetailSettings With {
            .GridTitle = "Object-Stores",
            .NewRecordTitle = "New Object Store",
            .[Module] = MODULE_IDB,
            .Entity = PAGE_IDB_OBJECT_STORES
        }},
        {PAGE_META_SOURCE_SQL, New DetailSettings With {
            .GridTitle = "Source SQL",
            .NewRecordTitle = "New Source SQL",
            .[Module] = MODULE_META,
            .Entity = PAGE_META_SOURCE_SQL
        }},
        {PAGE_GI_PROFILES, New DetailSettings With {
            .GridTitle = "Global Indexer Profile",
            .NewRecordTitle = "New Globix profile",
            .[Module] = MODULE_GI,
            .Entity = PAGE_GI_PROFILES
        }},
        {PAGE_CW_PROFILES, New DetailSettings With {
            .GridTitle = "Clipboard Watcher Profile",
            .NewRecordTitle = "New Clipwatcher profile",
            .[Module] = MODULE_CW,
            .Entity = PAGE_CW_PROFILES
        }},
        {PAGE_IDB_ATTRIBUTE_REL, New DetailSettings With {
            .GridTitle = "Attribute-Relations",
            .[Module] = MODULE_IDB,
            .Entity = PAGE_IDB_ATTRIBUTES,
            .IsRelationPage = True
        }},
        {PAGE_GI_RELATIONS, New DetailSettings With {
            .GridTitle = "Globix profile-relations",
            .[Module] = MODULE_GI,
            .Entity = PAGE_GI_RELATIONS,
            .IsRelationPage = True
        }},
        {PAGE_IDB_DOCTYPE_CONFIG, New DetailSettings With {
            .GridTitle = "Detail Doctype Configuration",
            .NewRecordTitle = "New Configuration",
            .[Module] = MODULE_IDB,
            .Entity = PAGE_IDB_DOCTYPE_CONFIG
        }},
        {PAGE_IDB_DOCTYPE, New DetailSettings With {
            .GridTitle = "Doctype Configuration",
            .[Module] = MODULE_IDB,
            .Entity = PAGE_IDB_DOCTYPE
        }},
          {PAGE_IDB_DOCTYPE_BE, New DetailSettings With {
            .GridTitle = "Doctype BE Relations",
            .[Module] = MODULE_IDB,
            .Entity = PAGE_IDB_DOCTYPE_BE,
            .IsRelationPage = True
        }},
          {PAGE_USERS_USERLIST, New DetailSettings With {
            .GridTitle = "User Overview",
            .NewRecordTitle = "New User",
            .[Module] = MODULE_USERS,
            .Entity = PAGE_USERS_USERLIST
        }},
        {PAGE_USERS_GROUPLIST, New DetailSettings With {
            .GridTitle = "Group Overview",
            .NewRecordTitle = "New Group",
            .[Module] = MODULE_USERS,
            .Entity = PAGE_USERS_GROUPLIST
        }},
        {PAGE_USERS_USER_GROUP_RELATIONS, New DetailSettings With {
            .GridTitle = "User Group Relations",
            .[Module] = MODULE_USERS,
            .Entity = PAGE_USERS_USER_GROUP_RELATIONS,
            .IsRelationPage = True
        }},
        {PAGE_IDB_COMMON_SQL, New DetailSettings With {
            .GridTitle = "IDB Common SQLs",
            .[Module] = MODULE_USERS,
            .Entity = PAGE_IDB_COMMON_SQL
        }}
    }

    Public Sub New(LogConfig As LogConfig)
        MyBase.New(LogConfig)
    End Sub

    Public Function Handle_OpenDetail(PrimaryKey As Integer, Page As String, IsInsert As Boolean) As Boolean
        Try
            Dim oForm As Form = Nothing

            Select Case Page
                Case PAGE_IDB_ATTRIBUTES
                    oForm = New frmAdmin_IDBAttribute(PrimaryKey) With {.IsInsert = IsInsert}

                Case PAGE_IDB_BUSINESS_ENTITIES
                    oForm = New frmAdmin_IDBEntity(PrimaryKey) With {.IsInsert = IsInsert}

                Case PAGE_IDB_OBJECT_STORES
                    oForm = New frmAdmin_IDBObjectStore(PrimaryKey) With {.IsInsert = IsInsert}

                Case PAGE_CW_PROFILES
                    oForm = New frmAdmin_ClipboardWatcher(PrimaryKey) With {.IsInsert = IsInsert}

                Case PAGE_GI_PROFILES
                    GLOBIX_JUMP_DOCTYPE_ID = PrimaryKey
                    oForm = New frmAdmin_Globix(PrimaryKey) With {.IsInsert = IsInsert}

                Case PAGE_META_SOURCE_SQL
                    oForm = New frmAdmin_SourceSQL(PrimaryKey) With {.IsInsert = IsInsert}

                Case PAGE_IDB_ATTRIBUTE_REL
                    oForm = New frmAdmin_IDBBERelations

                Case PAGE_GI_RELATIONS
                    oForm = New frmAdmin_GlobixRelations

                Case PAGE_IDB_DOCTYPE_CONFIG
                    oForm = New frmAdmin_DoctypeConfig(PrimaryKey) With {.IsInsert = IsInsert}

                Case PAGE_IDB_DOCTYPE
                    oForm = New frmAdmin_Doctype(0)

                Case PAGE_IDB_DOCTYPE_BE
                    oForm = New frmAdmin_DoctypeBE()

                Case PAGE_USERS_USERLIST
                    oForm = New frmAdmin_User(PrimaryKey) With {.IsInsert = IsInsert}

                Case PAGE_USERS_USER_GROUP_RELATIONS
                    oForm = New frmAdmin_UserGroupRelations(PrimaryKey) With {.IsInsert = IsInsert}
                Case PAGE_IDB_COMMON_SQL
                    oForm = New frmIDBCommonSQL
            End Select

            ' If the selected form was valid, open it
            ' and then raise the form closed event for it
            If oForm IsNot Nothing Then
                oForm.ShowDialog()
                RaiseEvent DetailFormClosed(Me, oForm)
                Return True
            Else
                Return False
            End If

        Catch ex As Exception
            Throw ex
        End Try

    End Function

    Public Function LoadData() As Boolean
        Try
            Dim oTable As DataTable = My.DatabaseECM.GetDatatable("SELECT * FROM TBZF_ADMIN_SOURCE_SQL WHERE SCOPE = 'OVERVIEW' ORDER BY GUID")
            DetailDataList.Clear()

            For Each oRow As DataRow In oTable.Rows
                Dim oCHeck = oRow.Item("ENTITY_TITLE").ToString
                Dim oItem As New ClassDetailForm.DetailData With {
                    .Guid = CInt(oRow.Item("GUID")),
                    .ParentId = CInt(oRow.Item("PARENT_ID")),
                    .Entity = oRow.Item("ENTITY_TITLE").ToString,
                    .Scope = oRow.Item("SCOPE").ToString,
                    .SQLCommand = oRow.Item("SQL_COMMAND").ToString.Replace("@LANG_CODE", My.Application.User.Language),
                    .PrimaryKey = Utils.NotNull(oRow.Item("PK_COLUMN"), String.Empty)
                }

                Try
                    oItem.SQLResult = My.DatabaseECM.GetDatatable(oItem.SQLCommand)
                Catch ex As Exception
                    oItem.SQLResult = Nothing
                    Logger.Error(ex)
                End Try

                Dim oKey As String = oItem.Entity & "-" & oItem.Scope
                DetailDataList.Add(oKey, oItem)
            Next

            Return True

        Catch ex As Exception
            Logger.Error(ex)
            Return False

        End Try
    End Function

    Public Function TestPageIsRelation(pPageName As String) As Boolean
        Return DetailSettingsList.Any(Function(page) page.Key = pPageName And page.Value.IsRelationPage = True)
    End Function

    Public Class DetailSettings
        Public Property GridTitle As String
        Public Property [Module] As String
        Public Property Entity As String

        ''' <summary>
        ''' This makes sure that the page can be opened even if there are no entries
        ''' </summary>
        ''' <returns></returns>
        Public Property IsRelationPage As Boolean = False

        ''' <summary>
        ''' If this is empty, the button "New Record will be hidden"
        ''' </summary>
        Public Property NewRecordTitle As String = ""
    End Class

    Public Class DetailData
        Public Property Guid As Integer
        Public Property ParentId As Integer
        Public Property Entity As String
        Public Property Scope As String
        Public Property PrimaryKey As String
        Public Property SQLCommand As String
        Public Property SQLResult As DataTable
    End Class
End Class
