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.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo

Imports Microsoft.VisualBasic
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.Data
Imports DevExpress.ExpressApp.Win.Editors
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp.Win
Imports System.IO
Imports DevExpress.ExpressApp.Utils

Public Class DragAndDropCRMOutlookProducts_VC
    Inherits DevExpress.ExpressApp.ViewController

    Private _Selection As New ArrayList
    Dim _ActSelection As Object
    Private downHitInfo As GridHitInfo = Nothing
    Dim _WaitFormClass As New WaitFormClass
    Private _CRM_Opportunities As CRM_Opportunities = Nothing
    Private _AllowSelect_CRM_Opportunities As Boolean = False

    Private _DoDragGridViewOutlookEMailAttachments As DevExpress.XtraGrid.Views.Grid.GridView
    Private _DoDragGridControlOutlookEMailAttachments As DevExpress.XtraGrid.GridControl

    Private _ToDragGridControlProducts As DevExpress.XtraGrid.GridControl
    Private _ToDragGridViewProducts As DevExpress.XtraGrid.Views.Grid.GridView
    Private _ToDragGridControlProductsDocumentations As DevExpress.XtraGrid.GridControl
    Private _ToDragGridViewProductsDocumentations As DevExpress.XtraGrid.Views.Grid.GridView
    Private _ToDragGridControlCustomersOrderPriority As DevExpress.XtraGrid.GridControl
    Private _ToDragGridViewCustomersOrderPriority As DevExpress.XtraGrid.Views.Grid.GridView
    Private _ToDragGridControlCustomersFileAttachment As DevExpress.XtraGrid.GridControl
    Private _ToDragGridViewCustomersFileAttachment As DevExpress.XtraGrid.Views.Grid.GridView

    Public Sub New()
        MyBase.New()

        InitializeComponent()
        RegisterActions(components)

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

        If View.Id = "OutlookEMailIn_OutlookEMailAttachments_ListView" Then
            Dim _View As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            _DoDragGridViewOutlookEMailAttachments = TryCast(_View.Editor, DevExpress.ExpressApp.Win.Editors.GridListEditor).GridView
            _DoDragGridViewOutlookEMailAttachments.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseUp
            _DoDragGridControlOutlookEMailAttachments = _DoDragGridViewOutlookEMailAttachments.GridControl

            AddHandler _DoDragGridViewOutlookEMailAttachments.MouseDown, AddressOf DoDragGridViewOutlookEMailAttachments_MouseDown_MouseDown
            AddHandler _DoDragGridViewOutlookEMailAttachments.MouseMove, AddressOf DoDragGridViewOutlookEMailAttachments_MouseDown_MouseMove
            AddHandler View.SelectionChanged, AddressOf OutlookEMailIn_OutlookEMailAttachments_ListView_SelectionChanged
        End If
        If View.Id = "Products_ListView_CRM_Outlook" Then
            Dim _View As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            _ToDragGridViewProducts = TryCast(_View.Editor, DevExpress.ExpressApp.Win.Editors.GridListEditor).GridView
            _ToDragGridControlProducts = _ToDragGridViewProducts.GridControl

            AddHandler _ToDragGridControlProducts.DragOver, AddressOf ToDragGridControlProducts_DragOver
            AddHandler _ToDragGridControlProducts.DragDrop, AddressOf ToDragGridControlProducts_DragDrop
        End If
        If View.Id = "Products_ProductsDocumentations_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.FileAttachments.Win.FileAttachmentListViewController).Active.SetItemValue("", False)

            Dim _View As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            _ToDragGridViewProductsDocumentations = TryCast(_View.Editor, DevExpress.ExpressApp.Win.Editors.GridListEditor).GridView
            _ToDragGridControlProductsDocumentations = _ToDragGridViewProductsDocumentations.GridControl

            AddHandler _ToDragGridControlProductsDocumentations.DragOver, AddressOf ToDragGridControlProductsDocumentations_DragOver
            AddHandler _ToDragGridControlProductsDocumentations.DragDrop, AddressOf ToDragGridControlProductsDocumentations_DragDrop
        End If
        If View.Id = "Products_CustomersOrderPriority_ListView_CRM_Outlook" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController).Active.SetItemValue("", False)

            Dim _View As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            _ToDragGridViewCustomersOrderPriority = TryCast(_View.Editor, DevExpress.ExpressApp.Win.Editors.GridListEditor).GridView
            _ToDragGridControlCustomersOrderPriority = _ToDragGridViewCustomersOrderPriority.GridControl

            AddHandler _ToDragGridControlCustomersOrderPriority.DragOver, AddressOf ToDragGridControlCustomersOrderPriority_DragOver
            AddHandler _ToDragGridControlCustomersOrderPriority.DragDrop, AddressOf ToDragGridControlCustomersOrderPrioritys_DragDrop
        End If
        If View.Id = "CustomersFileAttachment_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.FileAttachments.Win.FileAttachmentListViewController).Active.SetItemValue("", False)

            Dim _View As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            _ToDragGridViewCustomersFileAttachment = TryCast(_View.Editor, DevExpress.ExpressApp.Win.Editors.GridListEditor).GridView

            _ToDragGridControlCustomersFileAttachment = _ToDragGridViewCustomersFileAttachment.GridControl

            AddHandler _ToDragGridControlCustomersFileAttachment.DragOver, AddressOf ToDragGridControlCustomersFileAttachment_DragOver
            AddHandler _ToDragGridControlCustomersFileAttachment.DragDrop, AddressOf ToDragGridControlCustomersFileAttachment_DragDrop
        End If
    End Sub
    Private Sub DoDragGridViewOutlookEMailAttachments_MouseDown_MouseDown(sender As Object, e As MouseEventArgs)
        Dim view As GridView = TryCast(sender, GridView)
        downHitInfo = Nothing

        Dim hitInfo As GridHitInfo = view.CalcHitInfo(New Point(e.X, e.Y))
        If Control.ModifierKeys <> Keys.None Then
            Return
        End If
        If e.Button = MouseButtons.Left AndAlso hitInfo.InRow AndAlso hitInfo.HitTest <> GridHitTest.RowIndicator Then
            downHitInfo = hitInfo
        End If
    End Sub
    Private Sub DoDragGridViewOutlookEMailAttachments_MouseDown_MouseMove(sender As Object, e As MouseEventArgs)
        Dim view As GridView = TryCast(sender, GridView)
        If e.Button = MouseButtons.Left AndAlso downHitInfo IsNot Nothing Then
            Dim dragSize As Size = SystemInformation.DragSize
            Dim dragRect As New Rectangle(New Point(downHitInfo.HitPoint.X - dragSize.Width / 2, downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize)

            If (Not dragRect.Contains(New Point(e.X, e.Y))) Then
                _AllowSelect_CRM_Opportunities = True
                view.GridControl.DoDragDrop(_Selection, DragDropEffects.All)
                downHitInfo = Nothing
            End If
        End If
    End Sub
    Private Sub OutlookEMailIn_OutlookEMailAttachments_ListView_SelectionChanged(sender As Object, e As EventArgs)
        _Selection.Clear()
        _Selection.AddRange(View.SelectedObjects)
    End Sub

    Private Sub ToDragGridControlProducts_DragOver(sender As Object, e As DragEventArgs)
        e.Effect = DragDropEffects.Copy
        Dim _ListView As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
        If _ListView Is Nothing Then Return
        Dim _Grid As DevExpress.XtraGrid.GridControl = TryCast(sender, DevExpress.XtraGrid.GridControl)
        Dim _GridHitInfo As GridHitInfo = _Grid.MainView.CalcHitInfo(_Grid.PointToClient(New Point(e.X, e.Y)))

        '    If Not _ToDragGridViewProducts.IsDataRow(_GridHitInfo.RowHandle) Then _ToDragGridViewProducts.CollapseAllGroups()
        If _ToDragGridViewProducts.IsGroupRow(_GridHitInfo.RowHandle) Then _ToDragGridViewProducts.ExpandGroupRow(_GridHitInfo.RowHandle)

        If _ToDragGridViewProducts.IsDataRow(_GridHitInfo.RowHandle) Then
            _ToDragGridViewProducts.ClearSelection()
            _ToDragGridViewProducts.SelectRow(_GridHitInfo.RowHandle)
        End If
    End Sub
    Private Sub ToDragGridControlProducts_DragDrop(sender As Object, e As DragEventArgs)
        Try
            Dim _ListView As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            If _ListView Is Nothing Then Return
            Dim _Grid As DevExpress.XtraGrid.GridControl = TryCast(sender, DevExpress.XtraGrid.GridControl)
            Dim _GridHitInfo As GridHitInfo = _Grid.MainView.CalcHitInfo(_Grid.PointToClient(New Point(e.X, e.Y)))
            If Not _GridHitInfo.InRow OrElse Not _GridHitInfo.InRowCell Then Return
            If Not DirectCast(_Grid.MainView, GridView).IsDataRow(_GridHitInfo.RowHandle) Then Return
            Dim _Products As Products = TryCast(_Grid.MainView.GetRow(_GridHitInfo.RowHandle), Products)
            If _Products Is Nothing Then Return

            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

            Dim _AttachmentSelection As ArrayList = e.Data.GetData(GetType(ArrayList))
            If _AttachmentSelection IsNot Nothing Then
                '_WaitFormClass.InitWork(0, 1, _AttachmentSelection.Count)
                _Selection.Clear()
                For Each _OutlookEMailAttachments As OutlookEMailAttachments In _AttachmentSelection
                    Dim _ProductsDocumentations As New ProductsDocumentations(_onew.Session)
                    With (_ProductsDocumentations)
                        .Products = _onew.GetObject(_Products)
                        .Status = eProductsDocumentationStatus.ePublic
                        .File = _onew.GetObject(_OutlookEMailAttachments.DocumentFileData.File)
                        .ShortName = .File.FileName
                    End With
                    _Selection.Add(_ProductsDocumentations)
                    _OutlookEMailAttachments.OutlookEMailIn.IsReaded = True
                    _OutlookEMailAttachments.OutlookEMailIn.Qualified = True
                    '_WaitFormClass.DoWork
                Next
                _onew.CommitChanges()

                CType(New PopupWindowShowActionHelper(Frame.GetController(Of DragAndDropCRMOutlookProducts_VC).aDragDrop_Products_Change), PopupWindowShowActionHelper).ShowPopupWindow()
            Else
                Dim _FileIn As Object = e.Data.GetData(DataFormats.FileDrop)
                If _FileIn IsNot Nothing Then
                    '  _WaitFormClass.InitWork(0, 1, TryCast(_FileIn, String()).Length)
                    _Selection.Clear()
                    For Each _ActFile As String In _FileIn
                        If System.IO.File.Exists(_ActFile) Then
                            Dim _ProductsDocumentations As New ProductsDocumentations(_onew.Session)
                            With (_ProductsDocumentations)
                                .Products = _onew.GetObject(_Products)
                                .Status = eProductsDocumentationStatus.ePublic
                                .File = LoadOtherFile(_ActFile, New FileData(_onew.Session))
                                .ShortName = .File.FileName
                                .FileCreated = GetFileCreatedDate(_ActFile)
                                .FileModified = GetFileModifiedDate(_ActFile)
                            End With

                            _Selection.Add(_ProductsDocumentations)
                        End If
                        '  _WaitFormClass.DoWork
                    Next
                    _onew.CommitChanges()

                    CType(New PopupWindowShowActionHelper(Frame.GetController(Of DragAndDropCRMOutlookProducts_VC).aDragDrop_Products_Change), PopupWindowShowActionHelper).ShowPopupWindow()
                End If
            End If
        Catch ex As InvalidCastException
            MsgBox("Termékhez nem lehet e-mailt rendeleni!", MsgBoxStyle.OkOnly, "Hiba!")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        Finally
            '  _Waitformclass.FinalWork
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RefreshController).RefreshAction.DoExecute()
        End Try
    End Sub

    Private Sub ToDragGridControlProductsDocumentations_DragOver(sender As Object, e As DragEventArgs)
        ' e.Effect = DragDropEffects.Copy
        Dim _ListView As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
        If _ListView Is Nothing Then Return
        Dim _Grid As DevExpress.XtraGrid.GridControl = TryCast(sender, DevExpress.XtraGrid.GridControl)
        Dim _GridHitInfo As GridHitInfo = _Grid.MainView.CalcHitInfo(_Grid.PointToClient(New Point(e.X, e.Y)))

        If _ToDragGridViewProductsDocumentations.IsGroupRow(_GridHitInfo.RowHandle) Then _ToDragGridViewProductsDocumentations.ExpandGroupRow(_GridHitInfo.RowHandle)
        _ActSelection = Nothing

        If Not _ToDragGridViewProductsDocumentations.IsDataRow(_GridHitInfo.RowHandle) Then
            e.Effect = DragDropEffects.None
            _ToDragGridViewProductsDocumentations.Tag = "NODROPDOC"

        ElseIf _ToDragGridViewProductsDocumentations.IsDataRow(_GridHitInfo.RowHandle) Then
            e.Effect = DragDropEffects.Copy
            _ToDragGridViewProductsDocumentations.ClearSelection()
            _ToDragGridViewProductsDocumentations.SelectRow(_GridHitInfo.RowHandle)
            _ActSelection = _ToDragGridViewProductsDocumentations.GetRow(_ToDragGridViewProductsDocumentations.GetSelectedRows(0))
            _ToDragGridViewProductsDocumentations.Tag = ""
        End If
    End Sub
    Private Sub ToDragGridControlProductsDocumentations_DragDrop(sender As Object, e As DragEventArgs)
        Try
            If _ToDragGridViewProductsDocumentations.Tag = "NODROPDOC" Then
                _ToDragGridViewProductsDocumentations.Tag = ""
                Return
            End If

            Dim _ListView As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            If _ListView Is Nothing Then Return
            Dim _Grid As DevExpress.XtraGrid.GridControl = TryCast(sender, DevExpress.XtraGrid.GridControl)
            Dim _GridHitInfo As GridHitInfo = _Grid.MainView.CalcHitInfo(_Grid.PointToClient(New Point(e.X, e.Y)))
            Dim _ProductsDocumentations As ProductsDocumentations = Nothing

            If _ActSelection IsNot Nothing Then
                _ProductsDocumentations = TryCast(_ActSelection, ProductsDocumentations)
            Else
                If Not _GridHitInfo.InRow OrElse Not _GridHitInfo.InRowCell Then Return
                If Not DirectCast(_Grid.MainView, GridView).IsDataRow(_GridHitInfo.RowHandle) Then Return
                _ProductsDocumentations = TryCast(_Grid.MainView.GetRow(_GridHitInfo.RowHandle), ProductsDocumentations)
            End If

            If _ProductsDocumentations Is Nothing Then Return

            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)


            Dim _AttachmentSelection As ArrayList = e.Data.GetData(GetType(ArrayList))
            If _AttachmentSelection IsNot Nothing Then
                _WaitFormClass.Initwork(1, 1, _AttachmentSelection.Count)
                For Each _OutlookEMailAttachments As OutlookEMailAttachments In _AttachmentSelection
                    Dim _ProductsDocumentationsNew As New ProductsDocumentations(_ocur.Session)
                    With (_ProductsDocumentationsNew)
                        .RowGroup1 = _ProductsDocumentations.RowGroup1
                        .RowGroup2 = _ProductsDocumentations.RowGroup2
                        .Products = _ocur.GetObject(_ProductsDocumentations.Products)
                        .Status = eProductsDocumentationStatus.ePublic
                        .File = _ocur.GetObject(_OutlookEMailAttachments.DocumentFileData.File)
                        .ShortName = .File.FileName
                    End With
                    _Selection.Add(_ProductsDocumentations)
                    _OutlookEMailAttachments.OutlookEMailIn.IsReaded = True
                    _OutlookEMailAttachments.OutlookEMailIn.Qualified = True
                    _WaitFormClass.DoWork()
                Next
                _ocur.CommitChanges()

            Else
                Dim _FileIn As Object = e.Data.GetData(DataFormats.FileDrop)
                If _FileIn IsNot Nothing Then
                    _WaitFormClass.InitWork(0, 1, TryCast(_FileIn, String()).Length)
                    For Each _ActFile As String In _FileIn
                        If System.IO.File.Exists(_ActFile) Then
                            Dim _ProductsDocumentationsNew As New ProductsDocumentations(_ocur.Session)
                            With (_ProductsDocumentationsNew)
                                .RowGroup1 = _ProductsDocumentations.RowGroup1
                                .RowGroup2 = _ProductsDocumentations.RowGroup2
                                .Products = _ocur.GetObject(_ProductsDocumentations.Products)
                                .Status = eProductsDocumentationStatus.ePublic
                                .File = LoadOtherFile(_ActFile, New FileData(_ocur.Session))
                                .ShortName = .File.FileName
                                .FileCreated = GetFileCreatedDate(_ActFile)
                                .FileModified = GetFileModifiedDate(_ActFile)
                            End With
                        End If
                        _WaitFormClass.DoWork()
                    Next
                    _ocur.CommitChanges()

                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        Finally
            _Waitformclass.FinalWork()
        End Try
    End Sub

    Private Sub ToDragGridControlCustomersOrderPriority_DragOver(sender As Object, e As DragEventArgs)
        e.Effect = DragDropEffects.Copy
        Dim _ListView As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
        If _ListView Is Nothing Then Return
        Dim _Grid As DevExpress.XtraGrid.GridControl = TryCast(sender, DevExpress.XtraGrid.GridControl)
        Dim _GridHitInfo As GridHitInfo = _Grid.MainView.CalcHitInfo(_Grid.PointToClient(New Point(e.X, e.Y)))

        '   If Not _ToDragGridViewCustomersOrderPriority.IsDataRow(_GridHitInfo.RowHandle) Then _ToDragGridViewCustomersOrderPriority.CollapseAllGroups()
        If _ToDragGridViewCustomersOrderPriority.IsGroupRow(_GridHitInfo.RowHandle) Then _ToDragGridViewCustomersOrderPriority.ExpandGroupRow(_GridHitInfo.RowHandle)

        If _ToDragGridViewCustomersOrderPriority.IsDataRow(_GridHitInfo.RowHandle) Then
            _ToDragGridViewCustomersOrderPriority.ClearSelection()
            _ToDragGridViewCustomersOrderPriority.SelectRow(_GridHitInfo.RowHandle)
        End If
    End Sub
    Private Sub ToDragGridControlCustomersOrderPrioritys_DragDrop(sender As Object, e As DragEventArgs)
        Try
            Dim _ListView As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            If _ListView Is Nothing Then Return
            Dim _Grid As DevExpress.XtraGrid.GridControl = TryCast(sender, DevExpress.XtraGrid.GridControl)
            Dim _GridHitInfo As GridHitInfo = _Grid.MainView.CalcHitInfo(_Grid.PointToClient(New Point(e.X, e.Y)))
            If Not _GridHitInfo.InRow OrElse Not _GridHitInfo.InRowCell Then Return
            If Not DirectCast(_Grid.MainView, GridView).IsDataRow(_GridHitInfo.RowHandle) Then Return
            Dim _CustomersOrderPriority As CustomersOrderPriority = TryCast(_Grid.MainView.GetRow(_GridHitInfo.RowHandle), CustomersOrderPriority)
            If _CustomersOrderPriority Is Nothing Then Return

            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

            Dim _AttachmentSelection As ArrayList = e.Data.GetData(GetType(ArrayList))
            If _AttachmentSelection IsNot Nothing Then
                ' _WaitFormClass.InitWork(0, 1, _AttachmentSelection.Count)
                _Selection.Clear()
                For Each _OutlookEMailAttachments As OutlookEMailAttachments In _AttachmentSelection
                    Dim _CustomersFileAttachment As New CustomersFileAttachment(_onew.Session)
                    With (_CustomersFileAttachment)
                        .Customers = _onew.GetObject(TryCast(_CustomersOrderPriority.CustomersOrder, Customers))
                        .File = _onew.GetObject(_OutlookEMailAttachments.DocumentFileData.File)
                        .File.FileName = _OutlookEMailAttachments.DocumentFileData.File.FileName
                        .ShortName = .File.FileName
                    End With
                    _Selection.Add(_CustomersFileAttachment)
                    _OutlookEMailAttachments.OutlookEMailIn.IsReaded = True
                    _OutlookEMailAttachments.OutlookEMailIn.Qualified = True
                    '  _WaitFormClass.DoWork
                Next
                _onew.CommitChanges()

                CType(New PopupWindowShowActionHelper(Frame.GetController(Of DragAndDropCRMOutlookProducts_VC).aDragDrop_CustomersDoc_Change), PopupWindowShowActionHelper).ShowPopupWindow()
            Else
                Dim _FileIn As Object = e.Data.GetData(DataFormats.FileDrop)
                If _FileIn IsNot Nothing Then
                    '  _WaitFormClass.InitWork(0, 1, TryCast(_FileIn, String()).Length)
                    _Selection.Clear()
                    For Each _ActFile As String In _FileIn
                        If System.IO.File.Exists(_ActFile) Then
                            Dim _CustomersFileAttachment As New CustomersFileAttachment(_onew.Session)
                            With (_CustomersFileAttachment)
                                .Customers = _onew.GetObject(TryCast(_CustomersOrderPriority.CustomersOrder, Customers))
                                .File = LoadOtherFile(_ActFile, New FileData(_onew.Session))
                                .ShortName = .File.FileName
                                .FileCreated = GetFileCreatedDate(_ActFile)
                                .FileModified = GetFileModifiedDate(_ActFile)
                            End With
                            _Selection.Add(_CustomersFileAttachment)
                        End If
                        '  _WaitFormClass.DoWork
                    Next
                    _onew.CommitChanges()

                    CType(New PopupWindowShowActionHelper(Frame.GetController(Of DragAndDropCRMOutlookProducts_VC).aDragDrop_CustomersDoc_Change), PopupWindowShowActionHelper).ShowPopupWindow()
                End If
            End If
        Catch ex As InvalidCastException
            MsgBox("Beszállítóhoz nem lehet e-mailt rendeleni!", MsgBoxStyle.OkOnly, "Hiba!")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        Finally
            ' _Waitformclass.FinalWork

        End Try
    End Sub

    Private Sub ToDragGridControlCustomersFileAttachment_DragOver(sender As Object, e As DragEventArgs)
        'e.Effect = DragDropEffects.Copy
        Dim _ListView As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
        If _ListView Is Nothing Then Return
        Dim _Grid As DevExpress.XtraGrid.GridControl = TryCast(sender, DevExpress.XtraGrid.GridControl)
        Dim _GridHitInfo As GridHitInfo = _Grid.MainView.CalcHitInfo(_Grid.PointToClient(New Point(e.X, e.Y)))

        '  If Not _ToDragGridViewCustomersFileAttachment.IsDataRow(_GridHitInfo.RowHandle) Then _ToDragGridViewCustomersFileAttachment.CollapseAllGroups()
        If _ToDragGridViewCustomersFileAttachment.IsGroupRow(_GridHitInfo.RowHandle) Then _ToDragGridViewCustomersFileAttachment.ExpandGroupRow(_GridHitInfo.RowHandle)
        _ActSelection = Nothing

        If Not _ToDragGridViewCustomersFileAttachment.IsDataRow(_GridHitInfo.RowHandle) Then
            e.Effect = DragDropEffects.None
            _ToDragGridViewCustomersFileAttachment.Tag = "NODROPATTACH"

        ElseIf _ToDragGridViewCustomersFileAttachment.IsDataRow(_GridHitInfo.RowHandle) Then
            e.Effect = DragDropEffects.Copy
            _ToDragGridViewCustomersFileAttachment.ClearSelection()
            _ToDragGridViewCustomersFileAttachment.SelectRow(_GridHitInfo.RowHandle)
            _ActSelection = _ToDragGridViewCustomersFileAttachment.GetRow(_ToDragGridViewCustomersFileAttachment.GetSelectedRows(0))
            _ToDragGridViewCustomersFileAttachment.Tag = ""
        End If
    End Sub
    Private Sub ToDragGridControlCustomersFileAttachment_DragDrop(sender As Object, e As DragEventArgs)
        Try

            If _ToDragGridViewCustomersFileAttachment.Tag = "NODROPATTACH" Then
                _ToDragGridViewCustomersFileAttachment.Tag = ""
                Return
            End If

            Dim _ListView As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            If _ListView Is Nothing Then Return
            Dim _Grid As DevExpress.XtraGrid.GridControl = TryCast(sender, DevExpress.XtraGrid.GridControl)
            Dim _GridHitInfo As GridHitInfo = _Grid.MainView.CalcHitInfo(_Grid.PointToClient(New Point(e.X, e.Y)))
            Dim _CustomersFileAttachment As CustomersFileAttachment = Nothing
            If _ActSelection IsNot Nothing Then
                _CustomersFileAttachment = TryCast(_ActSelection, CustomersFileAttachment)
            Else
                If Not _GridHitInfo.InRow OrElse Not _GridHitInfo.InRowCell Then Return
                If Not DirectCast(_Grid.MainView, GridView).IsDataRow(_GridHitInfo.RowHandle) Then Return

                _CustomersFileAttachment = TryCast(_Grid.MainView.GetRow(_GridHitInfo.RowHandle), CustomersFileAttachment)
            End If

            If _CustomersFileAttachment Is Nothing Then Return

            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

            Dim _AttachmentSelection As ArrayList = e.Data.GetData(GetType(ArrayList))
            If _AttachmentSelection IsNot Nothing Then
                _WaitFormClass.InitWork(0, 1, _AttachmentSelection.Count)
                For Each _OutlookEMailAttachments As OutlookEMailAttachments In _AttachmentSelection
                    Dim _CustomersFileAttachmentNew As New CustomersFileAttachment(_ocur.Session)
                    With (_CustomersFileAttachmentNew)
                        .RowGroup1 = _CustomersFileAttachment.RowGroup1
                        .RowGroup2 = _CustomersFileAttachment.RowGroup2
                        .Customers = _ocur.GetObject(_CustomersFileAttachment.Customers)
                        .File = _ocur.GetObject(_OutlookEMailAttachments.DocumentFileData.File)
                        .ShortName = .File.FileName
                        .FileCreated = GetFileCreatedDate(.File)
                        .FileModified = GetFileModifiedDate(.File)
                    End With
                    _OutlookEMailAttachments.OutlookEMailIn.IsReaded = True
                    _OutlookEMailAttachments.OutlookEMailIn.Qualified = True
                    _WaitFormClass.DoWork()
                Next
                _ocur.CommitChanges()

            Else
                Dim _FileIn As Object = e.Data.GetData(DataFormats.FileDrop)
                If _FileIn IsNot Nothing Then
                    _WaitFormClass.InitWork(0, 1, TryCast(_FileIn, String()).Length)
                    For Each _ActFile As String In _FileIn
                        If System.IO.File.Exists(_ActFile) Then
                            Dim _CustomersFileAttachmentNew As New CustomersFileAttachment(_ocur.Session)
                            With (_CustomersFileAttachmentNew)
                                .RowGroup1 = _CustomersFileAttachment.RowGroup1
                                .RowGroup2 = _CustomersFileAttachment.RowGroup2
                                .Customers = _ocur.GetObject(_CustomersFileAttachment.Customers)
                                .File = LoadOtherFile(_ActFile, New FileData(_ocur.Session))
                                .ShortName = .File.FileName
                                .FileCreated = GetFileCreatedDate(_ActFile)
                                .FileModified = GetFileModifiedDate(_ActFile)
                            End With
                        End If
                        _WaitFormClass.DoWork()
                    Next
                    _ocur.CommitChanges()

                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        Finally
            _Waitformclass.FinalWork()
        End Try
    End Sub
    Private Sub aDragDrop_Products_Change_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aDragDrop_Products_Change.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ChangeFileAttachment_PopUp As ChangeFileAttachment_PopUp = _onew.CreateObject(Of ChangeFileAttachment_PopUp)()
            e.View = Application.CreateDetailView(_onew, "ChangeFileAttachment_PopUp_DetailView", True, _ChangeFileAttachment_PopUp)
            e.View.Tag = "aProductsDocumentations_ChangeGroup"
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aDragDrop_Products_Change_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aDragDrop_Products_Change.Execute
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ChangeFileAttachment_PopUp As ChangeFileAttachment_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), ChangeFileAttachment_PopUp)
            If TryCast(_Selection(0), ProductsDocumentations) IsNot Nothing Then
                _WaitFormClass.InitWork(1, 1, _Selection.Count)
                For Each _ProductsDocumentationsAct As ProductsDocumentations In _Selection
                    Dim _ProductsDocumentations As ProductsDocumentations = _onew.GetObject(_ProductsDocumentationsAct)
                    If _ChangeFileAttachment_PopUp.ChangeRowGroup1 Then
                        _ProductsDocumentations.RowGroup1 = _ChangeFileAttachment_PopUp.RowGroup1
                    End If
                    If _ChangeFileAttachment_PopUp.ChangeRowGroup2 Then
                        _ProductsDocumentations.RowGroup2 = _ChangeFileAttachment_PopUp.RowGroup2
                    End If
                    If _ChangeFileAttachment_PopUp.ChangeStatus Then
                        _ProductsDocumentations.Status = _ChangeFileAttachment_PopUp.Status
                    End If
                    _WaitFormClass.DoWork()
                Next
            End If
            _onew.CommitChanges()
            View.Refresh()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        Finally
            _Waitformclass.FinalWork()
            _Selection.Clear()
        End Try
    End Sub
    Private Sub aDragDrop_CustomersDoc_Change_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aDragDrop_CustomersDoc_Change.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ChangeFileAttachment_PopUp As ChangeFileAttachment_PopUp = _onew.CreateObject(Of ChangeFileAttachment_PopUp)()
            e.View = Application.CreateDetailView(_onew, "ChangeFileAttachment_PopUp_DetailView", True, _ChangeFileAttachment_PopUp)
            e.View.Tag = "aCustomersDocFileAttachment_ChangeGroup"
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aDragDrop_CustomersDoc_Change_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aDragDrop_CustomersDoc_Change.Execute
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ChangeFileAttachment_PopUp As ChangeFileAttachment_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), ChangeFileAttachment_PopUp)
            If TryCast(_Selection(0), CustomersFileAttachment) IsNot Nothing Then
                _WaitFormClass.InitWork(1, 1, _Selection.Count)
                For Each _CustomersFileAttachmentAct As CustomersFileAttachment In _Selection
                    Dim _CustomersFileAttachment As CustomersFileAttachment = _onew.GetObject(_CustomersFileAttachmentAct)
                    If _ChangeFileAttachment_PopUp.ChangeRowGroup1 Then
                        _CustomersFileAttachment.RowGroup1 = _ChangeFileAttachment_PopUp.RowGroup1
                    End If
                    If _ChangeFileAttachment_PopUp.ChangeRowGroup2 Then
                        _CustomersFileAttachment.RowGroup2 = _ChangeFileAttachment_PopUp.RowGroup2
                    End If
                    If _ChangeFileAttachment_PopUp.ChangeStatus Then
                        _CustomersFileAttachment.Status = _ChangeFileAttachment_PopUp.Status
                    End If
                    _WaitFormClass.DoWork()
                Next
            End If
            _onew.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        Finally
            _Waitformclass.FinalWork()
            _Selection.Clear()
        End Try
    End Sub

    '------Subs as Functions-------------
    Private Function LoadOtherFile(ByVal _ActFile As String, _FileData As FileData) As FileData
        Dim _StreamFile = New System.IO.FileStream(_ActFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)
        Dim _StreamReader As New System.IO.StreamReader(_StreamFile)
        _StreamFile.Seek(0, System.IO.SeekOrigin.Begin)

        _FileData.LoadFromStream(_ActFile, _StreamFile)
        _FileData.FileName = System.IO.Path.GetFileName(_ActFile)
        _StreamReader.Close()
        _StreamFile.Close()

        Return _FileData
    End Function
    Public Function GetFileCreatedDate(_FileData As FileData) As Date
        Try
            Dim _FileName As String = System.IO.Path.GetTempPath + _FileData.FileName
            Dim _StreamFile As New System.IO.FileStream(_FileName, FileMode.Create)

            _FileData.SaveToStream(_StreamFile)
            _StreamFile.Close()

            Return File.GetCreationTime(_FileName)
        Catch ex As Exception
        Finally
            Dim _FileName As String = System.IO.Path.GetTempPath + _FileData.FileName
            If File.Exists(_FileName) Then File.Delete(_FileName)
        End Try
    End Function
    Public Function GetFileCreatedDate(_FileName As String) As Date
        Return File.GetCreationTime(_FileName)
    End Function
    Public Function GetFileModifiedDate(_FileData As FileData) As Date
        Try
            Dim _FileName As String = System.IO.Path.GetTempPath + _FileData.FileName
            Dim _StreamFile As New System.IO.FileStream(_FileName, FileMode.Create)

            _FileData.SaveToStream(_StreamFile)
            _StreamFile.Close()

            Return File.GetLastAccessTime(_FileName)
        Catch ex As Exception
        Finally
            Dim _FileName As String = System.IO.Path.GetTempPath + _FileData.FileName
            If File.Exists(_FileName) Then File.Delete(_FileName)
        End Try
    End Function
    Public Function GetFileModifiedDate(_FileName As String) As Date
        Return File.GetLastAccessTime(_FileName)
    End Function
End Class
