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.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.Data.Filtering
Imports System.Net.Mail
Imports DevExpress.Persistent.BaseImpl
Imports System.Reflection

Public Class AnswerVC
    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 = "Answer_DetailView" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _Answer As Answer

            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", False)
            If SecuritySystem.CurrentUserName <> "ivan.szabo" Then Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)

            _Answer = TryCast(View.SelectedObjects(0), Answer)
            If _ocur.Session.IsNewObject(_Answer) Then
                Frame.GetController(Of AnswerVC).aSaveAnswer.Active.SetItemValue("", True)
            Else
                Frame.GetController(Of AnswerVC).aSaveAnswer.Active.SetItemValue("", False)
            End If
            If View.Id = "Answer_ListView" Then
                Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
                If SecuritySystem.CurrentUserName <> "ivan.szabo" Then Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            End If
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "Answer_DetailView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aSaveAnswer_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSaveAnswer.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Answer As Answer = TryCast(View.SelectedObjects(0), Answer)

        Dim _Events As Events
        If _Answer IsNot Nothing Then
            _Answer.Question.QuestionStatus = _Answer.QuestionStatus
            _Answer.Question.IssueType = _Answer.IssueType
            _Answer.Question.Save()
            _Answer.Save()

            _ocur.CommitChanges()

            _Events = _onew.CreateObject(Of Events)()
            _Events.Question = _onew.GetObject(_Answer.Question)
            _Events.QuestionStatus = _Answer.QuestionStatus
            _Events.Description = _Answer.Description
            If _Answer.Attachment IsNot Nothing Then _Events.Attachment = _onew.GetObject(_Answer.Attachment)
            _Events.Save()
            _Events.EventDirection = eEventDirection.eOut
            _onew.CommitChanges()


            Try
                Dim _WebParameters As WebParameters = _ocur.FindObject(Of WebParameters)(CriteriaOperator.Parse("1=1"))
                If _WebParameters Is Nothing Then Throw New Exception("*Nincsnek e-mail paraméterek beállítva!")
                If _WebParameters.SMTPHost Is Nothing Then Throw New Exception("*Nincs SMTP server beállítva!")
                If _WebParameters.SMTPUserName = "" Then Throw New Exception("*Nincs felhasználói név beállítva!")
                If _WebParameters.SMTPPort = 0 Then Throw New Exception("*Nincs SMTP port beállítva!")


                Dim _MailMessage As New MailMessage
                Dim _Attachment As Attachment
                Dim _SmtpClient As SmtpClient = New SmtpClient(_WebParameters.SMTPHost)
                _MailMessage.From = New MailAddress(_WebParameters.Email_SupportAdmin)
                _MailMessage.To.Add(_Answer.Question.UserCreated.Email)
                _MailMessage.Subject = _Answer.Question.DocumentNumber + " számú bejelentésre válasz"
                _MailMessage.IsBodyHtml = True

                Dim _EmailBody As String = _WebParameters.HTMLMailTemplate_QuestionStatusUpdate.HTMLBody
                _EmailBody = String.Format(_EmailBody, _Answer.Question.UserCreated.FullName, _Answer.Question.DocumentNumber, DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Enums\Nuvolar.Module.eQuestionStatus", _Answer.Question.QuestionStatus.ToString) + _
                                               vbNewLine, TryCast(SecuritySystem.CurrentUser, User).FullName)
                _EmailBody = _EmailBody + vbNewLine + CreateHTMLEventsMail(_Answer.Question, _ocur.Session)

                _MailMessage.Body = _EmailBody
                _MailMessage.Attachments.Clear()
                If Not String.IsNullOrEmpty(GLOBAL_ApplicationStartupPath_Mail) Then
                    _Attachment = New Attachment(GLOBAL_ApplicationStartupPath_Mail & "Images\iRedArrow.png")
                    _Attachment.ContentId = "c001"
                    _MailMessage.Attachments.Add(_Attachment)

                    _Attachment = New Attachment(GLOBAL_ApplicationStartupPath_Mail & "Images\iGreenArrow.png")
                    _Attachment.ContentId = "c002"
                    _MailMessage.Attachments.Add(_Attachment)
                End If

                _SmtpClient.Port = _WebParameters.SMTPPort
                _SmtpClient.Credentials = New System.Net.NetworkCredential(_WebParameters.SMTPUserName, _WebParameters.SMTPPassword)
                _SmtpClient.EnableSsl = _WebParameters.EnableSsl

                _SmtpClient.Send(_MailMessage)


            Catch ex As Exception
                Dim sf As StackFrame = New StackFrame()
                Dim _ErrorLog As ErrorLog = _ocur.CreateObject(Of ErrorLog)()
                _ErrorLog.Description = ex.Message
                _ErrorLog.SubName = TryCast(sf.GetMethod(), MethodBase).Name
                _ocur.CommitChanges()
            End Try


            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "Question_DetailView_Info", True, _onew.GetObject(_Answer.Question))
            View.Close()
        End If
    End Sub
End Class
