Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base

Public Class RecipeVC
	Inherits DevExpress.ExpressApp.ViewController
    Protected _selection As New ArrayList
    Protected _noselect As Boolean = False
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of RecipeVC).aRecipe_ChangeRowGroups.Active.SetItemValue("", False)
        Frame.GetController(Of RecipeVC).aInsertServicesToRecipe.Active.SetItemValue("", False)

        If View.Id = "Recipe_RecipeTo_ListView" Then
            Frame.GetController(Of RecipeVC).aRecipe_ChangeRowGroups.Active.SetItemValue("", True)
            AddHandler View.SelectionChanged, AddressOf RecipeLines_SelectionChange
        End If
        If View.Id = "Recipe_RecipeToOther_ListView" Then
            Frame.GetController(Of RecipeVC).aInsertServicesToRecipe.Active.SetItemValue("", True)
            Frame.GetController(Of RecipeVC).aRecipe_ChangeRowGroups.Active.SetItemValue("", True)
            AddHandler View.SelectionChanged, AddressOf RecipeLines_SelectionChange
        End If
        If View.Id = "Recipe_RecipeFrom_ListView" Then
            Frame.GetController(Of RecipeVC).aRecipe_ChangeRowGroups.Active.SetItemValue("", True)
            AddHandler View.SelectionChanged, AddressOf RecipeLines_SelectionChange
        End If
        If View.Id = "Recipe_RecipeFromOther_ListView" Then
            Frame.GetController(Of RecipeVC).aRecipe_ChangeRowGroups.Active.SetItemValue("", True)
            AddHandler View.SelectionChanged, AddressOf RecipeLines_SelectionChange
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "Recipe_RecipeTo_ListView" Then
            Frame.GetController(Of RecipeVC).aRecipe_ChangeRowGroups.Active.SetItemValue("", False)
            RemoveHandler View.SelectionChanged, AddressOf RecipeLines_SelectionChange
        End If
        If View.Id = "Recipe_RecipeToOther_ListView" Then
            Frame.GetController(Of RecipeVC).aRecipe_ChangeRowGroups.Active.SetItemValue("", False)
            RemoveHandler View.SelectionChanged, AddressOf RecipeLines_SelectionChange
        End If
        If View.Id = "Recipe_RecipeFrom_ListView" Then
            Frame.GetController(Of RecipeVC).aRecipe_ChangeRowGroups.Active.SetItemValue("", False)
            RemoveHandler View.SelectionChanged, AddressOf RecipeLines_SelectionChange
        End If
        If View.Id = "Recipe_RecipeFromOther_ListView" Then
            Frame.GetController(Of RecipeVC).aRecipe_ChangeRowGroups.Active.SetItemValue("", False)
            RemoveHandler View.SelectionChanged, AddressOf RecipeLines_SelectionChange
        End If
    End Sub
    Private Sub RecipeLines_SelectionChange(ByVal sender As Object, ByVal e As System.EventArgs)
        If View Is Nothing Then Exit Sub
        If View.SelectedObjects.Count > 0 Then
            If _noselect = False Then
                _selection.Clear()
                For Each _SelectedLines In View.SelectedObjects
                    _selection.Add(_SelectedLines)
                Next
            End If
        End If
    End Sub

    Private Sub aRecipe_ChangeRowGroups_Cancel(sender As Object, e As System.EventArgs) Handles aRecipe_ChangeRowGroups.Cancel
        _noselect = False
    End Sub
    Private Sub aRecipe_ChangeRowGroups_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aRecipe_ChangeRowGroups.CustomizePopupWindowParams
        _noselect = True
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _RecipePopup As RecipePopup = _onew.CreateObject(Of RecipePopup)()
        If View.SelectedObjects.Count > 0 Then
            e.View = Application.CreateDetailView(_onew, "RecipePopup_DetailView", False, _RecipePopup)
        Else
            Throw New UserFriendlyException("Nincs kijelölt elem!")
        End If
    End Sub
    Private Sub aRecipe_ChangeRowGroups_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aRecipe_ChangeRowGroups.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _RecipePopup As RecipePopup = TryCast(e.PopupWindow.View.SelectedObjects(0), RecipePopup)
        If View.Id = "Recipe_RecipeFrom_ListView" Then
            Dim _RecipeFrom As RecipeFrom
            For Each _RecipeFrom In _selection
                If _RecipePopup.Change_RowGroup1 Then
                    _RecipeFrom.RowGroup1 = _RecipePopup.RowGroup1
                End If
                If _RecipePopup.Change_RowGroup2 Then
                    _RecipeFrom.RowGroup2 = _RecipePopup.RowGroup2
                End If
                _RecipeFrom.Save()
            Next
        End If
        If View.Id = "Recipe_RecipeFromOther_ListView" Then
            Dim _RecipeFromOther As RecipeFromOther
            For Each _RecipeFromOther In _selection
                If _RecipePopup.Change_RowGroup1 Then
                    _RecipeFromOther.RowGroup1 = _RecipePopup.RowGroup1
                End If
                If _RecipePopup.Change_RowGroup2 Then
                    _RecipeFromOther.RowGroup2 = _RecipePopup.RowGroup2
                End If
                _RecipeFromOther.Save()
            Next
        End If
        If View.Id = "Recipe_RecipeTo_ListView" Then
            Dim _RecipeTo As RecipeTo
            For Each _RecipeTo In _selection
                If _RecipePopup.Change_RowGroup1 Then
                    _RecipeTo.RowGroup1 = _RecipePopup.RowGroup1
                End If
                If _RecipePopup.Change_RowGroup2 Then
                    _RecipeTo.RowGroup2 = _RecipePopup.RowGroup2
                End If
                _RecipeTo.Save()
            Next
        End If
        If View.Id = "Recipe_RecipeToOther_ListView" Then
            Dim _RecipeToOther As RecipeToOther
            For Each _RecipeToOther In _selection
                If _RecipePopup.Change_RowGroup1 Then
                    _RecipeToOther.RowGroup1 = _RecipePopup.RowGroup1
                End If
                If _RecipePopup.Change_RowGroup2 Then
                    _RecipeToOther.RowGroup2 = _RecipePopup.RowGroup2
                End If
                _RecipeToOther.Save()
            Next
        End If
        _ocur.CommitChanges()
        _noselect = False
    End Sub

    Private Sub aInsertServicesToRecipe_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aInsertServicesToRecipe.CustomizePopupWindowParams
        ' Az akció a szolgáltatások listview-ját hozza fel.
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CollectionSource As CollectionSource = New CollectionSource(_onew, GetType(Services))

        e.View = Application.CreateListView("Services_ListView_Browse", _CollectionSource, False)
    End Sub
    Private Sub aInsertServicesToRecipe_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aInsertServicesToRecipe.Execute
        'A popup-on kiválasztott szolgáltatásokból létrehoz a receptre szolgáltatás sorokat
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Recipe As Recipe = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, Recipe)
        Dim _Services As Services
        Dim _RecipeToOther_New As RecipeToOther

        For Each _Services In e.PopupWindow.View.SelectedObjects
            _RecipeToOther_New = _ocur.CreateObject(Of RecipeToOther)()

            _RecipeToOther_New.Recipe = _ocur.GetObject(_Recipe)
            _RecipeToOther_New.ShortName = _Services.ShortName
            _RecipeToOther_New.Description = _Services.Description
            _RecipeToOther_New.Units = _ocur.GetObject(_Services.Units)
            _RecipeToOther_New.QTT = 1
            _RecipeToOther_New.RowGroup1 = _Services.RowGroup1
            _RecipeToOther_New.RowGroup2 = _Services.RowGroup2

            _RecipeToOther_New.Save()
        Next
        _ocur.CommitChanges()
    End Sub
End Class
