Imports System.Collections.ObjectModel
Imports System.IO
Imports EgtBEAMWALL.Core
Imports EgtUILib
Imports EgtWPFLib5
Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.Rendering

Public Class StatisticsVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Public Enum StatisticsTab As Integer
      OPTIMIZER = 0
      RAWPART = 1
   End Enum

   Private m_SelStatisticsTab As StatisticsTab
   Public Property SelStatisticsTab As Integer
      Get
         Return m_SelStatisticsTab
      End Get
      Set(value As Integer)
         m_SelStatisticsTab = value
      End Set
   End Property

   Private m_bRawPart_IsChecked As Boolean
   Public Property bRawPart_IsChecked As Boolean
      Get
         Return m_bRawPart_IsChecked
      End Get
      Set(value As Boolean)
         m_bRawPart_IsChecked = value
         If value Then
            m_SelStatisticsTab = StatisticsTab.RAWPART
         Else
            m_SelStatisticsTab = StatisticsTab.OPTIMIZER
         End If
         NotifyPropertyChanged(NameOf(SelStatisticsTab))
      End Set
   End Property

   Private m_StatisticsColumns As New ObservableCollection(Of EgtDataGridColumn)
   Public Property StatisticsColumns As ObservableCollection(Of EgtDataGridColumn)
      Get
         Return m_StatisticsColumns
      End Get
      Set(value As ObservableCollection(Of EgtDataGridColumn))
         m_StatisticsColumns = value
      End Set
   End Property

   Private m_OptimizerStatisticsColumns As New ObservableCollection(Of EgtDataGridColumn)
   Public Property OptimizerStatisticsColumns As ObservableCollection(Of EgtDataGridColumn)
      Get
         Return m_OptimizerStatisticsColumns
      End Get
      Set(value As ObservableCollection(Of EgtDataGridColumn))
         m_OptimizerStatisticsColumns = value
      End Set
   End Property

   Private m_RawPartStatisticsColumns As New ObservableCollection(Of EgtDataGridColumn)
   Public Property RawPartStatisticsColumns As ObservableCollection(Of EgtDataGridColumn)
      Get
         Return m_RawPartStatisticsColumns
      End Get
      Set(value As ObservableCollection(Of EgtDataGridColumn))
         m_RawPartStatisticsColumns = value
      End Set
   End Property

   Private m_RawPartList As New ObservableCollection(Of SParam)
   Public ReadOnly Property RawPartList As ObservableCollection(Of SParam)
      Get
         Return m_RawPartList
      End Get
   End Property

   ' Definizione comandi
   Private m_cmdPrintPDF As ICommand

