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.Data.Filtering
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.Xpo

Public Class CRM_LeadsVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Private Sub aCRM_Leads_CreateMail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCRM_Leads_CreateMail.Execute
        Try
            Dim _CRM_Leads As CRM_Leads = TryCast(e.CurrentObject, CRM_Leads)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            If _CRM_Leads.EMail = "" Then Throw New UserFriendlyException("Nincs beállítva e-mail cím!")
            Dim _OutlookEMail As OutlookEMail = _ocur.CreateObject(Of OutlookEMail)()

            _OutlookEMail.CRM_Leads = _CRM_Leads
            _OutlookEMail.Recipients = _CRM_Leads.EMail
            _OutlookEMail.Subject = _CRM_Leads.LeadSource
            _OutlookEMail.eCRMEstate_Leads = eCRMEstate_Leads.eCRM

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_ocur, "OutlookEMail_DetailView", False, _OutlookEMail)
            View.Refresh()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aCRM_Leads_ReadMail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCRM_Leads_ReadMail.Execute
        Dim _CRM_Leads_Activity As CRM_Leads_Activity = TryCast(e.SelectedObjects(0), CRM_Leads_Activity)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        If _CRM_Leads_Activity IsNot Nothing Then
            '--Email megkeresése és új benyitása
            If _CRM_Leads_Activity.Email_Oid = "" Then
                Throw New UserFriendlyException("Nincs kapcsolódó e-mail üzenet!")
            Else
                Dim _OutlookEMail As OutlookEMail = _ocur.FindObject(Of OutlookEMail)(CriteriaOperator.Parse("Oid=?", _CRM_Leads_Activity.Email_Oid))
                Dim _OutlookEMailIn As OutlookEMailIn = _ocur.FindObject(Of OutlookEMailIn)(CriteriaOperator.Parse("Oid=?", _CRM_Leads_Activity.Email_Oid))
                If _OutlookEMail IsNot Nothing Then
                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_ocur, "OutlookEMail_DetailView", False, _OutlookEMail)
                Else
                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_ocur, "OutlookEMailIn_DetailView", False, _OutlookEMailIn)
                End If
                View.Refresh()
            End If

        End If
    End Sub

    Private Sub aCRM_LeadsDocumentation_OpenBusinessDirectory_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aCRM_LeadsDocumentation_OpenBusinessDirectory.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _CRM_Opportunities As CRM_Opportunities = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, CRM_Opportunities)
            Dim _BusinessDirectoryList As New ArrayList
            Dim _BusinessDirectory_CollectionSource As New CollectionSource(_onew, GetType(BusinessDirectory))

            If _CRM_Opportunities.MasterBusinessDirectory IsNot Nothing Then
                If _CRM_Opportunities.MasterBusinessDirectory.Children.Count > 0 Then
                    _BusinessDirectoryList.Add(_CRM_Opportunities.MasterBusinessDirectory)
                    RecursiveBusinessDirectory(_BusinessDirectoryList, _CRM_Opportunities.MasterBusinessDirectory.NestedBusinessDirectory)
                Else
                    If _CRM_Opportunities.MasterBusinessDirectory.IsFolder Then _BusinessDirectoryList.Add(_CRM_Opportunities.MasterBusinessDirectory)
                End If
            End If

            Dim _Filter As String = "Oid in("
            For Each _BusinessDirectory As BusinessDirectory In _BusinessDirectoryList
                _Filter += "'" + _BusinessDirectory.Oid.ToString + "',"
            Next
            _Filter = _Filter.Remove(_Filter.Length - 1, 1)
            _Filter += ")"

            _BusinessDirectory_CollectionSource.BeginUpdateCriteria()
            _BusinessDirectory_CollectionSource.Criteria.Clear()
            If _Filter.Length > 8 Then
                _BusinessDirectory_CollectionSource.Criteria.Add("1", CriteriaOperator.Parse(_Filter))
            Else
                _BusinessDirectory_CollectionSource.Criteria.Add("1", CriteriaOperator.Parse("1=2"))
            End If
            _BusinessDirectory_CollectionSource.EndUpdateCriteria()

            e.View = Application.CreateListView("BusinessDirectory_ListView", _BusinessDirectory_CollectionSource, False)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))

        End Try
    End Sub
    Private Sub aCRM_LeadsDocumentation_OpenBusinessDirectory_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aCRM_LeadsDocumentation_OpenBusinessDirectory.Execute
        Dim _BusinessDirectory As BusinessDirectory = TryCast(e.PopupWindow.View.SelectedObjects(0), BusinessDirectory)
        If Not _BusinessDirectory.IsFolder Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _RichEditControl_New As New RichEditControl

            _RichEditControl_New.LoadDocument(GetFile(_BusinessDirectory.FileData))

            Dim _OpenRTFDocument_PopUp As New OpenRTFDocument_PopUp(_onew.Session)
            _OpenRTFDocument_PopUp.BusinessDirectory = _onew.GetObject(_BusinessDirectory)
            _OpenRTFDocument_PopUp.RTFText = _RichEditControl_New.RtfText
            _OpenRTFDocument_PopUp.CRM_Opportunities = _onew.GetObject(TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, CRM_Opportunities))
            DeleteTempFile(_BusinessDirectory.FileData)

            Dim _ShowViewParameters As New ShowViewParameters
            Dim _ShowViewSource As New ShowViewSource(Frame, Nothing)

            _ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "OpenRTFDocument_PopUp_DetailView", True, _OpenRTFDocument_PopUp)
            _ShowViewParameters.NewWindowTarget = NewWindowTarget.MdiChild
            _ShowViewParameters.TargetWindow = TargetWindow.NewWindow
            Application.ShowViewStrategy.ShowView(_ShowViewParameters, _ShowViewSource)
        End If
    End Sub

#Region "Other Functions and Subs"
    Private Sub RecursiveBusinessDirectory(_BusinessDirectoryList As ArrayList, _NestedBusinessDirectory As XPCollection(Of BusinessDirectory))
        For Each _BusinessDirectory As BusinessDirectory In _NestedBusinessDirectory
            If _BusinessDirectory.Children.Count > 0 Then
                _BusinessDirectoryList.Add(_BusinessDirectory)
                RecursiveBusinessDirectory(_BusinessDirectoryList, _BusinessDirectory.NestedBusinessDirectory)
            Else
                _BusinessDirectoryList.Add(_BusinessDirectory)
            End If
        Next
    End Sub
    Private Function GetFile(_DocumentFile As DevExpress.Persistent.BaseImpl.FileData) As String
        Dim _FilePath As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), _DocumentFile.FileName)
        Dim _Stream As System.IO.Stream = New System.IO.FileStream(_FilePath, System.IO.FileMode.Create)
        _DocumentFile.SaveToStream(_Stream)
        _Stream.Close()

        Return _FilePath
    End Function
    Private Sub DeleteTempFile(_FileData As DevExpress.Persistent.BaseImpl.FileData)
        Dim _FilePath As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), _FileData.FileName)
        If IO.File.Exists(_FilePath) Then IO.File.Delete(_FilePath)
    End Sub
#End Region

   

End Class
