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.Win.Editors
Imports DevExpress.XtraGrid
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.ExpressApp.SystemModule

Public Class ProductFinderVC
    Inherits DevExpress.ExpressApp.ViewController(Of ListView)
    Private _GridListEditor_Selected As GridListEditor
    Private _GridListEditor_Select As GridListEditor
    Private gridView As ColumnView
    Private currentView As ListView
    Private falseCriteria As CriteriaOperator = CriteriaOperator.Parse("1=0")
    Private Edit_QTT_Will As RepositoryItemDoubleEdit
    Private Delegate Sub EditCellDelegate()

    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 = "ProductFinder_ProductSelect_ListView" Then
            currentView = CType(View, ListView)
            AddHandler currentView.ControlsCreated, AddressOf AutoFilterRowController_ControlsCreated
        End If
        If View.Id = "ProductFinder_DetailView" Then
            Frame.GetController(Of RefreshController).Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "ProductFinder_ProductSelected_ListView" Then
            If _GridListEditor_Selected IsNot Nothing Then
                'RemoveHandler _GridListEditor_Selected.GridView.CellValueChanged, AddressOf ProductFinder_ProductSelected_ListView_CellValueChanged
            End If
        End If

    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()

        If View.Id = "ProductFinder_ProductSelected_ListView" Then
            _GridListEditor_Selected = TryCast(View.Editor, GridListEditor)

            If _GridListEditor_Selected IsNot Nothing Then
                AddHandler _GridListEditor_Selected.GridView.CellValueChanged, AddressOf ProductFinder_ProductSelected_ListView_CellValueChanged
            End If


        End If
        If View.Id = "ProductFinder_ProductSelect_ListView" Then
            _GridListEditor_Select = TryCast(TryCast(View, ListView).Editor, GridListEditor)

            For Each column As DevExpress.XtraGrid.Columns.GridColumn In _GridListEditor_Select.GridView.Columns
                column.OptionsFilter.ImmediateUpdateAutoFilter = False
                If column.FieldName = "Products.ShortName" Then
                    column.OptionsFilter.AutoFilterCondition = Columns.AutoFilterCondition.Contains
                End If
            Next
            If _GridListEditor_Select IsNot Nothing Then
                AddHandler _GridListEditor_Select.ColumnCreated, AddressOf Products_ListView_ColumnCreated
                AddHandler _GridListEditor_Select.GridView.ColumnFilterChanged, AddressOf GridView_ColumnFilterChanged
                AddHandler _GridListEditor_Select.GridView.CellValueChanged, AddressOf ProductFinder_ProductSelect_ListView_CellValueChanged
                AddHandler _GridListEditor_Select.GridView.RowClick, AddressOf ProductFinder_ProductSelect_RowClick
            End If
        End If
        If View.Id = "ProductFinder_ProductFinderInfo_ListView" Then
            Dim _GLE As GridListEditor = TryCast(View.Editor, GridListEditor)
            If _GLE IsNot Nothing Then
                If _GLE.GridView IsNot Nothing Then
                    _GLE.GridView.OptionsView.RowAutoHeight = True
                    '_GLE.GridView.OptionsView.ShowColumnHeaders = False
                    '_GLE.GridView.OptionsView.ShowVerticalLines = False
                End If
            End If
        End If
    End Sub

    Private Sub ProductFinder_ProductSelected_ListView_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)

        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        Dim parentView As View = nestedFrame.View
        parentView.ObjectSpace.CommitChanges()

    End Sub
    Private Sub Products_ListView_ColumnCreated(sender As Object, e As ColumnCreatedEventArgs)
        e.Column.OptionsFilter.ImmediateUpdateAutoFilter = False
    End Sub

    Private Sub Timer_Elapsed()
        _GridListEditor_Select.GridView.GridControl.Invoke(New EditCellDelegate(AddressOf Edit_QTT_Will_Cell))
    End Sub
    Private Sub Edit_QTT_Will_Cell()
        'e.KeyCode = Windows.Forms.Keys.Enter And 
        If _GridListEditor_Select.GridView.FocusedRowHandle = GridControl.AutoFilterRowHandle And _GridListEditor_Select.GridView.RowCount >= 1 Then
            '_GridListEditor_Select.GridView.FocusedColumn = _GridListEditor_Select.GridView.GetVisibleColumn(_GridListEditor_Select.FindColumn("QTT_Will").VisibleIndex)
            _GridListEditor_Select.GridView.FocusedColumn = _GridListEditor_Select.GridView.Columns("QTT_Will")
            _GridListEditor_Select.GridView.HideEditor()
            _GridListEditor_Select.GridView.FocusedRowHandle = _GridListEditor_Select.GridView.GetVisibleRowHandle(0)
            _GridListEditor_Select.GridView.ShowEditor()
            If _GridListEditor_Select.GridView.ActiveEditor IsNot Nothing Then
                _GridListEditor_Select.GridView.ActiveEditor.Focus()
            End If
        End If
    End Sub
    Private Sub GridView_ColumnFilterChanged(sender As Object, e As EventArgs)
        Dim timer As System.Timers.Timer = New System.Timers.Timer(200)
        AddHandler timer.Elapsed, AddressOf Timer_Elapsed
        timer.Enabled = True
        timer.AutoReset = False
    End Sub

    Private Sub ProductFinder_ProductSelect_ListView_CellValueChanged(sender As Object, e As Views.Base.CellValueChangedEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        If e.Column.FieldName = "QTT_Will" Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

            Dim _ListView As ListView = TryCast(View, ListView)
            Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)
            Dim _ProductFinder As ProductFinder = TryCast(_PropertyCollectionSource.MasterObject, ProductFinder)


            Dim _ProductNumber As String = _GridListEditor_Select.GridView.GetRowCellValue(e.RowHandle, "Products.ProductNumber")
            Dim _Products As Products = _onew.FindObject(Of Products)(CriteriaOperator.Parse("ProductNumber=?", _ProductNumber))

            Dim _ProductSelected As ProductSelected = _onew.FindObject(Of ProductSelected)(CriteriaOperator.Parse("MasterKey=? AND Products=?", _ProductFinder.MasterKey, _Products))
            If _ProductSelected Is Nothing Then
                _ProductSelected = _onew.CreateObject(Of ProductSelected)()
                _ProductSelected.Products = _Products
                _ProductSelected.MasterKey = _ProductFinder.MasterKey
                _ProductSelected.QTT = _GridListEditor_Select.GridView.GetRowCellValue(e.RowHandle, "QTT_Will")
            Else
                _ProductSelected.QTT += _GridListEditor_Select.GridView.GetRowCellValue(e.RowHandle, "QTT_Will")
            End If

            _onew.CommitChanges()

        End If

        If _GridListEditor_Select.GridView.FocusedColumn Is _GridListEditor_Select.GridView.Columns("QTT_Will") Then
            _GridListEditor_Select.GridView.FocusedColumn = _GridListEditor_Select.GridView.GetVisibleColumn(_GridListEditor_Select.FindColumn("Products.ProductNumber").VisibleIndex)
            _GridListEditor_Select.GridView.FocusedRowHandle = GridControl.AutoFilterRowHandle
            _GridListEditor_Select.GridView.ShowEditor()
            If _GridListEditor_Select.GridView.ActiveEditor IsNot Nothing Then
                _GridListEditor_Select.GridView.ActiveEditor.Focus()
            End If
        End If
        If e.Column.FieldName = "QTT_Will" Then
            Dim _DetailView As DetailView = TryCast(TryCast(Frame, NestedFrame).ViewItem.View, DetailView)
            If _DetailView IsNot Nothing Then
                Dim _ListPropertyEditor As ListPropertyEditor = _DetailView.FindItem("ProductSelected")
                If _ListPropertyEditor IsNot Nothing Then
                    TryCast(_ListPropertyEditor.ListView, ListView).CollectionSource.Reload()
                End If
            End If
        End If
    End Sub
    Private Sub AutoFilterRowController_ControlsCreated(ByVal sender As Object, ByVal e As EventArgs)
        Dim grid As GridControl = TryCast(currentView.Control, GridControl)
        gridView = TryCast(grid.FocusedView, ColumnView)
        If gridView.ActiveFilter.IsEmpty Then
            currentView.CollectionSource.Criteria("FalseCriteria") = falseCriteria
        End If
        AddHandler gridView.ActiveFilter.Changed, AddressOf ActiveFilter_Changed
    End Sub
    Private Sub ActiveFilter_Changed(ByVal sender As Object, ByVal e As EventArgs)
        If (CType(sender, ViewFilter)).IsEmpty Then
            currentView.CollectionSource.Criteria("FalseCriteria") = falseCriteria
        Else
            If currentView.CollectionSource.Criteria.ContainsKey("FalseCriteria") Then
                currentView.CollectionSource.Criteria.Remove("FalseCriteria")
            End If
        End If
    End Sub
    Private Sub ProductFinder_ProductSelect_RowClick(sender As Object, e As Views.Grid.RowClickEventArgs)
        Dim _ProductFinder As ProductFinder
        Dim _Products As Products = _GridListEditor_Select.GridView.GetRowCellValue(e.RowHandle, "Products")
        Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource)
        Dim _ListPropertyEditor As ListPropertyEditor
        Dim _ProductFinderInfo As ProductFinderInfo

        If _PropertyCollectionSource IsNot Nothing Then
            _ProductFinder = TryCast(_PropertyCollectionSource.MasterObject, ProductFinder)
            If _ProductFinder IsNot Nothing Then
                Dim _DetailView As DetailView = TryCast(TryCast(Frame, NestedFrame).ViewItem.View, DetailView)
                If _DetailView IsNot Nothing Then
                    _ListPropertyEditor = _DetailView.FindItem("ProductFinderInfo")
                    If _ListPropertyEditor IsNot Nothing Then

                        Dim _ListView As ListView = TryCast(_ListPropertyEditor.ListView, ListView)
                        If _ListView IsNot Nothing Then
                            Dim _onew As Xpo.XPObjectSpace = TryCast(_ListView.ObjectSpace, Xpo.XPObjectSpace) 'Application.CreateObjectSpace 
                            _ListView.CollectionSource.ResetCollection()
                            If _Products IsNot Nothing Then
                                _ProductFinderInfo = _onew.CreateObject(Of ProductFinderInfo)()
                                _ProductFinderInfo.PropertyName = CaptionHelper.GetMemberCaption(GetType(Products), "ProductGroups")
                                _ProductFinderInfo.InfoString = _Products.ProductGroups.ShortName
                                _ListView.CollectionSource.Add(_ProductFinderInfo)

                                _ProductFinderInfo = _onew.CreateObject(Of ProductFinderInfo)()
                                _ProductFinderInfo.PropertyName = CaptionHelper.GetMemberCaption(GetType(Products), "Description")
                                _ProductFinderInfo.InfoString = _Products.Description
                                _ListView.CollectionSource.Add(_ProductFinderInfo)

                                _ProductFinderInfo = _onew.CreateObject(Of ProductFinderInfo)()
                                _ProductFinderInfo.PropertyName = CaptionHelper.GetMemberCaption(GetType(Products), "ProductNumber")
                                _ProductFinderInfo.InfoString = _Products.ProductNumber
                                _ListView.CollectionSource.Add(_ProductFinderInfo)

                                _ProductFinderInfo = _onew.CreateObject(Of ProductFinderInfo)()
                                _ProductFinderInfo.PropertyName = CaptionHelper.GetMemberCaption(GetType(Products), "ShortName")
                                _ProductFinderInfo.InfoString = _Products.ShortName
                                _ListView.CollectionSource.Add(_ProductFinderInfo)

                                _ProductFinderInfo = _onew.CreateObject(Of ProductFinderInfo)()
                                _ProductFinderInfo.PropertyName = CaptionHelper.GetMemberCaption(GetType(Products), "PacketUnitIn")
                                _ProductFinderInfo.InfoString = _Products.PacketUnitIn.ToString
                                _ListView.CollectionSource.Add(_ProductFinderInfo)

                                _ProductFinderInfo = _onew.CreateObject(Of ProductFinderInfo)()
                                _ProductFinderInfo.PropertyName = CaptionHelper.GetMemberCaption(GetType(Products), "ProductStrategyCode")
                                _ProductFinderInfo.InfoString = _Products.ProductStrategyCode
                                _ListView.CollectionSource.Add(_ProductFinderInfo)

                                _ProductFinderInfo = _onew.CreateObject(Of ProductFinderInfo)()
                                _ProductFinderInfo.PropertyName = CaptionHelper.GetMemberCaption(GetType(Products), "Units")
                                _ProductFinderInfo.InfoString = _Products.Units.ShortName
                                _ListView.CollectionSource.Add(_ProductFinderInfo)

                                _ProductFinderInfo = _onew.CreateObject(Of ProductFinderInfo)()
                                _ProductFinderInfo.PropertyName = CaptionHelper.GetMemberCaption(GetType(Products), "CustomsTariff")
                                If _Products.CustomsTariff IsNot Nothing Then
                                    _ProductFinderInfo.InfoString = _Products.CustomsTariff.CustomsTariffNumber
                                Else
                                    _ProductFinderInfo.InfoString = "nincs beállítva"
                                End If
                                _ListView.CollectionSource.Add(_ProductFinderInfo)


                                _ProductFinderInfo = _onew.CreateObject(Of ProductFinderInfo)()
                                _ProductFinderInfo.PropertyName = CaptionHelper.GetMemberCaption(GetType(Products), "GrossWeight")
                                _ProductFinderInfo.InfoString = _Products.GrossWeight
                                _ListView.CollectionSource.Add(_ProductFinderInfo)


                                _ProductFinderInfo = _onew.CreateObject(Of ProductFinderInfo)()
                                _ProductFinderInfo.PropertyName = CaptionHelper.GetMemberCaption(GetType(Products), "ListPriceOutHUF")
                                _ProductFinderInfo.InfoString = _Products.ListPriceOutHUF.ToString
                                _ListView.CollectionSource.Add(_ProductFinderInfo)

                                _ProductFinderInfo = _onew.CreateObject(Of ProductFinderInfo)()
                                _ProductFinderInfo.PropertyName = CaptionHelper.GetMemberCaption(GetType(Products), "VAT")
                                _ProductFinderInfo.InfoString = _Products.VAT.ShortName
                                _ListView.CollectionSource.Add(_ProductFinderInfo)

                                Dim _PricingActionRows As PricingActionRows = _onew.Session.FindObject(Of PricingActionRows) _
                                    (_onew.Session.ParseCriteria("Products=? and PricingActionHeader.IsActive=1 and PricingActionHeader.DateFrom<=Now() and PricingActionHeader.DateTo>=Now()", _Products))

                                If _PricingActionRows IsNot Nothing Then

                                    _ProductFinderInfo = _onew.CreateObject(Of ProductFinderInfo)()
                                    _ProductFinderInfo.PropertyName = "Akció!"
                                    _ProductFinderInfo.InfoString = Format(_PricingActionRows.ListPriceHUF, "#,##0") & Chr(13) & Chr(10) & _
                                        _PricingActionRows.PricingActionHeader.ShortName & Chr(13) & Chr(10) & _PricingActionRows.PricingActionHeader.DateFrom.ToShortDateString & _
                                        " - " & _PricingActionRows.PricingActionHeader.DateTo.ToShortDateString
                                    _ListView.CollectionSource.Add(_ProductFinderInfo)

                                End If

                            End If

                            '_ListView.CollectionSource.Reload()
                        End If

                    End If
                    _ListPropertyEditor = _DetailView.FindItem("StorageOutCirculation")
                    If _ListPropertyEditor IsNot Nothing Then
                        Dim _ListView As ListView = TryCast(_ListPropertyEditor.ListView, ListView)
                        If _ListView IsNot Nothing Then
                            Dim _ocur As Xpo.XPObjectSpace = TryCast(_ListView.ObjectSpace, Xpo.XPObjectSpace) 'Application.CreateObjectSpace 
                            _ListView.CollectionSource.ResetCollection()
                            If _Products IsNot Nothing Then
                                Dim _StorageOutCirculation As StorageOutCirculation
                                Dim _StorageOutCirculation_Collection As New XPCollection(Of StorageOutCirculation)(_ocur.Session, CriteriaOperator.Parse("Products.Oid=?", _Products.Oid))
                                For Each _StorageOutCirculation In _StorageOutCirculation_Collection
                                    _ListView.CollectionSource.Add(_StorageOutCirculation)
                                Next

                            End If

                        End If
                    End If
                End If

            End If
        End If
    End Sub



End Class
