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

Public Class Reply_VC
    Inherits DevExpress.ExpressApp.ViewController

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

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of Reply_VC).aSaveReply.Active.SetItemValue("", False)


        If View.Id = "Reply_DetailView" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of Reply_VC).aSaveReply.Active.SetItemValue("", True)
            If SecuritySystem.CurrentUserName <> "ivan.szabo" Then Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "Reply_DetailView" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of Reply_VC)().aSaveReply.Active.SetItemValue("", False)
        End If
    End Sub
    Protected Sub aSaveReply_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSaveReply.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Reply As Reply = TryCast(View.SelectedObjects(0), Reply)

        Dim _Events As Events
        If _Reply IsNot Nothing Then
            _Reply.Question.QuestionStatus = eQuestionStatus.eActive
            _Reply.Question.QuestionClosed = eQuestionClosed.eOpen
            _Reply.Question.Save()
            _Reply.Save()

            _ocur.CommitChanges()

            _Events = _onew.CreateObject(Of Events)()
            _Events.Question = _onew.GetObject(_Reply.Question)
            _Events.Description = _Reply.Description
            If _Reply.Attachment IsNot Nothing Then _Events.Attachment = _onew.GetObject(_Reply.Attachment)
            _Events.Save()
            _Events.EventDirection = eEventDirection.eIn
            _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(_Reply.Question.UserCreated.Email)
                _MailMessage.Subject = _Reply.Question.DocumentNumber + " számú bejelentés"
                _MailMessage.IsBodyHtml = True

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

                _EmailBody = _EmailBody + vbNewLine + CreateHTMLEventsMail(_Reply.Question, _ocur.Session)

                _MailMessage.Body = _EmailBody
                _MailMessage.Attachments.Clear()

                _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)

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

                _SmtpClient.Send(_MailMessage)

                _MailMessage.To.Clear()
                _MailMessage.To.Add(_WebParameters.Email_SupportAdmin)
                _MailMessage.Subject = "Új válasz érkezett"
                _MailMessage.Body = String.Format("{0} felhasználó a(z) {1} kérdéshez új választ rögzített.", _Reply.Question.UserCreated.FullName, _Reply.Question.DocumentNumber)
                _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(_Reply.Question))
            View.Close()
        End If
    End Sub
End Class
