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 Microsoft.Office.Interop
Imports System.IO
Imports DevExpress.ExpressApp.Utils
Imports System.Runtime.InteropServices
Imports Microsoft.Office

Public Class DragAndDropCRMOutlookCRM_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 _DoDragGridViewOutlookEMailIn As DevExpress.XtraGrid.Views.Grid.GridView
    Private _DoDragGridControlOutlookEMailIn As DevExpress.XtraGrid.GridControl

    Private _ToDragGridControlCRM_Opportunities As DevExpress.XtraGrid.GridControl
    Private _ToDragGridViewCRM_Opportunities As DevExpress.XtraGrid.Views.Grid.GridView
    Private _ToDragGridControlCRM_LeadsDocumentation As DevExpress.XtraGrid.GridControl
    Private _ToDragGridViewCRM_LeadsDocumentation 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 = "" Then
        End If
        If View.Id = "OutlookEMailIn_ListView_CRM_Outlook" Then
            Dim _View As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            _DoDragGridViewOutlookEMailIn = TryCast(_View.Editor, DevExpress.ExpressApp.Win.Editors.GridListEditor).GridView
            _DoDragGridViewOutlookEMailIn.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseUp
            _DoDragGridControlOutlookEMailIn = _DoDragGridViewOutlookEMailIn.GridControl

            AddHandler _DoDragGridViewOutlookEMailIn.MouseDown, AddressOf _DoDragGridViewOutlookEMailIn_MouseDown
            AddHandler _DoDragGridViewOutlookEMailIn.MouseMove, AddressOf _DoDragGridViewOutlookEMailIn_MouseMove
            AddHandler View.SelectionChanged, AddressOf OutlookEMailIn_ListView_SelectionChanged
        End If
        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 = "CRM_Opportunities_ListView_CRM_Outlook" Then
            Dim _View As DevExpress.ExpressApp.ListView = TryCast(View, DevExpress.ExpressApp.ListView)
            _ToDragGridViewCRM_Opportunities = TryCast(_View.Editor, DevExpress.ExpressApp.Win.Editors.GridListEditor).GridView
            _ToDragGridControlCRM_Opportunities = _ToDragGridViewCRM_Opportunities.GridControl

            AddHandler _ToDragGridControlCRM_Opportunities.DragOver, AddressOf _ToDragGridControlCRM_Opportunities_DragOver
            AddHandler _ToDragGridControlCRM_Opportunities.DragDrop, AddressOf _ToDragGridControlCRM_Opportunities_DragDrop
            AddHandler _ToDragGridViewCRM_Opportunities.MouseMove, AddressOf _ToDragGridControlCRM_Opportunities_MouseMove

            AddHandler View.SelectionChanged, AddressOf CRM_Opportunities_ListView_CRM_Outlook_SelectionChanged
        End If
        If View.Id = "CRM_Leads_CRM_LeadsDocumentation_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).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)
            _ToDragGridViewCRM_LeadsDocumentation = TryCast(_View.Editor, DevExpress.ExpressApp.Win.Editors.GridListEditor).GridView
            _ToDragGridControlCRM_LeadsDocumentation = _ToDragGridViewCRM_LeadsDocumentation.GridControl

            AddHandler _ToDragGridControlCRM_LeadsDocumentation.DragOver, AddressOf ToDragGridControlControlCRM_LeadsDocumentation_DragOver
            AddHandler _ToDragGridControlCRM_LeadsDocumentation.DragDrop, AddressOf ToDragGridControlCRM_LeadsDocumentation_DragDrop
            AddHandler _ToDragGridViewCRM_LeadsDocumentation.MouseMove, AddressOf ToDragGridView_MouseMove
        End If
    End Sub
    Private Sub _DoDragGridViewOutlookEMailIn_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 _DoDragGridViewOutlookEMailIn_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_ListView_SelectionChanged(sender As Object, e As EventArgs)
        _Selection.Clear()
        _Selection.AddRange(View.SelectedObjects)
    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 _ToDragGridControlCRM_Opportunities_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 _ToDragGridViewCRM_Opportunities.IsDataRow(_GridHitInfo.RowHandle) Then _ToDragGridViewCRM_Opportunities.CollapseAllGroups()
        If _ToDragGridViewCRM_Opportunities.IsGroupRow(_GridHitInfo.RowHandle) Then _ToDragGridViewCRM_Opportunities.ExpandGroupRow(_GridHitInfo.RowHandle)

        If _ToDragGridViewCRM_Opportunities.IsDataRow(_GridHitInfo.RowHandle) Then
            _ToDragGridViewCRM_Opportunities.ClearSelection()
            _ToDragGridViewCRM_Opportunities.SelectRow(_GridHitInfo.RowHandle)
        End If
    End Sub
    Private Sub _ToDragGridControlCRM_Opportunities_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 _CRM_Opportunities As CRM_Opportunities = TryCast(_Grid.MainView.GetRow(_GridHitInfo.RowHandle), CRM_Opportunities)
            If _CRM_Opportunities Is Nothing Then Return

            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace

            Dim _OutlookEMailInSelection As ArrayList = e.Data.GetData(GetType(ArrayList))
            If _OutlookEMailInSelection IsNot Nothing Then
                _WaitFormClass.InitWork(0, 1, _OutlookEMailInSelection.Count)
                If TryCast(_OutlookEMailInSelection(0), OutlookEMailIn) IsNot Nothing Then

                    For Each _OutlookEMailIn As OutlookEMailIn In _OutlookEMailInSelection
                        Dim _CRM_Leads_Activity As New CRM_Leads_Activity(_ocur.Session)
                        _OutlookEMailIn = _ocur.GetObject(_OutlookEMailIn)
                        _CRM_Leads_Activity.CRM_Opportunities = _CRM_Opportunities
                        _CRM_Leads_Activity.CRM_Leads = _CRM_Opportunities.CRM_Leads
                        _CRM_Leads_Activity.EventType = eEventType.eEMailIn
                        _CRM_Leads_Activity.Email_Oid = _OutlookEMailIn.Oid.ToString

                        _OutlookEMailIn.Qualified = True
                        _OutlookEMailIn.IsReaded = True
                        _WaitFormClass.DoWork
                    Next
                    _ocur.CommitChanges()

                    _Waitformclass.FinalWork
                ElseIf TryCast(_OutlookEMailInSelection(0), OutlookEMailAttachments) IsNot Nothing Then
                    If _CRM_Opportunities.CRM_Leads Is Nothing Then Throw New Exception("*Nem tartozik Potenciáli ügyfél a CRM ügyhöz!")
                    _Selection.Clear()

                    For Each _OutlookEMailAttachments As OutlookEMailAttachments In _OutlookEMailInSelection
                        Dim _CRM_LeadsDocumentation As New CRM_LeadsDocumentation(_ocur.Session)
                        With (_CRM_LeadsDocumentation)
                            .CRM_Leads = _ocur.GetObject(_CRM_Opportunities.CRM_Leads)
                            .File = _ocur.GetObject(_OutlookEMailAttachments.DocumentFileData.File)
                        End With
                        _Selection.Add(_CRM_LeadsDocumentation)
                        _WaitFormClass.DoWork
                    Next
                    _ocur.CommitChanges()

                    _Waitformclass.FinalWork

                    CType(New PopupWindowShowActionHelper(Frame.GetController(Of DragAndDropCRMOutlookCRM_VC).aDragDrop_CRM_Change), PopupWindowShowActionHelper).ShowPopupWindow()
                Else
                    Throw New Exception("*Váratlan hiba történt a folyamat megszakadt!")
                End If
            ElseIf e.Data.GetDataPresent(DataFormats.FileDrop) Then
                Dim _FileIn As Object = e.Data.GetData(DataFormats.FileDrop) 'File
                If _FileIn IsNot Nothing Then
                    _Selection.Clear()
                    _WaitFormClass.InitWork(0, 1, TryCast(_FileIn, String()).Length)
                    For Each _ActFile As String In _FileIn
                        If System.IO.File.Exists(_ActFile) Then
                            Dim _CRM_LeadsDocumentation As New CRM_LeadsDocumentation(_ocur.Session)
                            With (_CRM_LeadsDocumentation)
                                .CRM_Leads = _ocur.GetObject(_CRM_Opportunities.CRM_Leads)
                                .FileCreated = GetFileCreatedDate(_ActFile)
                                .FileModified = GetFileModifiedDate(_ActFile)
                                .File = LoadOtherFile(_ActFile, New FileData(_ocur.Session))
                                .ShortName = .File.FileName
                            End With
                            _Selection.Add(_CRM_LeadsDocumentation)
                        End If
                        _WaitFormClass.DoWork
                    Next
                    _ocur.CommitChanges()

                    _Waitformclass.FinalWork
                    CType(New PopupWindowShowActionHelper(Frame.GetController(Of DragAndDropCRMOutlookCRM_VC).aDragDrop_CRM_Change), PopupWindowShowActionHelper).ShowPopupWindow()
                End If
            ElseIf e.Data.GetDataPresent("FileGroupDescriptor") Then 'Email
                Dim _OL As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application
                _WaitFormClass.InitWork(0, 1, _OL.ActiveExplorer.Selection.Count)
                If _OL.ActiveExplorer.Selection.Count > 0 Then
                    For ik As Long = 1 To _OL.ActiveExplorer.Selection.Count
                        Dim _FileIn As Object = _OL.ActiveExplorer.Selection(ik)
                        Dim _MailItem As Microsoft.Office.Interop.Outlook.MailItem = TryCast(_FileIn, Microsoft.Office.Interop.Outlook.MailItem)
                        If _MailItem IsNot Nothing Then
                            Dim _FileName As String = ""
                            Dim _Subject As String = ""
                            If _MailItem.Subject IsNot Nothing Then
                                _Subject = _MailItem.Subject.Replace(":", "")
                                _Subject = _Subject.Replace("/", "-")
                            Else
                                _Subject = "Nincs tárgy"
                            End If
                            _FileName = System.IO.Path.GetTempPath + _Subject + ".msg"
                            _FileName = _FileName.Replace("\\", "\")
                            _MailItem.SaveAs(_FileName, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG)

                            If System.IO.File.Exists(_FileName) Then
                                Dim _CRM_LeadsDocumentation As New CRM_LeadsDocumentation(_ocur.Session)
                                With (_CRM_LeadsDocumentation)
                                    .CRM_Leads = _ocur.GetObject(_CRM_Opportunities.CRM_Leads)
                                    .FileCreated = GetFileCreatedDate(_FileName)
                                    .FileModified = GetFileModifiedDate(_FileName)
                                    .File = LoadOtherFile(_FileName, New FileData(_ocur.Session))
                                    .ShortName = .File.FileName
                                End With
                                System.IO.File.Delete(_FileName)
                            End If
                        End If
                        _WaitFormClass.DoWork
                    Next
                    _ocur.CommitChanges()
                    _Waitformclass.FinalWork
                    SwitchOutlookPanes(_OL)
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RefreshController).RefreshAction.DoExecute()
        End Try
    End Sub
    Private Sub _ToDragGridControlCRM_Opportunities_MouseMove(sender As Object, e As MouseEventArgs)
        If _AllowSelect_CRM_Opportunities Then
            Dim view As XafGridView = TryCast(sender, XafGridView)
            view.ClearSelection()
            Dim hitInfo As GridHitInfo = view.CalcHitInfo(New Point(e.X, e.Y))
            view.SelectRow(hitInfo.RowHandle)
        End If
    End Sub
    Private Sub CRM_Opportunities_ListView_CRM_Outlook_SelectionChanged(sender As Object, e As EventArgs)
        ' frissíteni kell a masterdetail view-et! na de hogy a pékbe?
    End Sub
    Private Sub ToDragGridControlControlCRM_LeadsDocumentation_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 TryCast(_ListView.CollectionSource.Collection, ProxyCollection).Count > 0 Then
            If _ToDragGridViewCRM_LeadsDocumentation.IsGroupRow(_GridHitInfo.RowHandle) Then _ToDragGridViewCRM_LeadsDocumentation.ExpandGroupRow(_GridHitInfo.RowHandle)
            _ActSelection = Nothing

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

            ElseIf _ToDragGridViewCRM_LeadsDocumentation.IsDataRow(_GridHitInfo.RowHandle) Then
                e.Effect = DragDropEffects.Copy
                _ToDragGridViewCRM_LeadsDocumentation.ClearSelection()
                _ToDragGridViewCRM_LeadsDocumentation.SelectRow(_GridHitInfo.RowHandle)
                _ActSelection = _ToDragGridViewCRM_LeadsDocumentation.GetRow(_ToDragGridViewCRM_LeadsDocumentation.GetSelectedRows(0))
                _ToDragGridViewCRM_LeadsDocumentation.Tag = ""
            End If
        Else
            e.Effect = DragDropEffects.Copy
        End If

       

    End Sub
    Private Sub ToDragGridControlCRM_LeadsDocumentation_DragDrop(sender As Object, e As DragEventArgs)
        Try
            If _ToDragGridViewCRM_LeadsDocumentation.Tag = "NODROPCRM" Then
                _ToDragGridViewCRM_LeadsDocumentation.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 _CRM_LeadsDocumentation As CRM_LeadsDocumentation = Nothing
            Dim _CRM_Opportunities As CRM_Opportunities = Nothing
            Dim _NoListViewElem As Boolean = False
            If _ActSelection IsNot Nothing Then
                _CRM_LeadsDocumentation = TryCast(_ActSelection, CRM_LeadsDocumentation)
            ElseIf TryCast(_ListView.CollectionSource.Collection, ProxyCollection).Count > 0 Then
                If Not _GridHitInfo.InRow OrElse Not _GridHitInfo.InRowCell Then Return
                If Not DirectCast(_Grid.MainView, GridView).IsDataRow(_GridHitInfo.RowHandle) Then Return
                _CRM_LeadsDocumentation = TryCast(_Grid.MainView.GetRow(_GridHitInfo.RowHandle), CRM_LeadsDocumentation)
            Else
                _NoListViewElem = True
            End If

            If _CRM_LeadsDocumentation Is Nothing And _NoListViewElem = False Then Return

            If _CRM_LeadsDocumentation Is Nothing Then
                _CRM_Opportunities = TryCast(TryCast(_ListView.CollectionSource, PropertyCollectionSource).MasterObject, CRM_Opportunities)
            End If

            If _CRM_LeadsDocumentation Is Nothing And _CRM_Opportunities Is Nothing Then Return

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

            Dim _OutlookEMailAttachments_Collection As ArrayList = e.Data.GetData(GetType(ArrayList))
            If _OutlookEMailAttachments_Collection IsNot Nothing Then
                _WaitFormClass.InitWork(0, 1, _OutlookEMailAttachments_Collection.Count)
                If TryCast(_OutlookEMailAttachments_Collection(0), OutlookEMailAttachments) IsNot Nothing Then
                    For Each _OutlookEMailAttachments As OutlookEMailAttachments In _OutlookEMailAttachments_Collection
                        Dim _CRM_LeadsDocumentation_New As New CRM_LeadsDocumentation(_ocur.Session)
                        With _CRM_LeadsDocumentation_New
                            If _CRM_LeadsDocumentation IsNot Nothing Then
                                .RowGroup1 = _CRM_LeadsDocumentation.RowGroup1
                                .RowGroup2 = _CRM_LeadsDocumentation.RowGroup2
                                .CRM_Leads = _ocur.GetObjectByKey(Of CRM_Leads)(_CRM_LeadsDocumentation.CRM_Leads.Oid)
                            Else
                                .RowGroup1 = ""
                                .RowGroup2 = ""
                                .CRM_Leads = _ocur.GetObjectByKey(Of CRM_Leads)(_CRM_Opportunities.CRM_Leads.Oid)
                            End If
                            .Status = eProductsDocumentationStatus.ePublic
                            .File = _ocur.GetObjectByKey(Of FileData)(_OutlookEMailAttachments.DocumentFileData.File.Oid)
                            .File.FileName = _OutlookEMailAttachments.DocumentFileData.File.FileName
                        End With
                        _WaitFormClass.DoWork
                    Next
                    _ocur.CommitChanges()
                ElseIf TryCast(_OutlookEMailAttachments_Collection(0), OutlookEMailIn) IsNot Nothing Then
                    Dim _OutlookApp As New Outlook.Application()
                    Dim _OutlookNameSpace As Outlook.NameSpace
                    _OutlookNameSpace = _OutlookApp.GetNamespace("MAPI")
                    _OutlookNameSpace.Logon(, , False, True)

                    For Each _OutlookEMailIn As OutlookEMailIn In _OutlookEMailAttachments_Collection
                        Dim _CRM_LeadsDocumentation_New As New CRM_LeadsDocumentation(_ocur.Session)
                        With _CRM_LeadsDocumentation_New
                            If _CRM_LeadsDocumentation IsNot Nothing Then
                                .RowGroup1 = _CRM_LeadsDocumentation.RowGroup1
                                .RowGroup2 = _CRM_LeadsDocumentation.RowGroup2
                                .CRM_Leads = _ocur.GetObjectByKey(Of CRM_Leads)(_CRM_LeadsDocumentation.CRM_Leads.Oid)
                            Else
                                .RowGroup1 = ""
                                .RowGroup2 = ""
                                .CRM_Leads = _ocur.GetObjectByKey(Of CRM_Leads)(_CRM_Opportunities.CRM_Leads.Oid)
                            End If

                            Dim _Mail As Outlook.MailItem = TryCast(_OutlookNameSpace.GetItemFromID(_OutlookEMailIn.EntryID), Outlook.MailItem)
                            _Mail.SaveAs(Path.GetTempPath() + "EmailMessage.msg")

                            Dim _fs As FileStream
                            _fs = New FileStream(Path.GetTempPath() + "EmailMessage.msg", FileMode.Open)
                            .File = New FileData(_ocur.Session)
                            .File.LoadFromStream("EmailMessage.msg", _fs)
                            .File.FileName = "E-mail üzenet"
                            _fs.Close()
                            If System.IO.File.Exists(Path.GetTempPath() + "EmailMessage.msg") Then
                                System.IO.File.Delete(Path.GetTempPath() + "EmailMessage.msg")
                            End If
                        End With
                        _WaitFormClass.DoWork
                    Next
                    _ocur.CommitChanges()
                End If
            ElseIf e.Data.GetDataPresent(DataFormats.FileDrop) Then
                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 _CRM_LeadsDocumentation_New As New CRM_LeadsDocumentation(_ocur.Session)
                            With _CRM_LeadsDocumentation_New
                                If _CRM_LeadsDocumentation IsNot Nothing Then
                                    .RowGroup1 = _CRM_LeadsDocumentation.RowGroup1
                                    .RowGroup2 = _CRM_LeadsDocumentation.RowGroup2
                                    .CRM_Leads = _ocur.GetObjectByKey(Of CRM_Leads)(_CRM_LeadsDocumentation.CRM_Leads.Oid)
                                Else
                                    .RowGroup1 = ""
                                    .RowGroup2 = ""
                                    .CRM_Leads = _ocur.GetObjectByKey(Of CRM_Leads)(_CRM_Opportunities.CRM_Leads.Oid)
                                End If
                                .Status = eProductsDocumentationStatus.ePublic
                                .FileCreated = GetFileCreatedDate(_ActFile)
                                .FileModified = GetFileModifiedDate(_ActFile)
                                .File = LoadOtherFile(_ActFile, New FileData(_ocur.Session))
                            End With
                        End If
                        _WaitFormClass.DoWork
                    Next
                    _ocur.CommitChanges()
                End If
            ElseIf e.Data.GetDataPresent("FileGroupDescriptor") Then 'Email
                Dim _OL As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application
                _WaitFormClass.InitWork(0, 1, _OL.ActiveExplorer.Selection.Count)
                If _OL.ActiveExplorer.Selection.Count > 0 Then
                    For ik As Long = 1 To _OL.ActiveExplorer.Selection.Count
                        Dim _FileIn As Object = _OL.ActiveExplorer.Selection(ik)
                        Dim _MailItem As Microsoft.Office.Interop.Outlook.MailItem = TryCast(_FileIn, Microsoft.Office.Interop.Outlook.MailItem)
                        If _MailItem IsNot Nothing Then
                            Dim _FileName As String = ""
                            Dim _Subject As String = ""
                            If _MailItem.Subject IsNot Nothing Then
                                _Subject = _MailItem.Subject.Replace(":", "")
                                _Subject = _Subject.Replace("/", "-")
                            Else
                                _Subject = "Nincs tárgy"
                            End If
                            _FileName = System.IO.Path.GetTempPath + _Subject + ".msg"
                            _FileName = _FileName.Replace("\\", "\")
                            _MailItem.SaveAs(_FileName, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG)

                            If System.IO.File.Exists(_FileName) Then
                                Dim _CRM_LeadsDocumentation_New As New CRM_LeadsDocumentation(_ocur.Session)
                                With _CRM_LeadsDocumentation_New
                                    If _CRM_LeadsDocumentation IsNot Nothing Then
                                        .RowGroup1 = _CRM_LeadsDocumentation.RowGroup1
                                        .RowGroup2 = _CRM_LeadsDocumentation.RowGroup2
                                        .CRM_Leads = _ocur.GetObjectByKey(Of CRM_Leads)(_CRM_LeadsDocumentation.CRM_Leads.Oid)
                                    Else
                                        .RowGroup1 = ""
                                        .RowGroup2 = ""
                                        .CRM_Leads = _ocur.GetObjectByKey(Of CRM_Leads)(_CRM_Opportunities.CRM_Leads.Oid)
                                    End If
                                    .Status = eProductsDocumentationStatus.ePublic
                                    .FileCreated = GetFileCreatedDate(_FileName)
                                    .FileModified = GetFileModifiedDate(_FileName)
                                    .File = LoadOtherFile(_FileName, New FileData(_ocur.Session))
                                End With
                                System.IO.File.Delete(_FileName)
                            End If
                        End If
                        _WaitFormClass.DoWork
                    Next
                    _ocur.CommitChanges()
                    _Waitformclass.FinalWork
                    SwitchOutlookPanes(_OL)
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            _Waitformclass.FinalWork
        End Try
    End Sub
    Private Sub ToDragGridView_MouseMove(sender As Object, e As MouseEventArgs)
        If _AllowSelect_CRM_Opportunities Then
            Dim view As XafGridView = TryCast(sender, XafGridView)
            view.ClearSelection()
            Dim hitInfo As GridHitInfo = view.CalcHitInfo(New Point(e.X, e.Y))
            view.SelectRow(hitInfo.RowHandle)
        End If
    End Sub
    Private Sub aDragDrop_CRM_Change_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aDragDrop_CRM_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 = "aCRM_LeadsDocumentation_ChangeGroup"
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aDragDrop_CRM_Change_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aDragDrop_CRM_Change.Execute
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ChangeFileAttachment_PopUp As ChangeFileAttachment_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), ChangeFileAttachment_PopUp)
            Dim _CRM_Opportunities As CRM_Opportunities = _onew.GetObject(TryCast(View.SelectedObjects(0), CRM_Opportunities))
            _WaitFormClass.InitWork(1, 1, _Selection.Count)
            For Each _CRM_LeadsDocumentation_Select As CRM_LeadsDocumentation In _Selection
                Dim _CRM_LeadsDocumentation As CRM_LeadsDocumentation = _onew.GetObject(_CRM_LeadsDocumentation_Select)
                If _ChangeFileAttachment_PopUp.ChangeRowGroup1 Then
                    _CRM_LeadsDocumentation.RowGroup1 = _ChangeFileAttachment_PopUp.RowGroup1
                End If
                If _ChangeFileAttachment_PopUp.ChangeRowGroup2 Then
                    _CRM_LeadsDocumentation.RowGroup2 = _ChangeFileAttachment_PopUp.RowGroup2
                End If
                If _ChangeFileAttachment_PopUp.ChangeStatus Then
                    _CRM_LeadsDocumentation.Status = _ChangeFileAttachment_PopUp.Status
                End If
                _WaitFormClass.DoWork
            Next
            _onew.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "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
    Private Function GetFileCreatedDate(_ActFile As String) As Date
        Return System.IO.File.GetCreationTime(_ActFile)
    End Function
    Private Function GetFileModifiedDate(_ActFile As String) As Date
        Return System.IO.File.GetLastAccessTime(_ActFile)
    End Function
    
    Public Sub SwitchOutlookPanes(_OL As Microsoft.Office.Interop.Outlook.Application)
        Dim _Explorer As Microsoft.Office.Interop.Outlook.Explorer = _OL.ActiveExplorer
        Try
            If _OL IsNot Nothing And _Explorer IsNot Nothing Then
                Dim nMAPIFOlder As Interop.Outlook.MAPIFolder = _Explorer.CurrentFolder
                Dim _NameSpace As Microsoft.Office.Interop.Outlook.NameSpace = _OL.GetNamespace("MAPI")
                If _NameSpace.Folders(1) IsNot Nothing Then
                    If _NameSpace.Folders(1).Folders.Count > 0 Then
                        If _NameSpace.Folders(1).Folders(_NameSpace.Folders(1).Folders.Count) IsNot Nothing Then
                            If _NameSpace.Folders(1).Folders(_NameSpace.Folders(1).Folders.Count).Name <> nMAPIFOlder.Name Then
                                _Explorer.CurrentFolder = _NameSpace.Folders(1).Folders(_NameSpace.Folders(1).Folders.Count)
                            Else
                                _Explorer.CurrentFolder = _NameSpace.Folders(1).Folders(1)
                            End If
                        End If
                    End If
                End If
                System.Threading.Thread.Sleep(1000)
                _Explorer.CurrentFolder = nMAPIFOlder
                System.Threading.Thread.Sleep(1000)
            End If
        Catch ex As System.Exception
        Finally
            Marshal.ReleaseComObject(_Explorer)
            Marshal.ReleaseComObject(_OL)
        End Try
    End Sub

End Class
