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.SystemModule
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.Persistent.BaseImpl
Imports System.IO

Public Class APCSheetReviewHeader_VC
    Inherits DevExpress.ExpressApp.ViewController

    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork

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

#Region "Overrides"
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "APCSheetReviewHeader_APCSheetReviewHeaderPhotos_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "APCSheetReviewHeader_APCSheetReviewRows_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "APCSheetReviewHeader_APCSheetReviewHeaderPhotos_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "APCSheetReviewHeader_APCSheetReviewRows_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
        End If
    End Sub
#End Region
   
    Private Sub aAPCSheetReviewHeaderPhotos_AddPhotos_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aAPCSheetReviewHeaderPhotos_AddPhotos.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _APCSheetReviewHeader As APCSheetReviewHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, APCSheetReviewHeader)
        If _APCSheetReviewHeader IsNot Nothing Then
            Try
                Dim _OpenFileDialog As New System.Windows.Forms.OpenFileDialog
                _OpenFileDialog.Filter = "JPG file (.jpg)|*.jpg|BMP file (.bmp)|*.bmp|GIF file (.gif)|*.gif|All Files (*.*)|*.*"
                _OpenFileDialog.Multiselect = True

                If _OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    RaiseEvent InitWork(1, 1, _OpenFileDialog.FileNames.Length)
                    For Each _FileName As String In _OpenFileDialog.FileNames
                        RaiseEvent DoWork()
                        Dim _APCSheetReviewHeaderPhotos As New APCSheetReviewHeaderPhotos(_ocur.Session)
                        With _APCSheetReviewHeaderPhotos
                            .APCSheetReviewHeader = _APCSheetReviewHeader
                            .PhotoFile = LoadOtherFile(_FileName, New FileData(_ocur.Session))
                        End With
                    Next
                End If

                _ocur.CommitChanges()

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
            Finally
                RaiseEvent FinalWork
            End Try
        End If
    End Sub
    Private Sub aAPCSheetReviewHeaderPhotos_OpenPhoto_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aAPCSheetReviewHeaderPhotos_OpenPhoto.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _APCSheetReviewHeaderPhotos As APCSheetReviewHeaderPhotos = TryCast(View.SelectedObjects(0), APCSheetReviewHeaderPhotos)
        If _APCSheetReviewHeaderPhotos IsNot Nothing Then
            Dim _FilePath As String = Path.Combine(Path.GetTempPath(), _APCSheetReviewHeaderPhotos.PhotoFile.FileName)
            Dim _Stream As Stream = New FileStream(_FilePath, FileMode.Create)
            _APCSheetReviewHeaderPhotos.PhotoFile.SaveToStream(_Stream)
            _Stream.Close()
            Process.Start(_FilePath)
        End If
    End Sub
#Region "Subs and Functions"
    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
#End Region
End Class
