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.Editors
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.XtraRichEdit

Public Class RTFDocumentsHandlerVC
    Inherits Nuvolar.Module.RTFDocumentsHandlerVC

    Private _RichEditControl As RichEditControl
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "RTFDocumentsHandler_DetailView" Then
            Dim _RTFDocumentsHandler As RTFDocumentsHandler = TryCast(View.SelectedObjects(0), RTFDocumentsHandler)
            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _RichEditContolViewItem As ViewItem = _DetailView.FindItem("DocumentBody")
            If _RichEditContolViewItem IsNot Nothing Then
                AddHandler _RichEditContolViewItem.ControlCreated, AddressOf RichEditContolViewItem_ControlCreated
            End If
        End If
    End Sub
    Private Sub aRFTDocuments_CreateBusinessDirectoryDocument_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aRFTDocuments_CreateBusinessDirectoryDocument.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _RTFDocumentsHandler As RTFDocumentsHandler = TryCast(View.SelectedObjects(0), RTFDocumentsHandler)
            If _RTFDocumentsHandler Is Nothing Then Throw New Exception("*Váratlan hiba történt a folyamat megszakadt!")
            Dim _RichEditControlNew As New RichEditControl
            _RichEditControlNew.RtfText = _RTFDocumentsHandler.DocumentBody

            Dim _RTFDocumentsCreatorHeader As New RTFDocumentsCreatorHeader(_onew.Session)
            Dim _Fields As DevExpress.XtraRichEdit.API.Native.FieldCollection = _RichEditControlNew.Document.Fields
            Dim _Document As DevExpress.XtraRichEdit.API.Native.Document = _RichEditControlNew.Document
            For Each _ActTagString As DevExpress.XtraRichEdit.API.Native.Field In _Fields
                Dim _RTFDocumentsCreatorRows As New RTFDocumentsCreatorRows(_onew.Session)
                With _RTFDocumentsCreatorRows
                    .RTFDocumentsCreatorHeader = _RTFDocumentsCreatorHeader
                    .InfoString = _Document.GetText(_ActTagString.CodeRange)
                    _RTFDocumentsCreatorHeader.RTFDocumentsCreatorRows.Add(_RTFDocumentsCreatorRows)
                End With
            Next
            _RTFDocumentsCreatorHeader.FileName = _RTFDocumentsHandler.ShortName

            e.View = Application.CreateDetailView(_onew, "RTFDocumentsCreatorHeader_DetailView", False, _RTFDocumentsCreatorHeader)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub
    Private Sub aRFTDocuments_CreateBusinessDirectoryDocument_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aRFTDocuments_CreateBusinessDirectoryDocument.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _RTFDocumentsHandler As RTFDocumentsHandler = TryCast(View.SelectedObjects(0), RTFDocumentsHandler)
            If _RTFDocumentsHandler Is Nothing Then Throw New Exception("*Váratlan hiba történt a folyamat megszakadt!")
            Dim _RichEditControlNew As New RichEditControl
            _RichEditControlNew.RtfText = _RTFDocumentsHandler.DocumentBody
            Dim _RTFDocumentsCreatorHeader As RTFDocumentsCreatorHeader = TryCast(e.PopupWindow.View.SelectedObjects(0), RTFDocumentsCreatorHeader)

            Dim _FieldIndex As Int16 = 0
            Dim _Fields As DevExpress.XtraRichEdit.API.Native.FieldCollection = _RichEditControlNew.Document.Fields

            _RichEditControlNew.BeginUpdate()

            Dim _Document As DevExpress.XtraRichEdit.API.Native.Document = _RichEditControlNew.Document

            _Document.BeginUpdate()

            For Each _RTFDocumentsCreatorRows As RTFDocumentsCreatorRows In _RTFDocumentsCreatorHeader.RTFDocumentsCreatorRows
                Dim field As DevExpress.XtraRichEdit.API.Native.Field = _Fields(_FieldIndex)
                field.ShowCodes = True
                Dim _FieldCode As String = _Document.GetText(field.CodeRange)
                Dim _FieldResult As String = _RTFDocumentsCreatorRows.ValueString
                Dim _FieldPosition As DocumentPosition = field.Range.Start
                _Document.Delete(field.Range)
                _Document.InsertText(_FieldPosition, _FieldResult)
            Next
            _Document.EndUpdate()
            _RichEditControlNew.EndUpdate()

            Dim _FilePath As String = System.IO.Path.GetTempPath
            Dim _FileName As String
            Select Case _RTFDocumentsCreatorHeader.RTFExportFileType
                Case eRTFExportFileType.ePDF
                    _FileName = _RTFDocumentsCreatorHeader.FileName + ".pdf"
                    _RichEditControlNew.ExportToPdf(_FilePath + _FileName)
                Case eRTFExportFileType.eRTF
                    _FileName = _RTFDocumentsCreatorHeader.FileName + ".rtf"
                    _RichEditControlNew.SaveDocument(_FilePath + _FileName, DocumentFormat.Rtf)
                Case eRTFExportFileType.eWord
                    _FileName = _RTFDocumentsCreatorHeader.FileName + ".doc"
                    _RichEditControlNew.SaveDocument(_FilePath + _FileName, DocumentFormat.Doc)
                Case eRTFExportFileType.eWordXDoc
                    _FileName = _RTFDocumentsCreatorHeader.FileName + ".docx"
                    _RichEditControlNew.SaveDocument(_FilePath + _FileName, DocumentFormat.WordML)
            End Select

            If System.IO.File.Exists(_FilePath + _FileName) Then
                Dim _BusinessDirectory As New BusinessDirectory(_ocur.Session)
                With _BusinessDirectory
                    _BusinessDirectory.BusinessDirectory_Parent = _ocur.GetObject(_RTFDocumentsCreatorHeader.BusinessDirectory)
                    _BusinessDirectory.IsFolder = False
                    _BusinessDirectory.FileData = LoadOtherFile(_FilePath + _FileName, New DevExpress.Persistent.BaseImpl.FileData(_ocur.Session))
                    _BusinessDirectory.FileCreated = GetFileCreatedDate(_FilePath + _FileName)
                    _BusinessDirectory.FileModified = GetFileModifiedDate(_FilePath + _FileName)
                End With

                System.IO.File.Delete(_FilePath + _FileName)
            End If

            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub
    Private Sub aRTFDocuments_InsertRTFDocVariable_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aRTFDocuments_InsertRTFDocVariable.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _RTFDocumanetsHandler_DocVariable_PopUp As New RTFDocumanetsHandler_DocVariable_PopUp(_onew.Session)

            e.View = Application.CreateDetailView(_onew, "RTFDocumanetsHandler_DocVariable_PopUp_DetailView", False, _RTFDocumanetsHandler_DocVariable_PopUp)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub
    Private Sub aRTFDocuments_InsertRTFDocVariable_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aRTFDocuments_InsertRTFDocVariable.Execute
        Try
            Dim _RTFDocumanetsHandler_DocVariable_PopUp As RTFDocumanetsHandler_DocVariable_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), RTFDocumanetsHandler_DocVariable_PopUp)
            _RichEditControl.Document.Fields.Add(_RichEditControl.Document.CaretPosition, "DOCVARIABLE " + _RTFDocumanetsHandler_DocVariable_PopUp.DocVariableString)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub
    Private Sub RichEditContolViewItem_ControlCreated(sender As Object, e As EventArgs)
        Dim _ItemControl As Object = TryCast(sender, ViewItem).Control
        Dim _RichEditUserControl As RichEditUserControl = TryCast(_ItemControl, RichEditUserControl)
        _RichEditControl = TryCast(_ItemControl, RichEditUserControl).RichEditControl
    End Sub
    Private Function LoadOtherFile(ByVal _ActFile As String, _FileData As DevExpress.Persistent.BaseImpl.FileData) As DevExpress.Persistent.BaseImpl.FileData
        Dim _StreamFile = New System.IO.FileStream(_ActFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)
        Dim _StreamReader As New System.IO.StreamReader(_StreamFile)
        _StreamFile.Seek(0, System.IO.SeekOrigin.Begin)

        _FileData.LoadFromStream(_ActFile, _StreamFile)
        _FileData.FileName = System.IO.Path.GetFileName(_ActFile)
        _StreamReader.Close()
        _StreamFile.Close()

        Return _FileData

    End Function
    Private Function GetFileCreatedDate(_ActFile As String) As Date
        Return System.IO.File.GetCreationTime(_ActFile)
    End Function
    Private Function GetFileModifiedDate(_ActFile As String) As Date
        Return System.IO.File.GetLastAccessTime(_ActFile)
    End Function
End Class
