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.DashboardCommon
Imports DevExpress.DataAccess
Imports DevExpress.DataAccess.ConnectionParameters
Imports DevExpress.DataAccess.Sql

Public Class DashboardViewerVC
    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 = "Dashboard_DetailView" Then
            Dim _Dashboard As Dashboard = TryCast(View.SelectedObjects(0), Dashboard)
            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
    End Sub
    Private Sub _DashboardViewer_ViewItem_ControlCreated(sender As Object, e As EventArgs)
        Dim _DashboardViewerPropertyEditor As DashboardViewerPropertyEditor = TryCast(sender, DashboardViewerPropertyEditor)
        If _DashboardViewerPropertyEditor IsNot Nothing Then

            AddHandler _DashboardViewerPropertyEditor.ASPxDashboardViewer.ConfigureDataConnection, AddressOf _DashboardViewerPropertyEditor_ASPxDashboardViewer_ConfigureDataConnection
            'AddHandler _DashboardViewerPropertyEditor.ASPxDashboardViewer.PreRender, AddressOf _DashboardViewerPropertyEditor_ASPxDashboardViewer_PreRender

            Dim _Dashboard As Dashboard = TryCast(View.SelectedObjects(0), Dashboard)

            Dim _FileName As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetTempPath, _Dashboard.XMLFileData.FileName)
            Dim _StreamFile As System.IO.Stream = New System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write)
            _Dashboard.XMLFileData.SaveToStream(_StreamFile)
            _StreamFile.Close()

            With _DashboardViewerPropertyEditor.ASPxDashboardViewer
                .DashboardXmlFile = _FileName
                .Width = 800
                .Height = 600
            End With
        End If
    End Sub

    Private Sub _DashboardViewerPropertyEditor_ASPxDashboardViewer_ConfigureDataConnection(sender As Object, e As ConfigureDataConnectionEventArgs)
        Dim _Dashboard As Dashboard = TryCast(View.SelectedObjects(0), Dashboard)
        If _Dashboard IsNot Nothing Then
            Select Case _Dashboard.SQLType.ToString
                Case "MSSQL"
                    Dim _MsSqlConnectionParameters As New MsSqlConnectionParameters
                    _MsSqlConnectionParameters.UserName = _Dashboard.UserName
                    _MsSqlConnectionParameters.Password = _Dashboard.Password
                    _MsSqlConnectionParameters.DatabaseName = _Dashboard.Database
                    _MsSqlConnectionParameters.ServerName = _Dashboard.SQLServer

                    e.ConnectionParameters = _MsSqlConnectionParameters
                Case "MySQL"
                    Dim _MySqlConnectionParameters As New MySqlConnectionParameters
                    _MySqlConnectionParameters.UserName = _Dashboard.UserName
                    _MySqlConnectionParameters.Password = _Dashboard.Password
                    _MySqlConnectionParameters.DatabaseName = _Dashboard.Database
                    _MySqlConnectionParameters.ServerName = _Dashboard.SQLServer

                    e.ConnectionParameters = _MySqlConnectionParameters
                Case "PostgerSQL"
                    Dim _PostgreSqlConnectionParameters As New PostgreSqlConnectionParameters
                    _PostgreSqlConnectionParameters.UserName = _Dashboard.UserName
                    _PostgreSqlConnectionParameters.Password = _Dashboard.Password
                    _PostgreSqlConnectionParameters.DatabaseName = _Dashboard.Database
                    _PostgreSqlConnectionParameters.ServerName = _Dashboard.SQLServer

                    e.ConnectionParameters = _PostgreSqlConnectionParameters
            End Select
        End If
    End Sub
    Private Sub _DashboardViewerPropertyEditor_ASPxDashboardViewer_PreRender(sender As Object, e As EventArgs)
        'Beep()


    End Sub

End Class
