Imports IntraSell_DLL
Imports MySql.Data.MySqlClient

Public Class TouchErfassungDnD
    Public Shared DnDDeaktiviert As Boolean = False

    Dim Typ As String
    Dim FlowLayoutPanelArtikel As FlowLayoutPanel
    WithEvents TimerDnD As Timer

    Dim DnDSource As Button = Nothing
    Dim DnDAktiv As Boolean = False
    Dim MouseIsDown As Boolean = False

    Public Event ArtikelButtonMouseClicked(ByRef bnt As Button)
    Public Event ButtonMouseMove(sender As Object, e As MouseEventArgs)
    Sub New(flowLayoutPanel As FlowLayoutPanel, _Typ As String)
        ' TODO: Complete member initialization 
        FlowLayoutPanelArtikel = flowLayoutPanel
        Typ = _Typ
        TimerDnD = New Timer With {
            .Interval = 2000
        }
        AddHandler TimerDnD.Tick, AddressOf TimerDnD_Tick
    End Sub

    ''' <summary>
    ''' when user presses for 2 seconds then we start DnD mode 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TimerDnD_Tick(sender As Object, e As EventArgs) ' Handles TimerDnD.Tick
        If DnDDeaktiviert Then
            Exit Sub
        End If
        If Err.Number > 0 Then
            Exit Sub
        End If
        'If mouseUpTime > mouseDownTime And mouseUpTime < mouseDownTime.AddSeconds(2) Then 'Schutz für Exceptions thrown 
        If MouseIsDown Then
            StartDnD(True)
            DnDSource.DoDragDrop(DnDSource.Text, DragDropEffects.Move)
        Else
            StartDnD(False)
        End If
        'End If
    End Sub

    Dim FlowLayoutPanelArtikelBackColor As Color
    Public Sub StartDnD(start As Boolean)
        If DnDDeaktiviert Then
            Exit Sub
        End If
        DnDAktiv = start
        If start Then
            FlowLayoutPanelArtikelBackColor = FlowLayoutPanelArtikel.BackColor
            FlowLayoutPanelArtikel.BackColor = Color.Yellow
            FlowLayoutPanelArtikel.Cursor = Cursors.Hand
            AddHandler FlowLayoutPanelArtikel.Click, AddressOf FinishDnD
        Else
            FlowLayoutPanelArtikel.BackColor = FlowLayoutPanelArtikelBackColor
            FlowLayoutPanelArtikel.Cursor = Cursors.Default
            'RemoveHandler FlowLayoutPanelArtikel.Click , 
        End If
        TimerDnD.Enabled = False
        TimerDnD.Stop()
        FlowLayoutPanelArtikel.Refresh()
    End Sub

    Public Sub FinishDnD(sender As Object, e As MouseEventArgs)
        Dim cmd As MySqlCommand
        StartDnD(False)
        'update pos 
        Dim i = 1
        For Each btn As Control In FlowLayoutPanelArtikel.Controls
            If IsNumeric(btn.Tag) Then
                Dim artNr As Integer = btn.Tag
                If Typ = "Kategorien" Then
                    cmd = New MySqlCommand("update `grArtikel-Kategorien` set `Order`=@Order where ArtKatNr=@ArtKatNr")
                    cmd.Parameters.AddWithValue("Order", i)
                    cmd.Parameters.AddWithValue("ArtKatNr", artNr)
                    RunSQL(cmd)
                Else
                    'Update Position
                    cmd = New MySqlCommand("update grArtikel set ArtKatPos=@ArtKatPos where ArtNr=@ArtNr")
                    cmd.Parameters.AddWithValue("ArtKatPos", i)
                    cmd.Parameters.AddWithValue("ArtNr", artNr)
                    RunSQL(cmd)
                End If
                i += 1
            End If
        Next
        InvalidateCache()
    End Sub

    Public Sub UpdateStyle(btn As Button)
        'Update color 
        Dim artNr As Integer = btn.Tag
        ModuleArtikel.UpdateStyle(Typ, artNr, btn.BackColor)
    End Sub

    Public Sub AktiviereDnD(btn As Button)
        btn.AllowDrop = True
        AddHandler btn.MouseDown, AddressOf Button_MouseDown
        AddHandler btn.MouseUp, AddressOf Button_MouseUp
        AddHandler btn.DragEnter, AddressOf Button_DragEnter
        AddHandler btn.DragDrop, AddressOf Button_DragDrop
        AddHandler btn.MouseClick, AddressOf Button_MouseClick
        AddHandler btn.MouseEnter, AddressOf Button_MouseEnter
        AddHandler btn.MouseMove, AddressOf Button_MouseMove
    End Sub

    Private Sub Button_MouseDown(sender As Object, e As MouseEventArgs)
        MouseIsDown = True
        DnDSource = sender
        If DnDAktiv Then
            DnDSource.DoDragDrop(DnDSource.Text, DragDropEffects.Move)
        Else
            TimerDnD.Enabled = True
            TimerDnD.Start()
        End If
    End Sub

    Private Sub Button_MouseUp(sender As Object, e As MouseEventArgs)
        MouseIsDown = False
        DnDSource = Nothing
        TimerDnD.Enabled = False
        TimerDnD.Stop()
    End Sub

    Private Sub Button_DragEnter(sender As Object, e As DragEventArgs)
        If Not DnDAktiv Then Exit Sub
        ' Check the format of the data being dropped.
        Dim anotherButtonTag = e.Data.GetData(DataFormats.Text)
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            ' Display the copy cursor.
            e.Effect = DragDropEffects.Move
        Else
            ' Display the no-drop cursor.
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Friend Sub Button_DragDrop(sender As Object, e As DragEventArgs)
        If Not DnDAktiv Then Exit Sub

        Dim anotherButtonTag = e.Data.GetData(DataFormats.Text)
        Dim source As Control = FindByText(anotherButtonTag)
        If sender.Equals(source) Then
            Exit Sub
        End If
        Dim newPos = FlowLayoutPanelArtikel.Controls.IndexOf(sender)
        FlowLayoutPanelArtikel.Controls.SetChildIndex(source, newPos)

    End Sub

    Function FindByText(tag) As Control
        For Each c As Control In FlowLayoutPanelArtikel.Controls
            If c.Text = tag Then
                Return c
            End If
        Next
        Return Nothing
    End Function

    Private Sub Button_MouseClick(sender As Object, e As MouseEventArgs)
        RaiseEvent ArtikelButtonMouseClicked(sender)
    End Sub

    Private Sub Button_MouseEnter(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button_MouseMove(sender As Object, e As MouseEventArgs)
        RaiseEvent ButtonMouseMove(sender, e)
    End Sub

End Class
