Imports System.Collections.ObjectModel
Imports EgtBEAMWALL.Core
Imports EgtWPFLib5
Imports EgtUILib
Imports System.Globalization
Imports System.IO

Public Class PDFEditorVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Private Enum PDFPage As Integer
      VIEW = 0       '  PDF Visualizzatore
      MACHING = 1    '  PDF Ottimizzatore
   End Enum

   ' Lista contenente le singole categorie
   Private m_ExpanderList As New ObservableCollection(Of ExpanderPDF)
   Public ReadOnly Property ExpanderList As ObservableCollection(Of ExpanderPDF)
      Get
         Return m_ExpanderList
      End Get
   End Property

   ' PDF visualizzato come preview nella sezione PDF Editor
   Private m_WebAddress As String
   Public ReadOnly Property WebAddress As String
      Get
         Return m_WebAddress
      End Get
   End Property
   Friend Sub SetWebAddress(value As String)
      m_WebAddress = value
      NotifyPropertyChanged(NameOf(WebAddress))
   End Sub

   Private m_PdfViewer_Visibility As Visibility
   Public ReadOnly Property PdfViewer_Visibility As Visibility
      Get
         Return m_PdfViewer_Visibility
      End Get
   End Property
   Friend Sub SetPdfViewer_Visibility(value As Visibility)
      m_PdfViewer_Visibility = value
      NotifyPropertyChanged(NameOf(PdfViewer_Visibility))
   End Sub

   ' Definizione comandi
   Private m_cmdCreatePDFPreviewVis As ICommand
   Private m_cmdCreatePDFPreviewOtt As ICommand
   Private m_cmdClosePreview As ICommand

#End Region

#Region "CONSTRUCTORS"

   Sub New()
      m_ExpanderList.Add(New ExpanderPDF(ListTypes.MACHGROUPTOT))    '  MachGroup Totals  
      m_ExpanderList.Add(New ExpanderPDF(ListTypes.MACHGROUP))       '  MachGroup List
      m_ExpanderList.Add(New ExpanderPDF(ListTypes.PART_LIST))       '  Raw Part
      m_ExpanderList.Add(New ExpanderPDF(ListTypes.RAWPART))         '  RawPart List
      m_ExpanderList.Add(New ExpanderPDF(ListTypes.PART_TOT))        '  Part Totals
      m_ExpanderList.Add(New ExpanderPDF(ListTypes.PART))            '  Part List
      SetWebAddress("")                                              '  Pagina vuota
      SetPdfViewer_Visibility(Visibility.Hidden)
      Map.SetRefPDFEditorVM(Me)
   End Sub

#End Region ' Constructors

#Region "COMMANDS"

#Region "CreatePreviewVis"

   Public ReadOnly Property CreatePreviewVis_Command As ICommand
      Get
         If m_cmdCreatePDFPreviewVis Is Nothing Then
            m_cmdCreatePDFPreviewVis = New Command(AddressOf CreatePreviewVis)
         End If
         Return m_cmdCreatePDFPreviewVis
      End Get
   End Property

   ''' <summary>
   ''' Funzione per la visualizzazione della preview del PDF della sezione visualizzatore
   ''' </summary>
   Public Sub CreatePreviewVis()
      SetPdfViewer_Visibility(Visibility.Visible)
      Configuration.CreatePrintPDF(PDFPage.VIEW, True)
      SetWebAddress(Map.refMainWindowVM.MainWindowM.sTempDir & "\Statistics.pdf")
   End Sub

#End Region    ' CreatePreviewVis

#Region "CreatePreviewOt"

   Public ReadOnly Property CreatePreviewOtt_Command As ICommand
      Get
         If m_cmdCreatePDFPreviewOtt Is Nothing Then
            m_cmdCreatePDFPreviewOtt = New Command(AddressOf CreatePreviewOtt)
         End If
         Return m_cmdCreatePDFPreviewOtt
      End Get
   End Property

   ''' <summary>
   ''' Funzione per la visualizzazione della preview del PDF della sezione ottimizzatore
   ''' </summary>
   Public Sub CreatePreviewOtt()
      SetPdfViewer_Visibility(Visibility.Visible)
      Configuration.CreatePrintPDF(PDFPage.MACHING, True)
      SetWebAddress(Map.refMainWindowVM.MainWindowM.sTempDir & "\Statistics.pdf")
   End Sub

