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
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.Model

Public Class ViewFilterContainerVC
    Inherits DevExpress.ExpressApp.ViewController
    Private dialogCotnroller As DialogController
    Private oldListView As View
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "ViewFilterContainer_DetailView" Then
            If Frame.GetController(Of DialogController)() IsNot Nothing Then
                Frame.GetController(Of DialogController).AcceptAction.Active.SetItemValue("", False)
                Frame.GetController(Of DialogController).CancelAction.Active.SetItemValue("", False)
            End If
            If Frame.GetController(Of RefreshController)() IsNot Nothing Then
                Frame.GetController(Of RefreshController).Active.SetItemValue("", False)
            End If
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            'Frame.GetController(Of RecordsNavigationController).Active.SetItemValue("", False)

        End If

        Frame.GetController(Of ViewFilterContainerVC).aInitCriteria.Active.SetItemValue("", False)
        If TryCast(View, ListView) IsNot Nothing Then
            Dim _IModelListViewExtender As IModelListViewExtender = TryCast(View.Model, IModelListViewExtender)
            If _IModelListViewExtender IsNot Nothing Then
                If _IModelListViewExtender.RequireCriteriaBeforeView Then
                    _IModelListViewExtender.DefaultDateYearInFilter = False
                    TryCast(View.Model, IModelListView).DataAccessMode = CollectionSourceDataAccessMode.Client
                    View.SaveModel()
                    Frame.GetController(Of ViewFilterContainerVC).aInitCriteria.Active.SetItemValue("", True)

                End If
            End If
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "ViewFilterContainer_DetailView" Then
            If Frame.GetController(Of DialogController)() IsNot Nothing Then
                Frame.GetController(Of DialogController).AcceptAction.Active.SetItemValue("", True)
                Frame.GetController(Of DialogController).CancelAction.Active.SetItemValue("", True)
            End If
            If Frame.GetController(Of RefreshController)() IsNot Nothing Then
                Frame.GetController(Of RefreshController).Active.SetItemValue("", True)
            End If
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", True)
            'Frame.GetController(Of RecordsNavigationController).Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aResetRequireCriteriaBeforeView_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aResetRequireCriteriaBeforeView.Execute
        Dim _ViewFilterContainer As ViewFilterContainer = TryCast(View.SelectedObjects(0), ViewFilterContainer)
        Dim _IModelListViewExtender As IModelListViewExtender = TryCast(View.Model, IModelListViewExtender)

        For Each _View In Application.Model.Views
            If _View.Id = _ViewFilterContainer.ViewID Then

                If TryCast(_View, IModelListViewExtender) IsNot Nothing Then
                    _ViewFilterContainer.Criteria = TryCast(_View, IModelListViewExtender).RequireCriteriaBeforeViewCriteria
                End If
                Exit For
            End If
        Next
    End Sub

    Private Sub aFindRecords_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFindRecords.Execute
        If Frame.GetController(Of DialogController)() IsNot Nothing Then
            Frame.GetController(Of DialogController).AcceptAction.Active.SetItemValue("", True)
            Frame.GetController(Of DialogController).AcceptAction.DoExecute()
        Else
            AcceptAction_Execute(sender, e)
        End If
    End Sub

    Private Sub aSaveCriteria_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aSaveCriteria.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ViewFilterContainerSaved As ViewFilterContainerSaved = _onew.CreateObject(Of ViewFilterContainerSaved)()
        e.View = Application.CreateDetailView(_onew, "ViewFilterContainerSaved_DetailView", False, _ViewFilterContainerSaved)

    End Sub

    Private Sub aSaveCriteria_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aSaveCriteria.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ViewFilterContainer As ViewFilterContainer = TryCast(View.SelectedObjects(0), ViewFilterContainer)
        Dim _ViewFilterContainerSaved_Popup As ViewFilterContainerSaved = TryCast(e.PopupWindow.View.SelectedObjects(0), ViewFilterContainerSaved)

        Dim _ViewFilterContainerSaved As ViewFilterContainerSaved = _ocur.CreateObject(Of ViewFilterContainerSaved)()
        _ViewFilterContainerSaved.ViewID = _ViewFilterContainer.ViewID
        _ViewFilterContainerSaved.ShortName = _ViewFilterContainerSaved_Popup.ShortName
        _ViewFilterContainerSaved.IsPublic = _ViewFilterContainerSaved_Popup.IsPublic
        _ViewFilterContainerSaved.Criteria = _ViewFilterContainer.Criteria

        _ocur.CommitChanges()
        View.Refresh()



    End Sub

    Private Sub aLoadCriteria_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aLoadCriteria.Execute
        Dim _ViewFilterContainer As ViewFilterContainer = TryCast(View.SelectedObjects(0), ViewFilterContainer)
        Dim _ListView As ListPropertyEditor = TryCast(View, DetailView).FindItem("ViewFilterContainerSaved")

        If _ListView.ListView.SelectedObjects.Count > 0 Then
            _ViewFilterContainer.Criteria = TryCast(_ListView.ListView.SelectedObjects(0), ViewFilterContainerSaved).Criteria
        End If

    End Sub

    Private Sub aDeleteCriteria_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aDeleteCriteria.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ViewFilterContainerSaved As ViewFilterContainerSaved = TryCast(View.SelectedObjects(0), ViewFilterContainerSaved)

        For Each _ViewFilterContainerSaved In View.SelectedObjects
            _ocur.Delete(_ViewFilterContainerSaved)
        Next
        _ocur.CommitChanges()

    End Sub

    Private Sub aInitCriteria_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aInitCriteria.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        oldListView = View
        Dim newViewFilterContainer As ViewFilterContainer = _onew.CreateObject(Of ViewFilterContainer)()
        Dim showViewParameters As ShowViewParameters = e.ShowViewParameters
        newViewFilterContainer.ObjectType = oldListView.ObjectTypeInfo.Type
        newViewFilterContainer.ViewID = oldListView.Id
        newViewFilterContainer.Criteria = CType(oldListView.Model, IModelListViewExtender).AdditionalCriteria
        showViewParameters.CreatedView = Application.CreateDetailView(_onew, newViewFilterContainer)
        showViewParameters.CreatedView.Caption = String.Format((CType(oldListView.Model, IModelListViewExtender)).RequireCriteriaBeforeViewCaption, oldListView.Caption)
        showViewParameters.TargetWindow = TargetWindow.Current

        dialogCotnroller = Application.CreateController(Of DialogController)()
        AddHandler dialogCotnroller.AcceptAction.Execute, AddressOf AcceptAction_Execute
        showViewParameters.Controllers.Add(dialogCotnroller)
    End Sub

    Private Sub AcceptAction_Execute(sender As Object, e As SimpleActionExecuteEventArgs)
        Try
            Dim currentViewFilterContainer As ViewFilterContainer = CType(e.CurrentObject, ViewFilterContainer)
            Dim _CollectionSource As CollectionSource
            Dim _IModelListView As IModelListView
            Dim _ModelClass As IModelClass
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            _IModelListView = TryCast(oldListView.Model, IModelListView)
            _ModelClass = TryCast(_IModelListView.ModelClass, IModelClass)

            _CollectionSource = New CollectionSource(_onew, _ModelClass.TypeInfo.Type)

            _CollectionSource.BeginUpdateCriteria()
            _CollectionSource.Criteria("ByViewFilterObject") = CriteriaEditorHelper.GetCriteriaOperator(currentViewFilterContainer.Criteria, currentViewFilterContainer.ObjectType, _onew)
            _CollectionSource.EndUpdateCriteria()

            CType(oldListView.Model, IModelListViewExtender).AdditionalCriteria = currentViewFilterContainer.Criteria


            e.ShowViewParameters.CreatedView = Application.CreateListView(oldListView.Id, _CollectionSource, False)
            e.ShowViewParameters.TargetWindow = TargetWindow.Current
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Hiba")
        End Try
    End Sub

End Class
