Imports DevExpress.XtraEditors
Imports DevExpress.XtraToolbox
Imports DevExpress.XtraLayout
Imports DevExpress.XtraLayout.Customization
Imports DevExpress.XtraLayout.Dragging
Imports DevExpress.XtraLayout.HitInfo
Imports DigitalData.Controls.LookupGrid
Imports DigitalData.GUIs.ClientSuite.Controls
Imports DigitalData.GUIs.ClientSuite.Controls.Properties
Imports DigitalData.GUIs.ClientSuite.Controls.Editors
Imports DigitalData.Modules.Language

Public Class frmFormDesigner
    Private _FormId As Int64
    Private _ControlLoader As ControlLoader

#Region "Drag Helper"
    Private _DragItem As BaseLayoutItem
    Private _Window As DragFrameWindow
    Private _DragController As LayoutItemDragController = Nothing

    Protected ReadOnly Property DragFrameWindow As DragFrameWindow
        Get
            If _Window Is Nothing Then
                _Window = New DragFrameWindow(LayoutControlMain)
            End If
            Return _Window
        End Get
    End Property

    Private Sub ShowDragHelper()
        DragFrameWindow.Visible = True
    End Sub

    Private Sub HideDragHelper()
        DragFrameWindow.Reset()
        DragFrameWindow.Visible = False
    End Sub

    Private Sub UpdateDragHelper(p As Point)
        p = LayoutControlMain.PointToClient(p)
        _DragController = New LayoutItemDragController(_DragItem, LayoutControlMain.Root, New Point(p.X, p.Y))
        DragFrameWindow.DragController = _DragController
    End Sub
#End Region
#Region "Drag Drop Actions"
    Private Sub ToolboxControlMain_DragItemDrop(sender As Object, e As ToolboxDragItemDropEventArgs) Handles ToolboxControlMain.DragItemDrop
        Dim oPosition As Point = LayoutControlMain.PointToClient(MousePosition)
        Dim oHitInfo As BaseLayoutItemHitInfo = LayoutControlMain.CalcHitInfo(oPosition)
        Dim oLayoutControl As LayoutControlItem = DirectCast(_DragItem, LayoutControlItem)
        Dim oControlName As String = oLayoutControl.Tag & Utils.ShortGUID()
        Dim oControl As Control = _ControlLoader.CreateLayoutControl(oLayoutControl.Tag, oControlName, 0)

        If oLayoutControl IsNot Nothing Then
            HideDragHelper()
            oLayoutControl.Control = oControl

            If (_DragController IsNot Nothing AndAlso _DragItem IsNot Nothing) Then
                If (_DragItem.Owner Is Nothing OrElse _DragItem.Parent Is Nothing) Then
                    _DragController.DragWildItem()
                Else
                    _DragController.Drag()
                End If
                Focus()
            End If
            HideDragHelper()
            _DragItem = Nothing
        End If
    End Sub

    Private Sub ToolboxControlMain_DragItemStart(sender As Object, e As ToolboxDragItemStartEventArgs) Handles ToolboxControlMain.DragItemStart
        _DragItem = CreateLayoutControlItem(e.Item.Tag)
    End Sub

    Private Sub ToolboxControlMain_DragItemMove(sender As Object, e As DevExpress.XtraToolbox.ToolboxDragItemMoveEventArgs) Handles ToolboxControlMain.DragItemMove
        If (CType(LayoutControlMain, ILayoutControl)).EnableCustomizationMode OrElse Me.DesignMode Then Return
        Dim oFormPosition As Point = PointToClient(e.Location)
        Dim oLayoutPosition As Point = LayoutControlMain.PointToClient(e.Location)

        If LayoutControlMain.Bounds.Contains(oFormPosition) Then

            If Not DragFrameWindow.Visible Then
                DragFrameWindow.Visible = True
                Return
            End If

            UpdateDragHelper(e.Location)
        Else
            DragFrameWindow.Visible = False
        End If
    End Sub
#End Region

    Public Sub New()
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()
        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        _FormId = 104
    End Sub

    Public Sub New(FormId As Int64)
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()
        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        _FormId = FormId
    End Sub

    Private Async Sub frmFormDesigner_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FormDesignerRibbon.SelectPage(RibbonPageFormDesigner)

        _ControlLoader = New ControlLoader(My.LogConfig, LayoutControlGroupMain)

        Dim oTable = Await My.Common.Views.VWIDB_FORM_CONTROL(_FormId)

        _ControlLoader.LoadControls(oTable)
    End Sub

    Private Function CreateLayoutControlItem(Id As String) As LayoutControlItem
        Return New LayoutControlItem() With {.Tag = Id}
    End Function

    Private Sub LayoutControlMain_ItemSelectionChanged(sender As Object, e As EventArgs) Handles LayoutControlMain.ItemSelectionChanged
        ' only layout control items have properties
        If TypeOf sender IsNot LayoutControlItem Then
            Exit Sub
        End If

        Dim oLayoutItem As LayoutControlItem = DirectCast(sender, LayoutControlItem)
        Dim oSelectedControl As BaseEdit = oLayoutItem.Control
        Dim oMetadata As New Metadata

        ' Don't load properties for layout items like splitters, separators, etc.
        If oLayoutItem.Control Is Nothing Then
            Exit Sub
        End If


        oMetadata = oSelectedControl.Tag

        Select Case oSelectedControl.GetType
            Case GetType(MemoEdit)
                PropertyGridControlMain.SelectedObject = New MemoeditProperties() With {
                    .Id = oMetadata.Id,
                    .Name = oMetadata.Caption
                }
            Case GetType(TextEdit)
                PropertyGridControlMain.SelectedObject = New TextboxProperties() With {
                    .Id = oMetadata.Id,
                    .Name = oMetadata.Caption
                }
            Case GetType(LookupControl2)
                PropertyGridControlMain.SelectedObject = New ComboboxProperties() With {
                    .Datasource = New DatasourceType,
                    .Id = oMetadata.Id,
                    .Name = oMetadata.Caption
                }
            Case GetType(DateEdit)
                PropertyGridControlMain.SelectedObject = New DatepickerProperties() With {
                    .Id = oMetadata.Id,
                    .Name = oMetadata.Caption
                }
            Case GetType(CheckEdit)
                PropertyGridControlMain.SelectedObject = New CheckboxProperties() With {
                    .Id = oMetadata.Id,
                    .Name = oMetadata.Caption
                }
        End Select
    End Sub

    Private Sub BarCheckEditLayout_CheckedChanged(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarCheckEditLayout.CheckedChanged
        If BarCheckEditLayout.Checked Then
            LayoutControlMain.ShowCustomizationForm()
            XtraTabPageProperties.PageEnabled = True
            XtraTabPageControls.PageEnabled = False
            XtraTabControlMain.SelectedTabPage = XtraTabPageProperties
        End If
    End Sub

    Private Sub BarCheckEditControls_CheckedChanged(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarCheckEditControls.CheckedChanged
        If BarCheckEditControls.Checked Then
            LayoutControlMain.HideCustomizationForm()
            XtraTabPageProperties.PageEnabled = False
            XtraTabPageControls.PageEnabled = True
            XtraTabControlMain.SelectedTabPage = XtraTabPageControls
        End If
    End Sub

    Private Sub LayoutControlMain_HideCustomization(sender As Object, e As EventArgs) Handles LayoutControlMain.HideCustomization
        BarCheckEditControls.Checked = True
    End Sub

    Private Sub LayoutControlMain_ShowCustomization(sender As Object, e As EventArgs) Handles LayoutControlMain.ShowCustomization
        BarCheckEditLayout.Checked = True
    End Sub


End Class