#Region "Messages"

   Public ReadOnly Property PDN_Msg As String
      Get
         Return EgtMsg(61809)
      End Get
   End Property

   Public ReadOnly Property ID_Msg As String
      Get
         Return EgtMsg(61809)
      End Get
   End Property

   Public ReadOnly Property DO_Msg As String
      Get
         Return EgtMsg(61810)
      End Get
   End Property

   Public ReadOnly Property W_Msg As String
      Get
         Return EgtMsg(61605)
      End Get
   End Property

   Public ReadOnly Property H_Msg As String
      Get
         Return EgtMsg(61606)
      End Get
   End Property

   Public ReadOnly Property L_Msg As String
      Get
         Return EgtMsg(61604)
      End Get
   End Property

   Public ReadOnly Property NAM_Msg As String
      Get
         Return EgtMsg(61603)
      End Get
   End Property

   Public ReadOnly Property MAT_Msg As String
      Get
         Return EgtMsg(61607)
      End Get
   End Property

   Public ReadOnly Property CNT_Msg As String
      Get
         Return EgtMsg(61608)
      End Get
   End Property

   Public ReadOnly Property ADDED_Msg As String
      Get
         Return EgtMsg(61813)
      End Get
   End Property
   Public ReadOnly Property INPROD_Msg As String
      Get
         Return EgtMsg(61609)
      End Get
   End Property
   Public ReadOnly Property DONE_Msg As String
      Get
         Return EgtMsg(61814)
      End Get
   End Property

   Public ReadOnly Property GRP_Msg As String
      Get
         Return EgtMsg(61612)
      End Get
   End Property

   Public ReadOnly Property UNIT_VOLUME_Msg As String
      Get
         Return EgtMsg(61712)
      End Get
   End Property

   Public ReadOnly Property TOT_VOLUME_Msg As String
      Get
         Return EgtMsg(61713)
      End Get
   End Property

   Public ReadOnly Property UNIT_TIME_Msg As String
      Get
         Return EgtMsg(61714)
      End Get
   End Property

   Public ReadOnly Property TOT_TIME_Msg As String
      Get
         Return EgtMsg(61715)
      End Get
   End Property

   Public ReadOnly Property BTLTotParts_Msg As String
      Get
         Return EgtMsg(61716) & ": "
      End Get
   End Property

   Public ReadOnly Property BTLTotVolume_Msg As String
      Get
         Return EgtMsg(61717) & ": "
      End Get
   End Property

   Public ReadOnly Property BTLTotEstimatedTime_Msg As String
      Get
         Return EgtMsg(61719) & ": "
      End Get
   End Property

   Public ReadOnly Property BTLTotTime_Msg As String
      Get
         Return EgtMsg(61718) & ": "
      End Get
   End Property

   Public ReadOnly Property BTLRemainingTime_Msg As String
      Get
         Return EgtMsg(61720) & ": "
      End Get
   End Property

   Public ReadOnly Property TotMachGroups_Msg As String
      Get
         Return EgtMsg(61721) & ": "
      End Get
   End Property

   Public ReadOnly Property TotCNTParts_Msg As String
      Get
         Return EgtMsg(61722) & ": "
      End Get
   End Property

   Public ReadOnly Property TotADDEDParts_Msg As String
      Get
         Return EgtMsg(61723) & ": "
      End Get
   End Property

   Public ReadOnly Property TotINPRODParts_Msg As String
      Get
         Return EgtMsg(61724) & ": "
      End Get
   End Property

   Public ReadOnly Property TotDONEParts_Msg As String
      Get
         Return EgtMsg(61725) & ": "
      End Get
   End Property

   Public ReadOnly Property Used_Msg As String
      Get
         Return EgtMsg(61817)
      End Get
   End Property

   Public ReadOnly Property Waste_Msg As String
      Get
         Return EgtMsg(61818)
      End Get
   End Property

   Public ReadOnly Property QTY_Msg As String
      Get
         Return EgtMsg(61726)
      End Get
   End Property

   Public ReadOnly Property CopyToClipboard_Msg As String
      Get
         Return EgtMsg(61841)
      End Get
   End Property

   Public ReadOnly Property PrintPDF_Msg As String
      Get
         Return EgtMsg(61727)
      End Get
   End Property

#End Region    ' Messages

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Sub New()
      ' Aggiungo riferimento a Map
      Map.SetRefStatisticsVM(Me)
      ' carico le colonne della datagrid
      GetPrivateProfileColumns(S_STATISTICS, StatisticsColumns)
      GetPrivateProfileColumns(S_OPTIMIZERSTATISTICS, OptimizerStatisticsColumns)
      GetPrivateProfileColumns(S_RAWPARTSTATISTICS, RawPartStatisticsColumns)
   End Sub

#End Region    ' CONSTRUCTOR

