Imports IntraSell_DLL

Public Class Tische

    Event Order_ClickEvent(sender As Object, e As EventArgs)
    Event RaumbezeichnungGeaendert()

    Dim dnd As TischeDnD
    Dim LAYOUT_FILENAME = ClassSettings.GetDataPath & "layout.xml"
    Dim _layoutModus = False
    Dim layoutDefintion As dsLayout.LayoutDataTable = Nothing
    Dim draggable As New Draggable
    Dim _RaumCode As String = ""

    Property RaumCode As String
        Get
            Return _RaumCode
        End Get
        Set(value As String)
            _RaumCode = value
            LAYOUT_FILENAME = ClassSettings.GetDataPath & "layout" & value & ".xml"
        End Set
    End Property

    Property RaumBezeichnung As String = ""
    Property SettingsSichtbar As Boolean = False

    Property LayoutModus As Boolean
        Get
            Return _layoutModus
        End Get
        Set(value As Boolean)
            _layoutModus = value
            If value Then
                ButtonLayout.Text = "Layout speichern"
            Else
                ButtonLayout.Text = "Tisch Layout"
            End If

        End Set
    End Property

    Private Sub pLayout_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            SetupDnD()
            txtWidth.Text = VarValue_Default(Variablen.TISCH_BREITE_PX, 100)
            txtHeight.Text = VarValue_Default(Variablen.TISCH_HÖHE_PX, 100)
        Catch ex As Exception
            'Ignore
        End Try
    End Sub

    Protected Friend Sub DatenAktualisieren()
        Try
            If LayoutModus Then
                Return
            End If
            DataLoaderOrders.DatenAktualisierenOrders(pLayout, DataLoaderOrders.SQL_OPEN_ORDERS_TISCHE(_RaumCode))
            LayoutTische()
            Me.LabelRaumBezeichnung.Text = RaumBezeichnung
            FlowLayoutPanelTischeAnlegen.Visible = SettingsSichtbar AndAlso HasRecht(Rechte.Administrator)
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub


