Imports System
Imports System.ComponentModel

Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation
Imports DevExpress.ExpressApp.ConditionalAppearance
Imports DevExpress.Persistent.Base.General
Imports System.Drawing


Public Enum eEMailMode
    eOutlook = 0
    eExchange = 1
    ePOP3 = 2
End Enum
<DefaultClassOptions()> _
<NavigationItem("HRStocks")> _
<Appearance("Disable HRPerson - UniqueObjects", AppearanceItemType.ViewItem, "1=1", TargetItems:="UniqueObjects", Enabled:=False)> _
Public Class HRPerson
    Inherits Person
    Implements IResource
    Private _Contact As Contacts 'Kapcsolat
    Private _User As User 'Program felhasználó
    Private _ShortName As String 'Rövid megnevezés
    Private _Barcode As String 'Vonalkód
    Private _Job As String 'Munkakör
    Private _Post As String 'Beosztás
    Private _UniqueObjects As UniqueObjects

    Private _EMailMode As eEMailMode
    '//Outlook settings
    Private _DefaultOutlookFolder As String
    Private _DefaultOutlookFolderEntryID As String
    Private _HRPerson_OutlookFolder As HRPerson_OutlookFolder
    '//Exchange
    Private _ExchangeHost As String

    '//POP3 SMTP
    Private _SMTPHost As String
    Private _SMTPPort As Long = 25
    Private _SMTPUserName As String
    Private _SMTPPassword As String

    Private _POP3Host As String
    Private _POP3Port As Long = 110
    Private _POP3UserName As String
    Private _POP3Password As String

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
#If MediumTrust Then
		<Persistent("Color"), Browsable(False)> _
		Public color_Renamed As Int32
#Else
    <Persistent("Color")> _
    Private color_Renamed As Int32
