Imports IntraSell_DLL

Public Class ucArtikelOptionen

    Dim letzteOption As CheckBox

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="modifikationen"></param>
    Public Sub RenderOptionen(modifikationen As String)
        Try
            Dim list = ParseOptionen(modifikationen)
            FlowLayoutPanel1.Controls.Clear()
            For Each opt In list
                Dim wertListe = opt.Werte.Split(",")

                If wertListe.Length <= 2 Then

                    Dim cb As New CheckBox With {
                    .Text = opt.Name,
                    .Width = 280,
                    .Height = 40,
                    .Tag = opt,
                    .ThreeState = True
                }
                    If opt.Preis < 0 Then
                        cb.ForeColor = Color.Red
                    End If

                    FlowLayoutPanel1.Controls.Add(cb)
                    AddHandler cb.Click, AddressOf BerechnePreis
                Else
                    ' Add as Radio button

                    Dim radioGroup As New GroupBox With {
                    .Text = opt.Name,
                    .Width = 800,
                    .Height = 60
                    }
                    Dim flow As New FlowLayoutPanel
                    flow.Dock = DockStyle.Fill

                    For Each wert In wertListe
                        Dim optForRadio As New ArtikelOption(opt.Name, opt.Preis, wert)

                        Dim radio As New RadioButton With {
                        .Text = wert,
                        .Tag = optForRadio
                        }
                        radio.Width = 200
                        flow.Controls.Add(radio)
                        AddHandler radio.Click, AddressOf BerechnePreisRadio
                    Next
                    radioGroup.Controls.Add(flow)
                    FlowLayoutPanel1.Controls.Add(radioGroup)

                End If
            Next
        Catch ex As Exception
            HandleAppError(ex, "Render Optionen", Silent:=True)
        End Try

    End Sub

    Private Sub BerechnePreis(sender As Object, e As EventArgs)
        Dim cb As CheckBox = sender
        cb.ForeColor = Color.Black
        If cb.CheckState = CheckState.Checked Then
            cb.ForeColor = Color.Green
        End If
        If cb.CheckState = CheckState.Unchecked Then
            cb.ForeColor = Color.LightGray
        End If

        If cb.CheckState = CheckState.Indeterminate Then
            cb.ForeColor = Color.Red
        End If
        BerechneSumme()
        letzteOption = cb
    End Sub

    Private Sub BerechnePreisRadio(sender As Object, e As EventArgs)
        Dim cb As RadioButton = sender
        Dim groupBox As GroupBox = cb.Parent.Parent
        groupBox.Tag = cb.Tag
        BerechneSumme()
    End Sub

    Sub BerechneSumme()
        Dim summe = (From o In GetSelectedOptionen() Select o.Menge * o.Preis).Sum()
        Me.LabelSumme.Text = "Summe: " & Format(summe, "c2")
        If summe < 0 Then
            Me.LabelSumme.ForeColor = Color.Red
        Else
            Me.LabelSumme.ForeColor = Color.Green
        End If
    End Sub


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    Public Function GetSelectedOptionen() As List(Of ArtikelOption)
        Dim selected = New List(Of ArtikelOption)
        Dim unselected = New List(Of ArtikelOption)
        For Each c In FlowLayoutPanel1.Controls

            If TypeOf c Is CheckBox Then
                Dim cb = CType(c, CheckBox)
                Dim opt As ArtikelOption = cb.Tag
                Dim optWertChecked = opt.Werte
                Dim optWertUnChecked = opt.Werte
                Try
                    optWertChecked = opt.Werte.Split(",")(0)
                    optWertUnChecked = opt.Werte.Split(",")(1)
                Catch ex As Exception

                End Try
                If cb.CheckState = CheckState.Checked Then
                    Dim checked As New ArtikelOption(opt.Name, opt.Preis, optWertChecked, opt.Menge)
                    selected.Add(checked)
                End If

                If cb.CheckState = CheckState.Indeterminate Then
                    Dim unchecked As New ArtikelOption(opt.Name.Replace("mit ", "ohne "), 0, optWertUnChecked)
                    selected.Add(unchecked)
                End If
            End If

            If TypeOf c Is GroupBox Then
                Dim gb = CType(c, GroupBox)
                If Not gb.Tag Is Nothing Then
                    selected.Add(gb.Tag)
                End If
            End If

        Next
        Return selected
    End Function

    Private Sub btn2x_Click(sender As Object, e As EventArgs) Handles btn2x.Click
        If letzteOption IsNot Nothing Then
            Dim opt As ArtikelOption = letzteOption.Tag
            opt.Menge = opt.Menge + 1
        End If
        BerechneSumme()
    End Sub
End Class