#End Region    ' CreatePreviewOt

#Region "ClosePreview"

   Public ReadOnly Property ClosePreview_Command As ICommand
      Get
         If m_cmdClosePreview Is Nothing Then
            m_cmdClosePreview = New Command(AddressOf ClosePreview)
         End If
         Return m_cmdClosePreview
      End Get
   End Property

   ''' <summary>
   ''' Funzione per la chiusura della preview
   ''' </summary>
   Public Sub ClosePreview()
      Try
         File.Delete(Map.refMainWindowVM.MainWindowM.sTempDir & "\Statistics.pdf")
      Catch ex As Exception
         EgtOutLog("Errore! Cancellazione dei file pdf fallita")
      End Try

      SetPdfViewer_Visibility(Visibility.Hidden)
      SetWebAddress("")
   End Sub

#End Region    ' CreatePreviewVis

#End Region    '  Commands

End Class

Public Class ExpanderPDF
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   'Nome della categoria MachGroup Totals, MachGroup List, Raw Part e Raw Part List 
   Private m_sNameCategory As String
   Public ReadOnly Property sNameCategory As String
      Get
         Return m_sNameCategory
      End Get
   End Property

   'Lista degli elementi visualizzati nell'expander es Numero di grezzi o le colonne es colName
   Private m_ExpanderElementList As New List(Of ExpanderPDF)
   Public ReadOnly Property ExpanderElementList As List(Of ExpanderPDF)
      Get
         Return m_ExpanderElementList
      End Get
   End Property

#End Region    ' Fields & Properties

#Region "CONTRUCTORS"

   Sub New()
   End Sub

   Sub New(nListType As Integer)
      Select Case nListType
         Case ListTypes.MACHGROUPTOT                                                '  MachGroup Totals
            m_sNameCategory = EgtMsg(61729)                                         '  Titolo Categoria: MachGroup Totals
            m_ExpanderElementList.Add(New ExpanderElement(EgtMsg(61721)))           '  Numero di grezzi
            m_ExpanderElementList.Add(New ExpanderElement(EgtMsg(61717)))           '  Volume totale
            m_ExpanderElementList.Add(New ExpanderElement(EgtMsg(61719)))           '  Tempo totole stimato
            m_ExpanderElementList.Add(New ExpanderElement(EgtMsg(61718)))           '  Tempo totale
            m_ExpanderElementList.Add(New ExpanderElement(EgtMsg(61720)))           '  Tempo rimanente
            m_ExpanderElementList.Add(New ExpanderElement(EgtMsg(61722)))           '  Numero di pezzi da BTL
            m_ExpanderElementList.Add(New ExpanderElement(EgtMsg(61723)))           '  Numero di pezzi aggiunti
            m_ExpanderElementList.Add(New ExpanderElement(EgtMsg(61724)))           '  Numero di pezzi inseriti nei grezzi
            m_ExpanderElementList.Add(New ExpanderElement(EgtMsg(61725)))           '  Numero di pezzi completati
         Case ListTypes.MACHGROUP                                                   '  MachGroup List
            m_sNameCategory = EgtMsg(61730)                                         '  Titolo Categoria: MachGroup List
            m_ExpanderElementList.Add(New ExpanderTable(S_OPTIMIZERSTATISTICS))     '  Tabella MachGroupList
         Case ListTypes.PART_LIST                                                   '  Raw Part
            m_sNameCategory = EgtMsg(61731)                                         '  Titolo Categoria: Raw Part
            m_ExpanderElementList.Add(New ExpanderTable(S_STATISTICS_PART))         '  Tabella RawPart + Sezione e Immagine
         Case ListTypes.RAWPART                                                     '  RawPart List
            m_sNameCategory = EgtMsg(61732)                                         '  Titolo Categoria: RawPart List
            m_ExpanderElementList.Add(New ExpanderTable(S_RAWPARTSTATISTICS))       '  Tabella RawPartList
         Case ListTypes.PART_TOT                                                    '  Part Totals
            m_sNameCategory = EgtMsg(61734)                                         '  Titolo Categoria: Part Totals
            m_ExpanderElementList.Add(New ExpanderElement(EgtMsg(61716)))           '  Numero totale di pezzi
            m_ExpanderElementList.Add(New ExpanderElement(EgtMsg(61717)))           '  Volume totale 
            m_ExpanderElementList.Add(New ExpanderElement(EgtMsg(61718)))           '  Tempo totale 
            m_ExpanderElementList.Add(New ExpanderElement(EgtMsg(61720)))           '  Tempo rimanente
         Case ListTypes.PART                                                        '  Part List
            m_sNameCategory = EgtMsg(61733)                                         '  Titolo Categoria: Part List
            m_ExpanderElementList.Add(New ExpanderTable(S_STATISTICS))              '  Tabella PartList
      End Select
   End Sub

#End Region    ' Constructors

End Class

Public Class ExpanderElement
   Inherits ExpanderPDF

#Region "FIELDS & PROPERTIES"

   'Nome dei parametri visualizzati nella sezione del PDF Machgroup Totals es Numero di grezzi
   Private m_sNameParameter As String
   Public ReadOnly Property sNameParameter As String
      Get
         Return m_sNameParameter
      End Get
   End Property

   'Check Box dei parametri visualizzati nella sezione del PDF Machgroup Totals
   Private m_bParameter_IsChecked As Boolean
   Public Property bParameter_IsChecked As Boolean
      Get
         Return m_bParameter_IsChecked
      End Get
      Set(value As Boolean)
         m_bParameter_IsChecked = value
         NotifyPropertyChanged(NameOf(bParameter_IsChecked))
         WriteMainPrivateProfileString(S_PDFEDITOR, m_sNameParameter, If(m_bParameter_IsChecked, 1, 0))
      End Set
   End Property

#End Region    'Fields & Properties

#Region "CONTRUCTORS"

   Sub New(NameParameter As String)
      m_sNameParameter = NameParameter
      m_bParameter_IsChecked = GetMainPrivateProfileInt(S_PDFEDITOR, m_sNameParameter, 0) <> 0
   End Sub

#End Region    'Constructors

End Class

Public Class ExpanderTable
   Inherits ExpanderPDF

#Region "FIELDS & PROPERTIES"

#Region "RAWPARTCOLUMNS"

   'Lista Colonne Tabella MachGroup List, RawPart List, PartList
   Private m_RawPartColumns As New ObservableCollection(Of EgtDataGridColumn)
   Public ReadOnly Property RawPartColumns As ObservableCollection(Of EgtDataGridColumn)
      Get
         Return m_RawPartColumns
      End Get
   End Property

   'Elemento delle Liste MachGroup List, RawPart List, PartList selezionato es colName
   Private m_SelColumns As EgtDataGridColumn
   Public Property SelColumns As EgtDataGridColumn
      Get
         Return m_SelColumns
      End Get
      Set(value As EgtDataGridColumn)
         m_SelColumns = value
      End Set
   End Property

#End Region    ' RawPartColumns

#Region "SECTION & IMAGE"

   ' Visualizza Sezione ed Immagine solo se fa parte di DG_Statistics_PART
   Private m_vRawPartVisibility As Visibility
   Public ReadOnly Property vRawPartVisibility As Visibility
      Get
         Return m_vRawPartVisibility
      End Get
   End Property

   ' Check Box per la visualizzazione nel PDF della sezione
   Private m_bSection_IsChecked As Boolean
   Public Property bSection_IsChecked As Boolean
      Get
         Return m_bSection_IsChecked
      End Get
      Set(value As Boolean)
         m_bSection_IsChecked = value
         NotifyPropertyChanged(NameOf(bSection_IsChecked))
         WriteMainPrivateProfileString(S_PDFEDITOR, S_SECTION, If(m_bSection_IsChecked, 1, 0))
      End Set
   End Property

   ' Check Box per la visualizzazione nel PDF dell' immagine
   Private m_bImage_IsChecked As Boolean
   Public Property bImage_IsChecked As Boolean
      Get
         Return m_bImage_IsChecked
      End Get
      Set(value As Boolean)
         m_bImage_IsChecked = value
         NotifyPropertyChanged(NameOf(bImage_IsChecked))
         WriteMainPrivateProfileString(S_PDFEDITOR, S_IMAGE, If(m_bImage_IsChecked, 1, 0))
      End Set
   End Property

#End Region    ' Section & Image

#End Region    ' FIELDS & PROPERTIES

#Region "MESSAGES"

   Public ReadOnly Property Section_Msg As String
      Get
         Return EgtMsg(61735).ToUpper()
      End Get
   End Property

   Public ReadOnly Property Image_Msg As String
      Get
         Return EgtMsg(61736).ToUpper()
      End Get
   End Property

#End Region    ' Messages

   ' Definizione comandi
   Private m_cmdMoveUpOrder As ICommand
   Private m_cmdMoveDownOrder As ICommand

#Region "CONTRUCTORS"

   Sub New(TypeTable As String)
      GetPrivateProfileColumns(TypeTable, m_RawPartColumns)
      m_vRawPartVisibility = If(TypeTable = S_STATISTICS_PART, Visibility.Visible, Visibility.Collapsed)
      m_bSection_IsChecked = GetMainPrivateProfileInt(S_PDFEDITOR, S_SECTION, 0) <> 0
      m_bImage_IsChecked = GetMainPrivateProfileInt(S_PDFEDITOR, S_IMAGE, 0) <> 0
   End Sub

#End Region    ' Constructors

#Region "COMMANDS"

#Region "MoveUpOrder"

   Public ReadOnly Property MoveUpOrder_Command As ICommand
      Get
         If m_cmdMoveUpOrder Is Nothing Then
            m_cmdMoveUpOrder = New Command(AddressOf MoveUpOrder)
         End If
         Return m_cmdMoveUpOrder
      End Get
   End Property

   ''' <summary>
   ''' Funzione per spostare in alto la colonna
   ''' </summary>
   Public Sub MoveUpOrder()
      If IsNothing(m_RawPartColumns) Then Return
      Dim nIndex As Integer = m_RawPartColumns.IndexOf(m_SelColumns)
      If nIndex < 0 Then Return
      If nIndex > 0 Then m_RawPartColumns.Move(nIndex, nIndex - 1)
   End Sub

