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 FarPoint.Win.Spread
Imports DevExpress.ExpressApp.SystemModule
Imports System.IO
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.Editors

Public Class WorksheetVC
    Inherits DevExpress.ExpressApp.ViewController
    Private _FpSpread As FpSpread
    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 = "WorkSheet_Header_DetailView" Then
            AddHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf SaveAction_Executing
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            AddHandler _WorkSheet_Header.Changed, AddressOf SetWorksheetTemplateXLS_Changed

        End If
        If View.Id = "WorkSheetType_DetailView" Then
            AddHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf SaveAction_Executing
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "WorkSheet_Header_DetailView" Then
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf SaveAction_Executing
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf SaveAction_Executing
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf SaveAction_Executing
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            RemoveHandler _WorkSheet_Header.Changed, AddressOf SetWorksheetTemplateXLS_Changed
        End If
        If View.Id = "WorkSheetType_DetailView" Then
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf SaveAction_Executing
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf SaveAction_Executing
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf SaveAction_Executing
        End If
    End Sub
    Private Sub ExcelControlViewItem_ControlCreated(ByVal sender As Object, ByVal e As EventArgs)
        Dim itemControl As Object = (CType(sender, ViewItem)).Control

        _FpSpread = TryCast(itemControl, ExcelUserControl).FpSpreadControl

        If View.Id = "WorkSheet_Header_DetailView" Then
            Dim _FileName As String = Path.GetTempPath & "OutSpread.xml"
            Dim _StreamFile As New FileStream(_FileName, FileMode.Create)
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            If _ocur.Session.IsNewObject(_WorkSheet_Header) = False Then
                If _WorkSheet_Header IsNot Nothing Then
                    If _WorkSheet_Header.ExcelFile IsNot Nothing Then
                        _WorkSheet_Header.ExcelFile.SaveToStream(_StreamFile)
                        _StreamFile.Close()
                        _FpSpread.Open(_FileName)
                    End If
                End If
            End If
        End If
        If View.Id = "WorkSheetType_DetailView" Then
            Dim _FileName As String = Path.GetTempPath & "OutSpread.xml"
            Dim _StreamFile As New FileStream(_FileName, FileMode.Create)
            Dim _WorkSheetType As WorkSheetType = TryCast(View.SelectedObjects(0), WorkSheetType)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            If _ocur.Session.IsNewObject(_WorkSheetType) = False Then
                If _WorkSheetType IsNot Nothing Then
                    If _WorkSheetType.ExcelFile IsNot Nothing Then
                        _WorkSheetType.ExcelFile.SaveToStream(_StreamFile)
                        _StreamFile.Close()
                        _FpSpread.Open(_FileName)
                    End If
                End If
            End If
        End If
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "WorkSheet_Header_DetailView" Then
            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _ExcelControlViewItem As ViewItem
            If _DetailView IsNot Nothing Then
                If _DetailView.GetItems(Of ExcelPropertyEditor).Count > 0 Then
                    _ExcelControlViewItem = _DetailView.GetItems(Of ExcelPropertyEditor)(0)
                    If _ExcelControlViewItem IsNot Nothing Then
                        AddHandler _ExcelControlViewItem.ControlCreated, AddressOf ExcelControlViewItem_ControlCreated
                    End If
                End If
            End If
        End If
        If View.Id = "WorkSheetType_DetailView" Then
            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _ExcelControlViewItem As ViewItem
            If _DetailView IsNot Nothing Then
                _ExcelControlViewItem = _DetailView.GetItems(Of ExcelPropertyEditor)(0)
                If _ExcelControlViewItem IsNot Nothing Then
                    AddHandler _ExcelControlViewItem.ControlCreated, AddressOf ExcelControlViewItem_ControlCreated
                End If
            End If
        End If
    End Sub
    Sub SaveAction_Executing(sender As Object, e As System.ComponentModel.CancelEventArgs)
        Dim _FileName As String = Path.GetTempPath & "InSpread.xml"
        Dim _StreamFile As New FileStream(_FileName, FileMode.Create)

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        If View.Id = "WorkSheet_Header_DetailView" Then
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            If _WorkSheet_Header IsNot Nothing Then
                If _FpSpread IsNot Nothing Then
                    If _WorkSheet_Header.DocumentNumber <> "" And _WorkSheet_Header.DocumentNumber IsNot Nothing Then
                        If _WorkSheet_Header.WorkSheetType.WorkSheetTypeExcelParam.Count > 0 Then
                            SetDatatoFpSpread(_WorkSheet_Header.WorkSheetType.WorkSheetTypeExcelParam)
                        End If
                        _FpSpread.Save(_StreamFile, False)
                        _StreamFile.Close()
                        Dim _StreamFile2 As New FileStream(_FileName, FileMode.Open)
                        _WorkSheet_Header.ExcelFile = New FileData(_ocur.Session)
                        _WorkSheet_Header.ExcelFile.LoadFromStream("Excel worksheet", _StreamFile2)
                        _WorkSheet_Header.Save()
                        _StreamFile2.Close()
                    End If
                Else
                    If _FpSpread IsNot Nothing Then
                        _WorkSheet_Header.WorkSheetType.ExcelFile.SaveToStream(_StreamFile)
                        _StreamFile.Close()
                        _FpSpread.Open(_FileName)
                        If _WorkSheet_Header.WorkSheetType.WorkSheetTypeExcelParam.Count > 0 Then
                            SetDatatoFpSpread(_WorkSheet_Header.WorkSheetType.WorkSheetTypeExcelParam)
                        End If
                        Dim _StreamFile2 As New FileStream(_FileName, FileMode.Open)
                        _WorkSheet_Header.ExcelFile = New FileData(_ocur.Session)
                        _WorkSheet_Header.ExcelFile.LoadFromStream("Excel worksheet", _StreamFile2)
                        _WorkSheet_Header.Save()
                        _StreamFile2.Close()
                    End If
                End If
            End If
        End If
        If View.Id = "WorkSheetType_DetailView" Then
            Dim _WorkSheetType As WorkSheetType = TryCast(View.SelectedObjects(0), WorkSheetType)
            If _WorkSheetType IsNot Nothing Then
                If _FpSpread IsNot Nothing Then
                    _FpSpread.Save(_StreamFile, False)
                    _StreamFile.Close()
                    Dim _StreamFile2 As New FileStream(_FileName, FileMode.Open)
                    _WorkSheetType.ExcelFile = New FileData(_ocur.Session)
                    _WorkSheetType.ExcelFile.LoadFromStream("Excel worksheet", _StreamFile2)
                    _WorkSheetType.Save()
                    _StreamFile2.Close()
                End If
            End If
        End If
    End Sub
    Private Sub SetWorksheetTemplateXLS_Changed(sender As Object, e As ObjectChangeEventArgs)

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)

        If _WorkSheet_Header.CustomersFrom IsNot Nothing And _WorkSheet_Header.WorkSheetType IsNot Nothing And _WorkSheet_Header.ExcelFile Is Nothing Then

            _WorkSheet_Header.ExcelFile = _WorkSheet_Header.WorkSheetType.ExcelFile
            If _WorkSheet_Header.ExcelFile IsNot Nothing Then

                Dim _FileName As String = Path.GetTempPath & "Temp.xls"
                Dim _StreamFile As FileStream = Nothing
                _StreamFile = File.Create(_FileName)

                _WorkSheet_Header.ExcelFile.SaveToStream(_StreamFile)
                If TryCast(View, DetailView).GetItems(Of ExcelPropertyEditor)(0).ExcelUserControl IsNot Nothing Then
                    _FpSpread = TryCast(View, DetailView).GetItems(Of ExcelPropertyEditor)(0).ExcelUserControl.FpSpreadControl
                    _StreamFile.Close()
                    _FpSpread.OpenExcel(_FileName)
                End If
            End If
        End If
    End Sub

    Private Sub SetDatatoFpSpread(xPCollection As DevExpress.Xpo.XPCollection(Of WorkSheetTypeExcelParam))
        Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
        Dim _Type As System.Type = GetType(WorkSheet_Header)
        Dim _PropertyInfo As System.Reflection.PropertyInfo() = _Type.GetProperties
        If _FpSpread Is Nothing Then Exit Sub
        For Each _WorkSheetTypeExcelParam As WorkSheetTypeExcelParam In xPCollection
            For Each _PropertyInfo_ACT As System.Reflection.PropertyInfo In _PropertyInfo
                If _WorkSheetTypeExcelParam.PropertiesInfo = _PropertyInfo_ACT.Name Then
                    Select Case _PropertyInfo_ACT.Name
                        Case "DocumentNumber"
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Locked = True
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).TabStop = False
                            If _WorkSheet_Header.DocumentNumber IsNot Nothing Or _WorkSheet_Header.DocumentNumber <> "" Then
                                _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Text = _WorkSheet_Header.DocumentNumber.ToString
                            Else
                                _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Text = ""
                            End If
                            Exit For
                        Case "Customers"
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Locked = True
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).TabStop = False
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Text = _WorkSheet_Header.Customers.FullName.ToString
                            Exit For
                        Case "DateExecution"
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Locked = True
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).TabStop = False
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Text = _WorkSheet_Header.DateExecution.ToString
                            Exit For
                    End Select
                End If
            Next
        Next
    End Sub


End Class
