Imports IntraSell_DLL

Public Class ucInterfaceTools

    Public dndArtikel As TouchErfassungDnD
    Public dndKategorie As TouchErfassungDnD
    Public touch As TouchErfassung
    Property ArtKatNr As Integer = Nothing
    Shared farbTool As FarbTool

    Dim SortierungAktiv As Boolean = False

    Private Sub ButtonFaerben_Click(sender As Object, e As EventArgs)
        ColorDialog1.ShowDialog()
    End Sub

    Private Sub ucInterfaceTools_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
        RemoveHandler1()
        If farbTool IsNot Nothing Then
            farbTool.Close()
            farbTool = Nothing
        End If
    End Sub

    Private Sub RemoveHandler1()
        RemoveHandler UcTasten1.EinstellungenGeaendert, AddressOf ReloadTouch
        RemoveHandler touch.FlowLayoutPanelArtikel.MouseMove, AddressOf touch_MouseMove
        RemoveHandler touch.FlowLayoutPanelKategorien.MouseMove, AddressOf touch_MouseMove
        RemoveHandler dndArtikel.ButtonMouseMove, AddressOf touch_MouseMove
        RemoveHandler dndKategorie.ButtonMouseMove, AddressOf touch_MouseMove
    End Sub

    Private Sub ucInterfaceTools_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler UcTasten1.EinstellungenGeaendert, AddressOf ReloadTouch
        AddHandler touch.FlowLayoutPanelArtikel.MouseMove, AddressOf touch_MouseMove
        AddHandler touch.FlowLayoutPanelKategorien.MouseMove, AddressOf touch_MouseMove
        AddHandler dndArtikel.ButtonMouseMove, AddressOf touch_MouseMove
        AddHandler dndKategorie.ButtonMouseMove, AddressOf touch_MouseMove
    End Sub

    ''' <summary>
    ''' Farbe setzen einmal dann muss neu die farbe gewählt werden
    ''' </summary>
    ''' <param name="bnt"></param>
    ''' <remarks></remarks>
    Public Sub ArtikelButtonMouseClicked(ByRef bnt As Button)
        If Parent IsNot Nothing Then
            Dim ColorPaintingMode As Boolean = Not UcColorPalette1.SelectedColor = Nothing
            If ColorPaintingMode Then
                bnt.BackColor = UcColorPalette1.SelectedColor
                dndArtikel.UpdateStyle(btn:=bnt)
            Else 'Öffne Artikel Quick maske
                Dim artNr As Integer = bnt.Tag
                If IsNumeric(artNr) Then
                    FormManager.OffneArtikelMaskeQuick(artNr)
                End If
            End If
        End If
    End Sub

    Sub KategorieButtonMouseClicked(ByRef bnt As Button)
        ArtKatNr = bnt.Tag
        If Parent IsNot Nothing Then
            Dim ColorPaintingMode As Boolean = Not UcColorPalette1.SelectedColor = Nothing
            If ColorPaintingMode Then
                bnt.BackColor = UcColorPalette1.SelectedColor
                dndKategorie.UpdateStyle(btn:=bnt)
                'UcColorPalette1.SelectedColor = Nothing
            End If
        End If
    End Sub

    Private Sub btnSpeichern_Click(sender As Object, e As EventArgs) Handles btnSpeichern.Click
        If dndArtikel IsNot Nothing Then
            dndArtikel.StartDnD(False)
            dndArtikel.FinishDnD(sender, e)
        End If

        If dndKategorie IsNot Nothing Then
            dndKategorie.StartDnD(False)
            dndKategorie.FinishDnD(sender, e)
        End If

        If touch IsNot Nothing Then
            InvalidateCache()
        End If

        ParentForm.Close()
    End Sub

    Private Sub ButtonCancel_Click(sender As Object, e As EventArgs) Handles ButtonCancel.Click
        If dndArtikel IsNot Nothing Then
            dndArtikel.StartDnD(False)
        End If
        If dndKategorie IsNot Nothing Then
            dndArtikel.StartDnD(False)
        End If
        ParentForm.Close()
    End Sub

    Private Sub ButtonKategorieAnlegen_Click(sender As Object, e As EventArgs) Handles ButtonKategorieAnlegen.Click
        Dim name = InputBox("Kategoriename:")
        If name <> "" Then
            IntraSell_Net_Base.GetArtKatNr(name)
        End If
        If touch IsNot Nothing Then
            InvalidateCache()
            touch.LadeKategorien(True)
        End If
    End Sub

    Private Sub ButtonArtikelAnlegen_Click(sender As Object, e As EventArgs) Handles ButtonArtikelAnlegen.Click
        If ArtKatNr = Nothing Then
            MsgBox("Wählen Sie eine Kategorie zuerst!")
            Exit Sub
        End If
        'öffne maske vorbelegt 
        Dim q As New ArtikelQuick3
        q.Show()
        q.BeginNew(ArtKatNr)
        AddHandler q.FormClosed, AddressOf ArtikelAngelegt

    End Sub

    Private Sub ArtikelAngelegt(sender As Object, e As FormClosedEventArgs)
        If touch IsNot Nothing Then
            InvalidateCache()
            touch.LadeArtikelFuerKategorie(ArtKatNr)
        End If
    End Sub

    Private Sub ReloadTouch()
        If touch IsNot Nothing Then
            touch.Laden()
        End If
    End Sub

    Private Sub touch_MouseMove(sender As Object, e As MouseEventArgs)
        Dim ColorPaintingMode As Boolean = Not UcColorPalette1.SelectedColor = Nothing
        If ColorPaintingMode Then
            If farbTool Is Nothing Then
                farbTool = New FarbTool With {
                    .Visible = True
                }
                farbTool.Show()
            End If
            farbTool.BackColor = UcColorPalette1.SelectedColor
            farbTool.Width = 32
            farbTool.Height = 32
            Dim ff = sender.PointToScreen(e.Location)
            farbTool.Top = ff.Y + 5
            farbTool.Left = ff.X + 5
        End If
    End Sub

    Private Sub ButtonSortieren_Click(sender As Object, e As EventArgs) Handles ButtonSortieren.Click
        Try
            SortierungAktiv = Not SortierungAktiv
            If dndArtikel IsNot Nothing Then
                dndArtikel.StartDnD(SortierungAktiv)
            End If

            If dndKategorie IsNot Nothing Then
                dndKategorie.StartDnD(SortierungAktiv)
            End If

            If SortierungAktiv Then
                ButtonSortieren.Text = "Sortierung beenden"
            Else
                ButtonSortieren.Text = "Sortierung aktivieren"
            End If
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub
End Class
