Imports DevExpress.Xpf.Reports.UserDesigner
Imports DevExpress.Xpf.Reports.UserDesigner.Native
Imports DevExpress.XtraReports.UI
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports DevExpress.Xpf.Core
Imports DevExpress.Xpf.Ribbon
Imports DevExpress.Xpf.Core.Native
Imports Microsoft.Win32

Public Class ucDiseñadorInformes
    'Private _irs As DevExpress.Xpf.Reports.UserDesigner.IReportStorage
    'Private _xr As DevExpress.XtraReports.UI.XtraReport
    'Private _id As Integer
    ' Public Delegate Sub Guardar(ms As IO.Stream, idPlantilla As Integer)
    '   Public Delegate Sub Guardar(b() As Byte, idPlantilla As Integer)
    '   Private _DelegadoGuardar As Guardar
    Private _idPlantilla As Integer
    Private Sub ucDiseñadorInformes_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        '  Me.tsrd.ReportStorage = _irs
        '  Me.tsrd.OpenDocument(_xr)
    End Sub

    Public Sub New(xr As DevExpress.XtraReports.UI.XtraReport, idPlantilla As Integer, DelegadoGuardar As IRS.Guardar)
        Try
            Dim irs = New IRS(idPlantilla, DelegadoGuardar)
            ' Llamada necesaria para el diseñador.
            DXSplashScreen.Show(Of tsWPF.SplashScreenTecnosis)()
            DXSplashScreen.SetState("Cargando Diseñador de Plantillas ...")
            InitializeComponent()
            DXSplashScreen.SetState("Cargando Plantilla ...")
            Me.tsrd.ReportStorage = irs
            Me.tsrd.OpenDocument(xr)
            _idPlantilla = idPlantilla
        Catch ex As Exception
            If DXSplashScreen.IsActive Then DXSplashScreen.Close()
            Throw New Exception(ex.Message, ex)
        End Try
        '  _xr = xr
        '   _DelegadoGuardar = DelegadoGuardar




        '  DXSplashScreen.Close()
        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().

    End Sub

    'Private Sub tsrd_DocumentSaved(sender As Object, e As ReportDesignerDocumentEventArgs)
    '    Dim report As XtraReport = e.Document.Report
    '    Dim ms As New MemoryStream
    '    report.SaveLayout(ms)
    '    _DelegadoGuardar(ms.ToArray, _idPlantilla)

    'End Sub

    Private Sub tsrd_Loaded(sender As Object, e As RoutedEventArgs) Handles tsrd.Loaded
        Try
            If DXSplashScreen.IsActive Then DXSplashScreen.Close()
            Dim rd As RibbonControl = CType(LayoutHelper.FindElementByType(tsrd, GetType(RibbonControl)), RibbonControl)
            If rd IsNot Nothing Then
                Dim categorias = rd.Categories
                For Each c In rd.Categories
                    For Each p In c.Pages
                        For Each g In p.Groups
                            For Each item In g.Items
                                If TypeOf item Is DevExpress.Xpf.Bars.BarButtonItem Then
                                    Dim it = TryCast(item, DevExpress.Xpf.Bars.BarButtonItem)
                                    If it IsNot Nothing AndAlso it.Content IsNot Nothing Then
                                        Debug.Write(it.Content)
                                        Select Case it.Content.ToString.ToLower
                                            'Case "new report", "open...", "save as..."
                                            '    it.IsVisible = False
                                            Case "new report"
                                                it.IsVisible = False
                                            Case "open..."
                                                it.Content = "Abrir..."
                                             '   it.IsVisible = True
                                            Case "save as..."
                                                it.Content = "Guardar como..."
                                            Case "save"
                                                it.Content = "Guardar"
                                        End Select
                                    End If
                                End If
                            Next
                        Next
                    Next
                Next
            End If
        Catch ex As Exception
            DXMessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Error en tsrd_Loaded")
            Throw New Exception(ex.Message, ex)
        End Try
        '   tsrdv.ActiveDocument.Close()
    End Sub
End Class


Public Class IRS

    Implements IReportStorage

    Public Delegate Sub Guardar(b() As Byte, idPlantilla As Integer)
    ' Private _xr As DevExpress.XtraReports.UI.XtraReport
    Private _idPlantilla As Integer
    Private _DelegadoGuardar As Guardar

    Public Function CanCreateNew() As Boolean Implements IReportStorage.CanCreateNew
        Return False
    End Function

    Public Function CanOpen() As Boolean Implements IReportStorage.CanOpen
        Return True
    End Function

    Public Function CreateNew() As XtraReport Implements IReportStorage.CreateNew

    End Function

    Public Function CreateNewSubreport() As XtraReport Implements IReportStorage.CreateNewSubreport

    End Function

    Public Function GetErrorMessage(exception As Exception) As String Implements IReportStorage.GetErrorMessage

    End Function


    Private Function GetBuffer(ByVal report As XtraReport) As Byte()
        Using stream As New MemoryStream()
            report.SaveLayout(stream)
            Return stream.ToArray()
        End Using
    End Function


    Public Function Load(reportID As String, designerReportSerializer As IReportSerializer) As XtraReport Implements IReportStorage.Load
        Return XtraReport.FromFile(reportID, True)
        '        Return XtraReport.FromStream(MS, True)

    End Function

    Public Function Open(designer As IReportDesignerUI) As String Implements IReportStorage.Open
        Dim fb As New OpenFileDialog
        fb.Filter = "Archivo repx|*.repx"
        fb.DefaultExt = ".repx"

        If fb.ShowDialog Then
            Return fb.FileName
        Else
            Return ""
        End If
    End Function

    Public Function Save(reportID As String, reportProvider As IReportProvider, saveAs As Boolean, reportTitle As String, designer As IReportDesignerUI) As String Implements IReportStorage.Save
        Dim report As XtraReport = reportProvider.GetReport()

        If saveAs Then
            Dim sfd As New SaveFileDialog
            sfd.FileName = report.Name & ".repx"
            sfd.Filter = "Archivo repx|*.repx"
            sfd.DefaultExt = ".repx"
            If sfd.ShowDialog Then
                Dim fs As New IO.FileStream(sfd.FileName, FileMode.Create)
                report.SaveLayout(fs)
                fs.Close()
            End If
            Return ""
        Else
            Dim ms As New MemoryStream
            report.SaveLayout(ms)
            _DelegadoGuardar(ms.ToArray, _idPlantilla)
            Return ""
        End If
    End Function

    Public Sub New(idPlantilla As Integer, DelegadoGuardar As Guardar)
        _DelegadoGuardar = DelegadoGuardar
        _idPlantilla = idPlantilla
        '  _xr = xr
    End Sub
End Class
