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.ExpressApp.Utils

<DefaultClassOptions()> _
<NavigationItem("Support")> _
<DefaultProperty("DisplayName")> _
<ImageName("document_down")> _
<Appearance("Hide description in question", AppearanceItemType:="ViewItem", TargetItems:="Description", Visibility:=False, Criteria:="DocumentNumber<>''", Context:="Any")> _
<Appearance("Disable properties", AppearanceItemType:="ViewItem", _
    TargetItems:="QuestionStatus;QuestionClosed;IssueType;DocumentNumber;Subject;Attachment;User;ObjectCreated;UserCreated", _
    Enabled:=False, Criteria:="DocumentNumber<>''", Context:="Any")> _
<Appearance("Font appearance Question", AppearanceItemType:="ViewItem", Criteria:="QuestionStatus in ('eActive','eReactivated')", Context:="ListView", TargetItems:="*", FontStyle:=Drawing.FontStyle.Bold)> _
<Appearance("Question - Hide QuestionInProgressState, when QuestionStatus<>eInProgress (8)", AppearanceItemType:="ViewItem", TargetItems:="QuestionInProgressState", Visibility:=Editors.ViewItemVisibility.Hide, Criteria:="QuestionStatus<>8", Context:="Any")> _
Public Class Question
    Inherits BaseObject
    Private _QuestionStatus As eQuestionStatus
    Private _QuestionClosed As eQuestionClosed
    Private _IssueType As eIssueType
    Private _DocumentNumber As String
    Private _Subject As String
    Private _Attachment As FileData
    Private _Description As String
    Private _User As User
    Private _UserResponsible As User 'Felelős személy
    Private _ObjectCreated As Date
    Private _UserCreated As User
    Private _Customers As Customers
    Private _AliasUser As User 'Aki létrehozta az User nevében
    Private _QuestionInProgressState As QuestionInProgressState

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        ObjectCreated = GetSQLTime(Session)
        DocumentNumber = ""
    End Sub
    Protected Overrides Sub OnSaving()
        Dim _Contacts As Contacts
        MyBase.OnSaving()
        If Session.IsNewObject(Me) Then
            ObjectCreated = Now
            UserCreated = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
            If User Is Nothing Then
                User = UserCreated
            End If
            If String.IsNullOrEmpty(DocumentNumber) And NumberGenerator IsNot Nothing Then
                DocumentNumber = NumberGenerator.GetNewNumber(NumberGenerator, Year(Now).ToString)
            End If
            If AliasUser IsNot Nothing Then
                _Contacts = Session.FindObject(Of Contacts)(CriteriaOperator.Parse("User.Oid=?", AliasUser.Oid))
                If _Contacts IsNot Nothing Then Customers = _Contacts.Customers
            Else
                _Contacts = Session.FindObject(Of Contacts)(CriteriaOperator.Parse("User.Oid=?", User.Oid))
                If _Contacts IsNot Nothing Then Customers = _Contacts.Customers
            End If

        End If
    End Sub
    <EditorAlias(DevExpress.ExpressApp.Editors.EditorAliases.DefaultPropertyEditor)> _
    Property QuestionStatus As eQuestionStatus
        Get
            Return _QuestionStatus
        End Get
        Set(value As eQuestionStatus)
            SetPropertyValue("QuestionStatus", _QuestionStatus, value)
        End Set
    End Property
    <EditorAlias(DevExpress.ExpressApp.Editors.EditorAliases.DefaultPropertyEditor)> _
    Property QuestionClosed As eQuestionClosed
        Get
            Return _QuestionClosed
        End Get
        Set(value As eQuestionClosed)
            SetPropertyValue("QuestionClosed", _QuestionClosed, value)
        End Set
    End Property
    <EditorAlias(DevExpress.ExpressApp.Editors.EditorAliases.DefaultPropertyEditor)> _
    Property IssueType As eIssueType
        Get
            Return _IssueType
        End Get
        Set(value As eIssueType)
            SetPropertyValue("IssueType", _IssueType, value)
        End Set
    End Property
    <EditorAlias(DevExpress.ExpressApp.Editors.EditorAliases.DefaultPropertyEditor)> _
    Property DocumentNumber As String
        Get
            Return _DocumentNumber
        End Get
        Set(value As String)
            SetPropertyValue("DocumentNumber", _DocumentNumber, value)
        End Set
    End Property
    <Size(255)> _
    <RuleRequiredField("Question-Subject", DefaultContexts.Save)> _
    Property Subject As String
        Get
            Return _Subject
        End Get
        Set(value As String)
            SetPropertyValue("Subject", _Subject, value)
        End Set
    End Property
    <VisibleInListView(False)> _
    Property Attachment As FileData
        Get
            Return _Attachment
        End Get
        Set(value As FileData)
            SetPropertyValue("Attachment", _Attachment, value)
        End Set
    End Property
    <EditorAlias(DevExpress.ExpressApp.Editors.EditorAliases.HtmlPropertyEditor)> _
    <Size(-1)> _
    <RuleRequiredField("Question-Description", DefaultContexts.Save)> _
    Property Description As String
        Get
            Return _Description
        End Get
        Set(value As String)
            SetPropertyValue("Description", _Description, value)
        End Set
    End Property
    <EditorAlias(DevExpress.ExpressApp.Editors.EditorAliases.DefaultPropertyEditor)> _
    Property User As User
        Get
            Return _User
        End Get
        Set(value As User)
            SetPropertyValue("User", _User, value)
        End Set
    End Property
    <DataSourceProperty("UserResponsibles")> _
    Property UserResponsible As User
        Get
            Return _UserResponsible
        End Get
        Set(value As User)
            SetPropertyValue("UserResponsible", _UserResponsible, value)
        End Set
    End Property
    <NonCloneable> _
    <EditorAlias(DevExpress.ExpressApp.Editors.EditorAliases.DefaultPropertyEditor)> _
    Property ObjectCreated As Date
        Get
            Return _ObjectCreated
        End Get
        Set(value As Date)
            SetPropertyValue("ObjectCreated", _ObjectCreated, value)
        End Set
    End Property
    <EditorAlias(DevExpress.ExpressApp.Editors.EditorAliases.DefaultPropertyEditor)> _
    <NonCloneable> _
    Property UserCreated As User
        Get
            Return _UserCreated
        End Get
        Set(value As User)
            SetPropertyValue("UserCreated", _UserCreated, value)
        End Set
    End Property
    <EditorAlias(DevExpress.ExpressApp.Editors.EditorAliases.DefaultPropertyEditor)> _
    Property Customers As Customers
        Get
            Return _Customers
        End Get
        Set(value As Customers)
            SetPropertyValue("Customers", _Customers, value)
        End Set
    End Property
    Property AliasUser As User
        Get
            Return _AliasUser
        End Get
        Set(value As User)
            SetPropertyValue("AliasUser", _AliasUser, value)
        End Set
    End Property
    Property QuestionInProgressState As QuestionInProgressState
        Get
            Return _QuestionInProgressState
        End Get
        Set(value As QuestionInProgressState)
            SetPropertyValue("QuestionInProgressState", _QuestionInProgressState, value)
        End Set
    End Property

    '----------------ReadOnly------------
    <VisibleInListView(False)> _
    <VisibleInDetailView(False)> _
    ReadOnly Property DisplayName As String
        Get
            Return DocumentNumber & ", " & Subject
        End Get
    End Property
    <VisibleInListView(False)> _
    <Size(-1)> _
    <EditorAlias(DevExpress.ExpressApp.Editors.EditorAliases.DefaultPropertyEditor)> _
    ReadOnly Property HTMLEvents As String
        Get
            Return CreateHTMLEvents(Me, Session)
        End Get
    End Property
    <VisibleInDetailView(False)> _
    ReadOnly Property IMG As System.Drawing.Image
        Get
            If Me.QuestionStatus = eQuestionStatus.eActive Then
                Return ImageLoader.Instance.GetImageInfo("mail2").Image
            Else
                Return ImageLoader.Instance.GetImageInfo("mail").Image
            End If
        End Get
    End Property
    <Browsable(False)> _
    ReadOnly Property NumberGenerator As NumberGenerator
        Get
            Return TryCast(Session.FindObject(Of SupportNG)(CriteriaOperator.Parse("SupportType=0")), SupportNG).NumberGenerator
        End Get
    End Property
    <VisibleInListView(False)> _
    ReadOnly Property WorkSheet_Header As XPCollection(Of WorkSheet_Header)
        Get
            Return New XPCollection(Of WorkSheet_Header)(Session, CriteriaOperator.Parse("Question.Oid=?", Me.Oid))
        End Get
    End Property
    <VisibleInListView(False)> _
    <Browsable(False)> _
    Private ReadOnly Property UserResponsibles As XPCollection(Of User)
        Get
            Dim _UserResponsibles_Collection As XPCollection(Of User) = New XPCollection(Of User)(Session, CriteriaOperator.Parse("1=1"))
            Dim _UserResponsibles_Back_Collection As New XPCollection(Of User)(Session, CriteriaOperator.Parse("1=2"))
            Dim _Need As Boolean = False
            For Each _ActUser As User In _UserResponsibles_Collection
                _Need = False
                If SecuritySystemHelper.IsUserInRole("Supporter", _ActUser) Then
                    _Need = True
                Else
                    Dim _Contacts As Contacts = Session.FindObject(Of Contacts)(CriteriaOperator.Parse("User.Oid=?", _ActUser.Oid))
                    If _Contacts IsNot Nothing Then
                        Dim _Customers As Customers = _Contacts.Customers

                        Dim _Subcontractor As Subcontractor = Session.FindObject(Of Subcontractor)(CriteriaOperator.Parse("Customers=?", _Customers))
                        If _Subcontractor IsNot Nothing Then
                            _Need = True
                        End If
                    End If
                End If
                _UserResponsibles_Back_Collection.Add(_ActUser)
            Next
            Return _UserResponsibles_Back_Collection
        End Get
    End Property


    'Private Function CreateHTMLEvents() As String
    '    Dim _Events_Collection As New XPCollection(Of Events)(Session, CriteriaOperator.Parse("Question.Oid=?", Me.Oid))
    '    Dim _Events As Events
    '    Dim s As String = ""
    '    If _Events_Collection IsNot Nothing Then
    '        _Events_Collection.Sorting.Add(New SortProperty("ObjectCreated", DB.SortingDirection.Ascending))

    '        For Each _Events In _Events_Collection
    '            If _Events.EventDirection = eEventDirection.eIn Then
    '                s += "<table cellspacing='0' cellpadding='0' border='0' width=100% style='background-color:&&ECDEEC;'>"
    '                s += "<tr>"
    '                s += "<td style='width:10px;border-top:solid 1px;border-bottom:solid 1px;background-color:&&ECDEEC;color:&&866A86;"
    '                s += " font-size:small;'>"
    '                s += "</td>"

    '                s += "<td style='background-color:&&ECDEEC;color:&&866A86;vertical-align:center;width:25px;height:30px;"
    '                s += " border-top:solid 1px;border-bottom:solid 1px;"
    '                s += "'>"
    '                s += "<img id='1' alt='->' src='Images/iRedArrow.png' style='height:22px;width:21px;border-style:none;'/>"
    '                s += "</td>"

    '                s += "<td style='background-color:&&ECDEEC;color:&&866A86;vertical-align:center;"
    '                s += " border-top:solid 1px;border-bottom:solid 1px;"
    '                s += "'>"
    '                s += _Events.UserCreated.LastName & " " & _Events.UserCreated.FirstName & " írta"
    '                s += "</td>"

    '                s += "<td style='background-color:&&ECDEEC;color:&&866A86;vertical-align:center;text-align:right;"
    '                s += " border-top:solid 1px;border-bottom:solid 1px;font-size:small;"
    '                s += "'>"
    '                s += Format(_Events.ObjectCreated, "yyyy/MM/dd hh:mm")
    '                s += "</td>"
    '                s += "<td style='width:5px;border-top:solid 1px;border-bottom:solid 1px;background-color:&&ECDEEC;color:&&866A86;"
    '                s += " font-size:small;'>"
    '                s += "</td>"
    '                s += "</tr>"
    '                s += "</table><br>"
    '                s += "<table cellspacing='0' cellpadding='0' border='0' width=100%>"
    '                s += "<tr>"
    '                s += "<td style='width:25px;'></td>"
    '                s += "<td>"
    '                s += _Events.Description
    '                s += "</td>"
    '                s += "</tr>"
    '                s += "</table>"
    '                s += "<br><br>"
    '            End If
    '            If _Events.EventDirection = eEventDirection.eOut Then
    '                s += "<table cellspacing='0' cellpadding='0' border='0' width=100% style='background-color:&&E1F7EC;'>"
    '                s += "<tr>"
    '                s += "<td style='width:10px;border-top:solid 1px;border-bottom:solid 1px;background-color:&&E1F7EC;color:&&83968D;"
    '                s += " font-size:small;'>"
    '                s += "</td>"
    '                s += "<td style='background-color:&&E1F7EC;color:&&83968D;vertical-align:center;width:25px;height:30px;"
    '                s += " border-top:solid 1px;border-bottom:solid 1px;"
    '                s += "'>"
    '                s += "<img id='1' alt='->' src='Images/iGreenArrow.png' style='height:22px;width:21px;border-style:none;'/>"
    '                s += "</td>"

    '                s += "<td style='background-color:&&E1F7EC;color:&&83968D;vertical-align:center;"
    '                s += " border-top:solid 1px;border-bottom:solid 1px;"
    '                s += "'>"
    '                s += _Events.UserCreated.LastName & " " & _Events.UserCreated.FirstName & " válaszolta"
    '                s += "</td>"

    '                s += "<td style='background-color:&&E1F7EC;color:&&83968D;vertical-align:center;text-align:right;"
    '                s += " border-top:solid 1px;border-bottom:solid 1px;font-size:small;"
    '                s += "'>"
    '                s += Format(_Events.ObjectCreated, "yyyy/MM/dd hh:mm")
    '                s += "</td>"
    '                s += "<td style='width:5px;border-top:solid 1px;border-bottom:solid 1px;background-color:&&E1F7EC;color:&&83968D;"
    '                s += " font-size:small;'>"
    '                s += "</td>"
    '                s += "</tr>"
    '                s += "</table><br>"
    '                s += "<table cellspacing='0' cellpadding='0' border='0' width=100%>"
    '                s += "<tr>"
    '                s += "<td style='width:25px;'></td>"
    '                s += "<td>"
    '                s += _Events.Description
    '                s += "</td>"
    '                s += "</tr>"
    '                s += "</table>"

    '                s += "<br><br>"
    '            End If

    '        Next

    '    End If
    '    s = Replace(s, "'", """")
    '    s = Replace(s, "&&", "#")
    '    Return s
    'End Function

End Class