#Region "Layout"

    Private Sub SetupDnD()
        If dnd Is Nothing Then
            'If Me.pLayout Is Nothing Then
            'dnd = New TischeDnD(Me.FlowLayoutPanelOrders, "Tische")
            'Else
            dnd = New TischeDnD(pLayout, "Tische")
            'End If

            AddHandler dnd.DnDAktiviert, AddressOf DnDAktiviert
            AddHandler dnd.DnDBeendet, AddressOf DnDBeendet

            AddHandler draggable.ControlDropped, AddressOf TischDropped
        End If
    End Sub



    Private Sub DnDAktiviert()
        If Not LayoutModus Then
            ButtonLayout_Click(Nothing, Nothing)
        End If
    End Sub

    Private Sub DnDBeendet()
        If LayoutModus Then
            ButtonLayout_Click(Nothing, Nothing)
        End If
    End Sub


    Private Sub LoadLayoutDefinition()
        If IO.File.Exists(LAYOUT_FILENAME) Then
            Dim data As New dsLayout
            data.ReadXml(LAYOUT_FILENAME)
            layoutDefintion = data.Tables("Layout")
        End If
    End Sub


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ButtonLayout_Click(sender As Object, e As EventArgs) Handles ButtonLayout.Click
        Try
            If LayoutModus Then
                LayoutModus = Not LayoutModus
                SaveLayout()
                LoadLayoutDefinition()
                'TouchOrders_Load(Nothing, Nothing)
                pLayout.Controls.Clear()
                DatenAktualisieren()
            Else
                LayoutModus = Not LayoutModus
                dnd.DnDAktiv = True
                pLayout.BackColor = Color.Yellow
                pLayout.Enabled = True
                For ii = 0 To pLayout.Controls.Count - 1
                    Dim c1 As Control = pLayout.Controls(ii)
                    draggable.MakeDraggable(c1, CType(Parent.Parent.Parent.Parent.Parent.Parent.Parent, Form))
                Next
            End If
            'Buttons aktivieren 
            LabelDnDDelete.Enabled = LayoutModus
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Sub SaveLayout()
        For i = 0 To pLayout.Controls.Count - 1
            Dim c As Control = pLayout.Controls(i)
            If TypeOf c.Tag Is OffeneBestellung Then
                Dim ofBest As OffeneBestellung = CType(c.Tag, OffeneBestellung)

                StoreLayout(ofBest.IDNR, c.Location.X, c.Location.Y)
            End If
        Next
        pLayout.BackColor = pLayout.Parent.BackColor
    End Sub

    Sub SaveLayout_Removeme()
        Dim data As New dsLayout
        Dim layout As dsLayout.LayoutDataTable = data.Tables("Layout")
        For i = 0 To pLayout.Controls.Count - 1
            Dim c As Control = pLayout.Controls(i)
            If TypeOf c.Tag Is OffeneBestellung Then
                Dim ofBest As OffeneBestellung = CType(c.Tag, OffeneBestellung)
                Dim r = layout.NewLayoutRow()
                r.Key = ofBest.Name
                r.X = c.Location.X
                r.Y = c.Location.Y
                Try
                    layout.Rows.Add(r)
                Catch ex As Exception 'Primary key 
                    'Ignore
                End Try
            End If
        Next
        pLayout.BackColor = pLayout.Parent.BackColor
        data.WriteXml(LAYOUT_FILENAME)
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="ForceLayout"></param>
    ''' <remarks></remarks>
    Sub AutoLayout(ForceLayout As Boolean)
        'Initial Layout when all controls are still in the flow layout 
        Dim row As Integer = 0
        Dim col As Integer = 0
        For Each c In pLayout.Controls
            If TypeOf c.Tag Is OffeneBestellung Then
                Dim TISCH_HEIGHT As Integer = VarValue_Default(Variablen.TISCH_HÖHE_PX, 80)
                Dim TISCH_WIDTH As Integer = VarValue_Default(Variablen.TISCH_BREITE_PX, 140)

                Dim x = 10 + col * (TISCH_WIDTH + 10)
                Dim y = 10 + row * (TISCH_HEIGHT + 10)
                If x > pLayout.Width - col * TISCH_WIDTH Then
                    row += 1
                    col = 0
                End If

                If ForceLayout Or (c.Location.X < 5 And c.Location.Y < 5) Then
                    c.Location = New System.Drawing.Point(x, y)
                End If
            End If
            col += 1
        Next
    End Sub

    Private Sub ButtonLayoutLoeschen_Click(sender As Object, e As EventArgs)
        Try
            If IO.File.Exists(LAYOUT_FILENAME) Then
                IO.File.Delete(LAYOUT_FILENAME)
            End If

            LayoutModus = False
            DatenAktualisieren()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ButtonTischAnlegen_Click(sender As Object, e As EventArgs) Handles ButtonTischAnlegen.Click
        Try
            Tisch.TextBoxRaum.Text = _RaumCode
            Tisch.ShowDialog()
            DatenAktualisieren()
            SaveLayout()
        Catch ex As Exception
            'Ignore
        End Try
    End Sub

    Sub StoreLayout(IDNR, X, Y)
        Dim sql = $"update ofAdressen set LayoutX={X}, LayoutY={Y} where Idnr={IDNR}"
        RunSQL(sql)
    End Sub

    Private Sub LayoutTische()
        Try
            If IO.File.Exists(LAYOUT_FILENAME) Then
                If layoutDefintion Is Nothing Then
                    LoadLayoutDefinition()
                End If
                pLayout.Location = New System.Drawing.Point(0, 0)

                For ii = 0 To pLayout.Controls.Count - 1
                    Dim c1 As Control = pLayout.Controls(ii)
                    If c1.Tag IsNot Nothing Then
                        Dim ofBest As OffeneBestellung = CType(c1.Tag, OffeneBestellung)
                        Dim found As dsLayout.LayoutRow = layoutDefintion.Rows.Find(ofBest.Name)
                        If found IsNot Nothing Then
                            c1.Location = New Point(found.X, found.Y)
                            StoreLayout(ofBest.IDNR, found.X, found.Y)
                        End If
                    End If
                Next

                IO.File.Move(LAYOUT_FILENAME, LAYOUT_FILENAME & "_deleteme")
            End If

        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

#End Region

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub pLayout_ControlAdded(sender As Object, e As ControlEventArgs) Handles pLayout.ControlAdded
        Try
            Dim btn = e.Control
            If TypeOf btn Is Button Then
                AddHandler btn.Click, AddressOf Order_Click
                If dnd Is Nothing Then
                    SetupDnD()
                End If
                dnd.AktiviereDnD(btn)
            End If
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Order_Click(sender As Object, e As EventArgs)
        RaiseEvent Order_ClickEvent(sender, e)
    End Sub

    Private Sub ButtonRaumAendern_Click(sender As Object, e As EventArgs) Handles ButtonRaumAendern.Click
        Try
            RaumBezeichnung = InputBox("Neue Raumbezeichung", "Raum", RaumBezeichnung)
            DatenAktualisieren()
            SetVarValue("RAUMBEZEICHNUNG_" & RaumCode, RaumBezeichnung)
            RaiseEvent RaumbezeichnungGeaendert()
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Private Sub ButtonSettings_Click(sender As Object, e As EventArgs) Handles ButtonSettings.Click
        SettingsSichtbar = Not SettingsSichtbar
        DatenAktualisieren()
    End Sub

    Private Sub btnSelectTisch_Click(sender As Object, e As EventArgs) Handles btnSelectTisch.Click
        Dim f = New TischWaehlen
        f.Show()
        AddHandler f.TischGewaehlt, AddressOf TischGewaehlt
    End Sub

    Private Sub TischGewaehlt(ByRef TischIdnr As Object)
        Dim bestellung = New OffeneBestellung(0, "AU", TischIdnr, "")
        Dim b As Button = New Button
        b.Tag = bestellung
        RaiseEvent Order_ClickEvent(b, Nothing)
    End Sub

    Private Sub TischDropped(tisch As Control)

        Try
            If TypeOf tisch Is Button Then

                Dim labelDeleteTopLeft = LabelDnDDelete.PointToScreen(New Point(0, 0))
                Dim dropOverDeleteLabel = Cursor.Position.Y > labelDeleteTopLeft.Y AndAlso
                    Cursor.Position.Y < labelDeleteTopLeft.Y + LabelDnDDelete.Height AndAlso
                    Cursor.Position.X > labelDeleteTopLeft.X AndAlso
                    Cursor.Position.X < labelDeleteTopLeft.X + LabelDnDDelete.Width
                If dropOverDeleteLabel AndAlso TischLoeschen(tisch.Tag) Then
                    pLayout.Controls.Remove(tisch)
                End If
            End If
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Private Sub LabelDnDDelete_MouseEnter(sender As Object, e As EventArgs) Handles LabelDnDDelete.MouseEnter
        'Ignore
    End Sub

    Private Function TischLoeschen(offeneBestellung As OffeneBestellung)
        Try
            If MsgBox("Tisch wirklich löschen?") = MsgBoxResult.Ok Then
                ModuleKunden.KundenLoeschen(offeneBestellung.IDNR)
                Return True
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
        Return False
    End Function

    Private Sub ButtonKüchenDisplay_Click(sender As Object, e As EventArgs) Handles ButtonKüchenDisplay.Click
        Try
            OeffneKuechenDisplay()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub txtWidth_Validated(sender As Object, e As EventArgs) Handles txtWidth.Validated
        SetVarValue(Variablen.TISCH_BREITE_PX, txtWidth.Text)
    End Sub

    Private Sub txtHeight_Validated(sender As Object, e As EventArgs) Handles txtHeight.Validated
        SetVarValue(Variablen.TISCH_HÖHE_PX, txtHeight.Text)
    End Sub
End Class