#End If
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        Dim _HRPerson_OutlookFolderCollection As New XPCollection(Of HRPerson_OutlookFolder)(Session, CriteriaOperator.Parse("1=1"))
        Session.Delete(_HRPerson_OutlookFolderCollection)

        If Session.IsNewObject(Me) Then
            UniqueObjects = New UniqueObjects(Session)
            UniqueObjects.Name = FullName
            UniqueObjects.HRPerson = Me
            UniqueObjects.Save()
        End If
    End Sub
    Protected Overrides Sub OnSaved()
        MyBase.OnSaved()
        Me.HRPerson_OutlookFolder = Nothing
        Dim _HRPerson_OutlookFolderCollection As New XPCollection(Of HRPerson_OutlookFolder)(Session, CriteriaOperator.Parse("1=1"))
        Session.Delete(_HRPerson_OutlookFolderCollection)
    End Sub
    '// IResources
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        color_Renamed = Color.White.ToArgb()
    End Sub
    <NonPersistent(), Browsable(False)> _
    Public ReadOnly Property Id() As Object Implements IResource.Id
        Get
            Return Oid
        End Get
    End Property
    Public Property Caption() As String Implements IResource.Caption
        Get
            Return Me.FullName
        End Get
        Set(ByVal value As String)
            'UserName = value
            OnChanged("Caption")
        End Set
    End Property
    <NonPersistent(), Browsable(False)> _
    Public ReadOnly Property OleColor() As Int32 Implements IResource.OleColor
        Get
            Return ColorTranslator.ToOle(Color.FromArgb(color_Renamed))
        End Get
    End Property
    <Association("HRPersonEvent-HRPerson", GetType(HRPersonEvent)), VisibleInListView(False), VisibleInLookupListView(False)> _
    Public ReadOnly Property Events() As XPCollection(Of HRPersonEvent)
        Get
            Return GetCollection(Of HRPersonEvent)("Events")
        End Get
    End Property
    <NonPersistent()> _
    Public Property Color() As Color
        Get
            Return Color.FromArgb(color_Renamed)
        End Get
        Set(ByVal value As Color)
            color_Renamed = value.ToArgb()
            OnChanged("Color")
        End Set
    End Property

    '// Base property
    Property Contact() As Contacts
        Get
            Return _Contact
        End Get
        Set(ByVal value As Contacts)
            SetPropertyValue("Contatcts", _Contact, value)
        End Set
    End Property
    Property User() As User
        Get
            Return _User
        End Get
        Set(ByVal value As User)
            SetPropertyValue("User", _User, value)
            If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving Then
                If _User IsNot Nothing Then
                    Me.Address1 = _User.Address1
                    Me.Address2 = _User.Address2
                    Me.Birthday = _User.Birthday
                    Me.Email = _User.Email
                    Me.FirstName = _User.FirstName
                    Me.LastName = _User.LastName
                    Me.MiddleName = _User.MiddleName
                End If
            End If
        End Set
    End Property
    Property ShortName As String
        Get
            Return _ShortName
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ShortName", _ShortName, value)
        End Set
    End Property
    Property Barcode As String
        Get
            Return _Barcode
        End Get
        Set(value As String)
            SetPropertyValue("Barcode", _Barcode, value)
        End Set
    End Property
    Property Job As String
        Get
            Return _Job
        End Get
        Set(value As String)
            SetPropertyValue("Job", _Job, value)
        End Set
    End Property
    Property Post As String
        Get
            Return _Post
        End Get
        Set(value As String)
            SetPropertyValue("Post", _Post, value)
        End Set
    End Property
    Property UniqueObjects As UniqueObjects
        Get
            Return _UniqueObjects
        End Get
        Set(value As UniqueObjects)
            SetPropertyValue("UniqueObjects", _UniqueObjects, value)
        End Set
    End Property

    Property EMailMode As eEMailMode
        Get
            Return _EMailMode
        End Get
        Set(value As eEMailMode)
            SetPropertyValue("EMailMode", _EMailMode, value)
        End Set
    End Property

    '// Outlook settings
    Property DefaultOutlookFolder As String
        Get
            Return _DefaultOutlookFolder
        End Get
        Set(value As String)
            SetPropertyValue("DefaultOutlookFolder", _DefaultOutlookFolder, value)
        End Set
    End Property
    <Size(255)> _
    Property DefaultOutlookFolderEntryID As String
        Get
            Return _DefaultOutlookFolderEntryID
        End Get
        Set(value As String)
            SetPropertyValue("DefaultOutlookFolderEntryID", _DefaultOutlookFolderEntryID, value)
        End Set
    End Property
    <NonPersistent()>
    <ImmediatePostData(True), DataSourceProperty("HRPerson_OutlookFolderCollection")>
    Property HRPerson_OutlookFolder As HRPerson_OutlookFolder
        Get
            Return _HRPerson_OutlookFolder
        End Get
        Set(value As HRPerson_OutlookFolder)

            SetPropertyValue("HRPerson_OutlookFolder", _HRPerson_OutlookFolder, value)

            If Session.IsObjectsSaving = False And Session.IsObjectsLoading = False Then
                If Me.HRPerson_OutlookFolder IsNot Nothing Then
                    Me.DefaultOutlookFolder = Me.HRPerson_OutlookFolder.FolderPath
                    Me.DefaultOutlookFolderEntryID = Me.HRPerson_OutlookFolder.EntryID
                End If
            End If
        End Set
    End Property
    '//Exchange settings

    '//POP3 SMTP settings
    Property SMTPHost As String
        Get
            Return _SMTPHost
        End Get
        Set(value As String)
            SetPropertyValue("SMTPHost", _SMTPHost, value)
        End Set
    End Property
    Property SMTPPort As Long
        Get
            Return _SMTPPort
        End Get
        Set(value As Long)
            SetPropertyValue("SMTPPort", _SMTPPort, value)
        End Set
    End Property
    Property SMTPUserName As String
        Get
            Return _SMTPUserName
        End Get
        Set(value As String)
            SetPropertyValue("SMTPUserName", _SMTPUserName, value)
        End Set
    End Property
    <PasswordPropertyText()> _
    <ValueConverter(GetType(Encryption))> _
    Property SMTPPassword As String
        Get
            Return _SMTPPassword
        End Get
        Set(value As String)
            SetPropertyValue("SMTPPassword", _SMTPPassword, value)
        End Set
    End Property

    Property POP3Host As String
        Get
            Return _POP3Host
        End Get
        Set(value As String)
            SetPropertyValue("POP3Host", _POP3Host, value)
        End Set
    End Property
    Property POP3Port As Long
        Get
            Return _POP3Port
        End Get
        Set(value As Long)
            SetPropertyValue("POP3Port", _POP3Port, value)
        End Set
    End Property
    Property POP3UserName As String
        Get
            Return _POP3UserName
        End Get
        Set(value As String)
            SetPropertyValue("POP3UserName", _POP3UserName, value)
        End Set
    End Property
    <PasswordPropertyText()> _
    <ValueConverter(GetType(Encryption))> _
    Property POP3Password As String
        Get
            Return _POP3Password
        End Get
        Set(value As String)
            SetPropertyValue("POP3Password", _POP3Password, value)
        End Set
    End Property

    '---------------ReadOnly--------------
    <VisibleInListView(False)> _
    <Association("HRPerson-HRGroups", GetType(HRGroups))> _
    ReadOnly Property HRGroups() As XPCollection(Of HRGroups)
        Get
            Return GetCollection(Of HRGroups)("HRGroups")
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property FixedAssets_Collection As XPCollection(Of FixedAssets)
        Get
            '// Kiadott tárgyieszközök kollekciója
            Return New XPCollection(Of FixedAssets)(Session, CriteriaOperator.Parse("HRPerson_Using=?", Me))
        End Get
    End Property
    <Association("HRPerson-HRPersonCostHolder", GetType(HRPersonCostHolder)), VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property HRPersonCostHolder As XPCollection(Of HRPersonCostHolder)
        Get
            Return GetCollection(Of HRPersonCostHolder)("HRPersonCostHolder")
        End Get
    End Property
    <Association("HRPerson-HRPersonCustomers", GetType(HRPersonCustomers)), VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property HRPersonCustomers As XPCollection(Of HRPersonCustomers)
        Get
            Return GetCollection(Of HRPersonCustomers)("HRPersonCustomers")
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property HRPersonTask As XPCollection(Of HRPersonTasks)
        Get
            Return New XPCollection(Of HRPersonTasks)(Session, CriteriaOperator.Parse("HRPerson=?", Me))
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property CustomersThemesHRPerson As XPCollection(Of CustomersThemesHRPerson)
        Get
            Return New XPCollection(Of CustomersThemesHRPerson)(Session, CriteriaOperator.Parse("HRPerson=?", Me))
        End Get
    End Property
    Private ReadOnly Property HRPerson_OutlookFolderCollection As XPCollection(Of HRPerson_OutlookFolder)
        Get
            Dim _HRPerson_OutlookFolderCollection As New XPCollection(Of HRPerson_OutlookFolder)(Session, CriteriaOperator.Parse("HRPerson=?", Me))
            Session.Delete(_HRPerson_OutlookFolderCollection)
            Dim _OutlookApp As New Microsoft.Office.Interop.Outlook.Application()
            Dim _OutlookNameSpace As Microsoft.Office.Interop.Outlook.NameSpace

            _OutlookNameSpace = _OutlookApp.GetNamespace("MAPI")
            _OutlookNameSpace.Logon(, , False, True)
            For Each _BaseFolder As Microsoft.Office.Interop.Outlook.Folder In _OutlookNameSpace.Folders
                OutlookFolderList(_BaseFolder, _HRPerson_OutlookFolderCollection)
            Next
            '_HRPerson_OutlookFolderCollection.Criteria = CriteriaOperator.Parse("HRPerson=?", Me)
            Return _HRPerson_OutlookFolderCollection
        End Get
    End Property
    Private Sub OutlookFolderList(_OutlookFolder As Microsoft.Office.Interop.Outlook.Folder, HRPerson_OutlookFolderCollection As XPCollection(Of [Module].HRPerson_OutlookFolder))
        If _OutlookFolder.Folders.Count > 0 Then
            For _ik = 1 To _OutlookFolder.Folders.Count
                OutlookFolderList(_OutlookFolder.Folders(_ik), HRPerson_OutlookFolderCollection)
            Next
        Else
            Dim _HRPerson_OutlookFolder As New HRPerson_OutlookFolder(Session)
            _HRPerson_OutlookFolder.EntryID = _OutlookFolder.EntryID.ToString
            _HRPerson_OutlookFolder.FolderPath = _OutlookFolder.FolderPath
            _HRPerson_OutlookFolder.HRPerson = Me

            HRPerson_OutlookFolderCollection.Add(_HRPerson_OutlookFolder)
        End If

    End Sub
End Class
