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.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.Data.Filtering
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.DashboardCommon
Imports DevExpress.Xpo

Imports System.IO
Imports System.Xml
Imports System.Reflection
Imports DevExpress.DashboardWin

Public Class Dashboard_VC
    Inherits Nuvolar.Module.Dashboard_VC

    Private _DashboardViewer As DashboardViewer
    Private _DashboardDesigner As DashboardDesigner

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

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "Dashboard_DetailView_Viewer" Then

            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)

            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _DashboardViewer_ViewItem As ViewItem = _DetailView.FindItem("DashboardViewer")
            If _DashboardViewer_ViewItem IsNot Nothing Then
                AddHandler _DashboardViewer_ViewItem.ControlCreated, AddressOf DashboardViewer_ViewItem_ControlCreated
            End If
        End If
        If View.Id = "Dashboard_DetailView_Designer" Then
            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)

            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _DashboardDesigner_ViewItem As ViewItem = _DetailView.FindItem("DashboardDesigner")
            If _DashboardDesigner_ViewItem IsNot Nothing Then
                AddHandler _DashboardDesigner_ViewItem.ControlCreated, AddressOf DashboardDesigner_ViewItem_ControlCreated
            End If

        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "Dashboard_DetailView_Viewer" Or
            View.Id = "Dashboard_DetailView_Designer" Then
            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)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub DashboardViewer_ViewItem_ControlCreated(sender As Object, e As EventArgs)
        Dim _Dashboard As Dashboard = TryCast(View.SelectedObjects(0), Dashboard)
        Dim _ItemControl As Object = TryCast(sender, ViewItem).Control
        Dim _DashboardViewerUserControl As DashboardViewerUserControl = TryCast(_ItemControl, DashboardViewerUserControl)
        _DashboardViewer = _DashboardViewerUserControl.DashboardViewer1

        If _DashboardViewer IsNot Nothing Then
            With _DashboardViewerUserControl
                .UserName = _Dashboard.UserName
                .Password = _Dashboard.Password
                .SQLType = _Dashboard.SQLType
                .SQLServer = _Dashboard.SQLServer
                .Database = _Dashboard.Database
            End With

            Dim _MemoryStream As New IO.MemoryStream()
            _Dashboard.XMLFileData.SaveToStream(_MemoryStream)
            _MemoryStream.Seek(0, IO.SeekOrigin.Begin)
            With _DashboardViewer
                .LoadDashboard(_MemoryStream)
            End With
        End If
    End Sub
    Private Sub DashboardDesigner_ViewItem_ControlCreated(sender As Object, e As EventArgs)
        Dim _Dashboard As Dashboard = TryCast(View.SelectedObjects(0), Dashboard)
        Dim _ItemControl As Object = TryCast(sender, ViewItem).Control
        Dim _DashboardDesignerUserControl As DashboardDesignerUserControl = TryCast(_ItemControl, DashboardDesignerUserControl)
        _DashboardDesigner = _DashboardDesignerUserControl.DashboardDesigner1

        If _DashboardDesigner IsNot Nothing Then
            If Not _Dashboard.IsNewDashboard Then
                With _DashboardDesignerUserControl
                    .UserName = _Dashboard.UserName
                    .Password = _Dashboard.Password
                    .SQLType = _Dashboard.SQLType
                    .SQLServer = _Dashboard.SQLServer
                    .Database = _Dashboard.Database
                End With

                Dim _MemoryStream As New IO.MemoryStream()
                _Dashboard.XMLFileData.SaveToStream(_MemoryStream)
                _MemoryStream.Seek(0, IO.SeekOrigin.Begin)
                With _DashboardDesigner
                    .LoadDashboard(_MemoryStream)
                    .ConfirmSaveOnClose = False
                End With
            End If
        End If
    End Sub
    Private Sub aDashboard_SaveDashDesForBusinessDirectory_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aDashboard_SaveDashDesForBusinessDirectory.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Dashboard As Dashboard = _ocur.GetObject(TryCast(View.SelectedObjects(0), Dashboard))
        Dim _FileName As String = IO.Path.GetTempPath
        If _Dashboard.IsNewDashboard Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _BusinessDirectory As New BusinessDirectory(_onew.Session)
            With _BusinessDirectory
                .IsDashBoard = True
                _FileName += _DashboardDesigner.SelectedDataSource.Name + ".xml"
                _DashboardDesigner.Dashboard.SaveToXml(_FileName)
                If IO.File.Exists(_FileName) Then
                    _BusinessDirectory.FileData = Nothing
                    _BusinessDirectory.FileData = LoadOtherFile(_FileName, New FileData(_onew.Session))
                End If
            End With
            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "BusinessDirectory_DetailView", True, _BusinessDirectory)
        Else
            Select Case _Dashboard.Object_Type
                Case GetType(BusinessDirectory)
                    Dim _BusinessDirectory As BusinessDirectory = _ocur.FindObject(Of BusinessDirectory)(CriteriaOperator.Parse("Oid=?", _Dashboard.Object_Oid))
                    If _BusinessDirectory IsNot Nothing Then
                        _FileName += _BusinessDirectory.FileData.FileName
                        _DashboardDesigner.Dashboard.SaveToXml(_FileName)
                        If IO.File.Exists(_FileName) Then
                            _BusinessDirectory.FileData = Nothing
                            _BusinessDirectory.FileData = LoadOtherFile(_FileName, New FileData(_ocur.Session))
                        End If
                    End If
                Case Else
                    MsgBox("Almafa!")
            End Select

            'Dim _da As New DevExpress.DashboardCommon.Dashboard
            'Dim _dd As New DevExpress.XtraDashboard.DashboardDesigner
            '_dd.LoadDashboard(_FileName)
            'Beep()
            '_dd.d()
            ''_dd.DataBindings
            _ocur.CommitChanges()
        End If


    End Sub
    Private Function LoadOtherFile(ByVal _ActFile As String, _FileData As FileData) As 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 Sub GetXMLInfo(_FileName As String)
        Dim reader As New XmlTextReader("books.xml")
        While reader.Read()
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    ' A csomópont egy összetevő.
                    Console.Write("<" & reader.Name)
                    Console.WriteLine(">")
                    Exit Select
                Case XmlNodeType.Text
                    'Az egyes összetevők szövegének megjelenítése.
                    Console.WriteLine(reader.Value)
                    Exit Select
                Case XmlNodeType.EndElement
                    'Az összetevő végének megjelenítése.
                    Console.Write("</" & reader.Name)
                    Console.WriteLine(">")
                    Exit Select
            End Select
        End While
        Console.ReadLine()
    End Sub
End Class