#End Region    ' MoveUpOrder

#Region "MoveDownOrder"

   Public ReadOnly Property MoveDownOrder_Command As ICommand
      Get
         If m_cmdMoveDownOrder Is Nothing Then
            m_cmdMoveDownOrder = New Command(AddressOf MoveDownOrder)
         End If
         Return m_cmdMoveDownOrder
      End Get
   End Property

   ''' <summary>
   ''' Funzione per spostare in basso la colonna
   ''' </summary>
   Public Sub MoveDownOrder()
      If IsNothing(m_RawPartColumns) Then Return
      Dim nIndex As Integer = m_RawPartColumns.IndexOf(m_SelColumns)
      If nIndex < 0 Then Return
      If nIndex < m_RawPartColumns.Count - 1 Then m_RawPartColumns.Move(nIndex, nIndex + 1)
   End Sub

#End Region    ' MoveDownOrder

#End Region    ' Commands

End Class

Class ColumnNameConverter
   Implements IValueConverter

   Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
      Dim ColName As String = ""
      If TypeOf value Is String Then
         ColName = CStr(value)
         Select Case value
            Case COL_PDN
               Return Statistic_Messages.PDN_Msg
            Case COL_W
               Return Statistic_Messages.W_Msg
            Case COL_H
               Return Statistic_Messages.H_Msg
            Case COL_L
               Return Statistic_Messages.L_Msg
            Case COL_DESC
               Return Statistic_Messages.NAM_Msg
            Case COL_NAME
               Return Statistic_Messages.NAM_Msg
            Case COL_MATERIAL
               Return Statistic_Messages.MAT_Msg
            Case COL_CNT
               Return Statistic_Messages.CNT_Msg
            Case COL_ADDED
               Return Statistic_Messages.ADDED_Msg
            Case COL_DONE
               Return Statistic_Messages.DONE_Msg
            Case COL_USAGE
               Return Statistic_Messages.Used_Msg
            Case COL_WASTE
               Return Statistic_Messages.Waste_Msg
            Case COL_UNITVOLUME
               Return Statistic_Messages.UNIT_VOLUME_Msg
            Case COL_TOTVOLUME
               Return Statistic_Messages.TOT_VOLUME_Msg
            Case COL_UNITTIME
               Return Statistic_Messages.UNIT_TIME_Msg
            Case COL_TOTTIME
               Return Statistic_Messages.TOT_TIME_Msg
            Case COL_QTY
               Return Statistic_Messages.QTY_Msg
            Case COL_INPROD
               Return Statistic_Messages.INPROD_Msg
            Case Else
               Return Nothing
         End Select
      Else
         Return Nothing
      End If
   End Function

   Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
      Throw New NotImplementedException()
   End Function
End Class