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 Microsoft.Office.Interop
Imports DevExpress.ExpressApp.Win.Editors
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Editors

Public Class OutlookReminder_VC
	Inherits DevExpress.ExpressApp.ViewController

	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 = "OutlookReminder_DetailView" Then
            AddHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf OutlookReminder_DetailView_Save
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf OutlookReminder_DetailView_Save
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf OutlookReminder_DetailView_Save
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "OutlookReminder_DetailView" Then
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf OutlookReminder_DetailView_Save
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf OutlookReminder_DetailView_Save
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf OutlookReminder_DetailView_Save
        End If
    End Sub
    Private Sub OutlookReminder_DetailView_Save()
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OutlookApp As New Outlook.Application()
        Dim _OutlookNameSpace As Outlook.NameSpace = _OutlookApp.GetNamespace("MAPI")

        Dim _OutlookReminder As OutlookReminder = TryCast(View.SelectedObjects(0), OutlookReminder)
        Dim _NewTask As Outlook.TaskItem = Nothing

        If Not String.IsNullOrEmpty(_OutlookReminder.TaskEntryID) Then
            Try
                _NewTask = _OutlookNameSpace.GetItemFromID(_OutlookReminder.TaskEntryID)
            Catch ex As Exception
            End Try
        End If

        If _NewTask Is Nothing Then _NewTask = _OutlookApp.CreateItem(Outlook.OlItemType.olTaskItem)

        _OutlookNameSpace.Logon(, , True, True)
        _NewTask.ReminderSet = True
        _NewTask.Body = _OutlookReminder.Body
        _NewTask.DueDate = New DateTime(_OutlookReminder.DueDate.Year, _OutlookReminder.DueDate.Month, _OutlookReminder.DueDate.Day, _OutlookReminder.Hours, _OutlookReminder.Minutes, 0)
        _NewTask.ReminderTime = _OutlookReminder.ReminderTimeDay
        _NewTask.Subject = _OutlookReminder.Subject
        _NewTask.UnRead = True
        _NewTask.Save()

        If String.IsNullOrEmpty(_OutlookReminder.TaskEntryID) Then _OutlookReminder.TaskEntryID = _NewTask.EntryID

        _ocur.CommitChanges()
    End Sub

End Class
