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"

   Friend Event m_CloseWindow(bDialogResult As Boolean)

   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
   Private m_cmdOk As ICommand

#Region "Messages"

   Public ReadOnly Property PDN_Msg As String
      Get
         Return Statistic_Messages.PDN_Msg
      End Get
   End Property

   Public ReadOnly Property ID_Msg As String
      Get
         Return Statistic_Messages.ID_Msg
      End Get
   End Property

   Public ReadOnly Property DO_Msg As String
      Get
         Return Statistic_Messages.DO_Msg
      End Get
   End Property

   Public ReadOnly Property W_Msg As String
      Get
         Return Statistic_Messages.W_Msg
      End Get
   End Property

   Public ReadOnly Property H_Msg As String
      Get
         Return Statistic_Messages.H_Msg
      End Get
   End Property

   Public ReadOnly Property L_Msg As String
      Get
         Return Statistic_Messages.L_Msg
      End Get
   End Property

   Public ReadOnly Property NAM_Msg As String
      Get
         Return Statistic_Messages.NAM_Msg
      End Get
   End Property

   Public ReadOnly Property MAT_Msg As String
      Get
         Return Statistic_Messages.MAT_Msg
      End Get
   End Property

   Public ReadOnly Property CNT_Msg As String
      Get
         Return Statistic_Messages.CNT_Msg
      End Get
   End Property

   Public ReadOnly Property ADDED_Msg As String
      Get
         Return Statistic_Messages.ADDED_Msg
      End Get
   End Property

   Public ReadOnly Property INPROD_Msg As String
      Get
         Return Statistic_Messages.INPROD_Msg
      End Get
   End Property

   Public ReadOnly Property DONE_Msg As String
      Get
         Return Statistic_Messages.DONE_Msg
      End Get
   End Property

   Public ReadOnly Property GRP_Msg As String
      Get
         Return Statistic_Messages.GRP_Msg
      End Get
   End Property

   Public ReadOnly Property UNIT_VOLUME_Msg As String
      Get
         Return Statistic_Messages.UNIT_VOLUME_Msg
      End Get
   End Property

   Public ReadOnly Property TOT_VOLUME_Msg As String
      Get
         Return Statistic_Messages.TOT_VOLUME_Msg
      End Get
   End Property

   Public ReadOnly Property UNIT_TIME_Msg As String
      Get
         Return Statistic_Messages.UNIT_TIME_Msg
      End Get
   End Property

   Public ReadOnly Property TOT_TIME_Msg As String
      Get
         Return Statistic_Messages.TOT_TIME_Msg
      End Get
   End Property

   Public ReadOnly Property BTLTotParts_Msg As String
      Get
         Return Statistic_Messages.BTLTotParts_Msg
      End Get
   End Property

   Public ReadOnly Property BTLTotVolume_Msg As String
      Get
         Return Statistic_Messages.BTLTotVolume_Msg
      End Get
   End Property

   Public ReadOnly Property BTLTotEstimatedTime_Msg As String
      Get
         Return Statistic_Messages.BTLTotEstimatedTime_Msg
      End Get
   End Property

   Public ReadOnly Property BTLTotTime_Msg As String
      Get
         Return Statistic_Messages.BTLTotTime_Msg
      End Get
   End Property

   Public ReadOnly Property BTLRemainingTime_Msg As String
      Get
         Return Statistic_Messages.BTLRemainingTime_Msg
      End Get
   End Property

   Public ReadOnly Property TotMachGroups_Msg As String
      Get
         Return Statistic_Messages.TotMachGroups_Msg
      End Get
   End Property

   Public ReadOnly Property TotCNTParts_Msg As String
      Get
         Return Statistic_Messages.TotCNTParts_Msg
      End Get
   End Property

   Public ReadOnly Property TotADDEDParts_Msg As String
      Get
         Return Statistic_Messages.TotADDEDParts_Msg
      End Get
   End Property

   Public ReadOnly Property TotINPRODParts_Msg As String
      Get
         Return Statistic_Messages.TotINPRODParts_Msg
      End Get
   End Property

   Public ReadOnly Property TotDONEParts_Msg As String
      Get
         Return Statistic_Messages.TotDONEParts_Msg
      End Get
   End Property

   Public ReadOnly Property Used_Msg As String
      Get
         Return Statistic_Messages.Used_Msg
      End Get
   End Property

   Public ReadOnly Property Waste_Msg As String
      Get
         Return Statistic_Messages.Waste_Msg
      End Get
   End Property

   Public ReadOnly Property QTY_Msg As String
      Get
         Return Statistic_Messages.QTY_Msg
      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 ProjectManagerVM.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

   Public Sub CreatePrintPDF(ActivePage As Integer, bPreview As Boolean)
      LoadingWndHelper.OpenLoadingWnd(ActiveIds.CREATINGPDF, 1, "Creating PDF", "", 100)
      Try
         ' creo documento MigraDoc
         Dim document As Document = PDFHelper.CreateStatReport(ActivePage, bPreview)
         Dim documentPath As String = Map.refMainWindowVM.MainWindowM.sTempDir & "\MigraDoc.mdddl"
         MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(document, documentPath)
         Dim renderer As New PdfDocumentRenderer(True) With {
            .Document = document
         }
         renderer.RenderDocument()
         ' salvo il documento
         Dim sFileName As String = ""

         If (ActivePage = Pages.VIEW AndAlso Not bPreview) OrElse (ActivePage = Pages.ONLYPRODPAGE AndAlso Not Map.refInstrumentPanelVM.bVisOtt_Selected AndAlso Not bPreview) Then
            sFileName = ProjectManagerVM.CurrProj.sProjDirPath & "\" & "Statistics.pdf"
         ElseIf ActivePage = Pages.VIEW OrElse Pages.ONLYPRODPAGE AndAlso bPreview Then
            sFileName = Map.refMainWindowVM.MainWindowM.sTempDir & "\Statistics.pdf"  ' Se la pagina e' 0 e' bPreview e' True creo PDF Visualization
         ElseIf (ActivePage = Pages.MACHINING AndAlso Not bPreview) OrElse (ActivePage = Pages.ONLYPRODPAGE AndAlso Map.refInstrumentPanelVM.bVisOtt_Selected AndAlso Not bPreview) Then
            sFileName = ProjectManagerVM.CurrProd.sProdDirPath & "\" & ProjectManagerVM.CurrProd.sProdId & " - " & "Statistics.pdf"
         ElseIf ActivePage = Pages.MACHINING OrElse Pages.ONLYPRODPAGE AndAlso bPreview Then ' Se la pagina e' 1 e' bPreview e' True creo PDF Optimizer
            sFileName = Map.refMainWindowVM.MainWindowM.sTempDir & "\Statistics.pdf"
            If Not bOnlyProd Then PrevSelect()
         End If

         ' avvio il visualizzatore 
         renderer.PdfDocument.Save(sFileName)
         ' cancello file MigraDoc
         File.Delete(documentPath)

         If bPreview Then
         Else
            Process.Start(sFileName)
         End If
      Catch ex As Exception
         EgtOutLog("Error genereting pdf: " & ex.ToString())
      End Try
      LoadingWndHelper.CloseLoadingWnd(ActiveIds.CREATINGPDF)

   End Sub

   Private Shared Sub PrevSelect()
      ' 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
      Core.ViewPanelVM.BWSetView(If(Core.ViewPanelVM.Type = BWType.BEAM, VT.ISO_SW, VT.TOP), False)
      EgtZoom(ZM.ALL, True)
   End Sub

#End Region ' PrintPDF

#Region "COMMANDS"

#Region "Ok"

   Public ReadOnly Property Ok_Command As ICommand
      Get
         If m_cmdOk Is Nothing Then
            m_cmdOk = New Command(AddressOf Ok)
         End If
         Return m_cmdOk
      End Get
   End Property

   Public Sub Ok()
      RaiseEvent m_CloseWindow(True)
   End Sub

#End Region ' Ok

#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()
      Configuration.CreatePrintPDF(Map.refMainMenuVM.SelPage, False)
   End Sub

#End Region ' PrintPDF

#End Region    ' COMMANDS

End Class