#Region "METHODS"

   Friend Sub LoadRawPart()
      m_RawPartList.Clear()
      Select Case Map.refProdManagerVM.CurrProd.nType
         Case BWType.BEAM
            For Each MachGroup As MyMachGroupVM In Map.refProjectVM.MachGroupPanelVM.MachGroupVMList
               Dim RawPart As SParam = m_RawPartList.FirstOrDefault(Function(x) x.SectXMat.dW = MachGroup.dW AndAlso x.SectXMat.dH = MachGroup.dH AndAlso x.dL = MachGroup.dL AndAlso x.SectXMat.sMaterial.Any(Function(y) MachGroup.Section.sMaterial.Any(Function(z) z = y)))
               If Not IsNothing(RawPart) Then
                  RawPart.nQuantity += 1
               Else
                  m_RawPartList.Add(New SParam(MachGroup.Section, MachGroup.dL, 1, True))
               End If
            Next
         Case BWType.WALL
            For Each MachGroup As MyMachGroupVM In Map.refProjectVM.MachGroupPanelVM.MachGroupVMList
               Dim RawPart As SParam = m_RawPartList.FirstOrDefault(Function(x) x.dW = MachGroup.dW AndAlso x.SectXMat.dH = MachGroup.dH AndAlso x.dL = MachGroup.dL AndAlso x.SectXMat.sMaterial.Any(Function(y) MachGroup.Section.sMaterial.Any(Function(z) z = y)))
               If Not IsNothing(RawPart) Then
                  RawPart.nQuantity += 1
               Else
                  m_RawPartList.Add(New SParam(MachGroup.Section, MachGroup.dW, MachGroup.dL, 1, True))
               End If
            Next
      End Select
   End Sub

#End Region    ' METHODS

#Region "COMMANDS"

#Region "PrintPDF"

   Public ReadOnly Property PrintPDF_Command As ICommand
      Get
         If m_cmdPrintPDF Is Nothing Then
            m_cmdPrintPDF = New Command(AddressOf PrintPDF)
         End If
         Return m_cmdPrintPDF
      End Get
   End Property

   Public Sub PrintPDF()
      LoadingWndHelper.OpenLoadingWnd(ActiveIds.CREATINGPDF, 1, "Creating PDF", "", 100)
      Try
         ' creo documento MigraDoc
         Dim document As Document = PDFHelper.CreateStatReport(Map.refMainMenuVM.SelPage)
         Dim documentPath As String = Map.refMainWindowVM.MainWindowM.sTempDir & "\MigraDoc.mdddl"
         MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(document, documentPath)
         Dim renderer As PdfDocumentRenderer = New PdfDocumentRenderer(True)
         renderer.Document = document
         renderer.RenderDocument()
         ' salvo il documento
         Dim sFileName As String = Map.refMainWindowVM.MainWindowM.sTempDir & "\Statistics.pdf"
         Select Case Map.refMainMenuVM.SelPage
            Case Pages.VIEW
               sFileName = Map.refProjManagerVM.CurrProj.sProjDirPath & "\" & "Statistics.pdf"
            Case Pages.MACHINING
               sFileName = Map.refProdManagerVM.CurrProd.sProdDirPath & "\" & Map.refProdManagerVM.CurrProd.sProdId & " - " & "Statistics.pdf"
               ' ripristino selezione precedente
               Dim nCurrMachGroupId As Integer = EgtGetCurrMachGroup()
               If Not IsNothing(Map.refProjectVM.MachGroupPanelVM.SelectedMachGroup) AndAlso nCurrMachGroupId <> Map.refProjectVM.MachGroupPanelVM.SelectedMachGroup.Id Then
                  EgtSetCurrMachGroup(Map.refProjectVM.MachGroupPanelVM.SelectedMachGroup.Id)
               End If
               'If Map.refProjectVM.BTLStructureVM.nPROJTYPE = BWType.BEAM Then
               '   EgtSetView(VT.ISO_SW, False)
               'Else
               '   EgtSetView(VT.TOP, False)
               'End If
               Core.ViewPanelVM.BWSetView(If(Core.ViewPanelVM.Type = BWType.BEAM, VT.ISO_SW, VT.TOP), False)
               EgtZoom(ZM.ALL, True)
         End Select

         renderer.PdfDocument.Save(sFileName)
         ' cancello file MigraDoc
         File.Delete(documentPath)
         ' avvio il visualizzatore
         Process.Start(sFileName)
      Catch ex As Exception
         EgtOutLog("Error genereting pdf: " & ex.ToString())
      End Try
      LoadingWndHelper.CloseLoadingWnd(ActiveIds.CREATINGPDF)

   End Sub

#End Region ' PrintPDF

#End Region    ' COMMANDS

End Class
