Imports System.Collections.ObjectModel

Public Module StatisticsUtility

   Public Sub CopyToClipboard(dgOptimizerStatistics As DataGrid, dgRawPartStatistics As DataGrid, StatisticsVM As StatisticsVM)
      If Map.refStatisticsVM.bRawPart_IsChecked Then
         ExportToExcelAndCsv(dgRawPartStatistics, StatisticsVM)
      Else
         ExportToExcelAndCsv(dgOptimizerStatistics, StatisticsVM)
      End If
   End Sub

   Public Sub ExportToExcelAndCsv(dgDataGrid As DataGrid, StatisticsVM As StatisticsVM)
      ' imposto gli header
      For Each DataGridColumn In dgDataGrid.Columns
         If IsNothing(DataGridColumn) OrElse IsNothing(DataGridColumn.HeaderTemplate) Then Continue For
         ' recupero il contenuto del HeaderTemplate
         Dim DataGridColumnHeaderFE As FrameworkElement = DataGridColumn.HeaderTemplate.LoadContent()
         ' recupero la stringa di binding
         Dim DataGridColumnHeaderBindingText = DataGridColumnHeaderFE.GetBindingExpression(TextBlock.TextProperty).ParentBinding.Path.Path()
         ' applico la stessa proprieta' del binding
         Select Case DataGridColumnHeaderBindingText
            Case "DataContext.PDN_Msg"
               DataGridColumn.Header = StatisticsVM.PDN_Msg
            Case "DataContext.ID_Msg"
               DataGridColumn.Header = StatisticsVM.ID_Msg
            Case "DataContext.W_Msg"
               DataGridColumn.Header = StatisticsVM.W_Msg
            Case "DataContext.H_Msg"
               DataGridColumn.Header = StatisticsVM.H_Msg
            Case "DataContext.L_Msg"
               DataGridColumn.Header = StatisticsVM.L_Msg
            Case "DataContext.NAM_Msg"
               DataGridColumn.Header = StatisticsVM.NAM_Msg
            Case "DataContext.MAT_Msg"
               DataGridColumn.Header = StatisticsVM.MAT_Msg
            Case "DataContext.CNT_Msg"
               DataGridColumn.Header = StatisticsVM.CNT_Msg
            Case "DataContext.ADDED_Msg"
               DataGridColumn.Header = StatisticsVM.ADDED_Msg
            Case "DataContext.INPROD_Msg"
               DataGridColumn.Header = StatisticsVM.INPROD_Msg
            Case "DataContext.DONE_Msg"
               DataGridColumn.Header = StatisticsVM.DONE_Msg
            Case "DataContext.Used_Msg"
               DataGridColumn.Header = StatisticsVM.Used_Msg
            Case "DataContext.Waste_Msg"
               DataGridColumn.Header = StatisticsVM.Waste_Msg
            Case "DataContext.UNIT_VOLUME_Msg"
               DataGridColumn.Header = StatisticsVM.UNIT_VOLUME_Msg
            Case "DataContext.TOT_VOLUME_Msg"
               DataGridColumn.Header = StatisticsVM.TOT_VOLUME_Msg
            Case "DataContext.UNIT_TIME_Msg"
               DataGridColumn.Header = StatisticsVM.UNIT_TIME_Msg
            Case "DataContext.TOT_TIME_Msg"
               DataGridColumn.Header = StatisticsVM.TOT_TIME_Msg
            Case "DataContext.QTY_Msg"
               DataGridColumn.Header = StatisticsVM.QTY_Msg
         End Select
      Next
      'Dim x = DirectCast(dgDataGrid.ItemsSource, ObservableCollection(Of BTLPartVM))
      'Dim TotRow As Core.BTLPartM = Core.BTLPartM.CreateNewBTLPart()
      'TotRow.m_sNAM = "Totale"
      'TotRow.m_dBtlW = 1234.567
      'Dim TotRowVm As BTLPartVM = New BTLPartVM(TotRow, Nothing)
      'x.Add(TotRowVm)
      dgDataGrid.SelectAllCells()
      dgDataGrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader
      Try
         ApplicationCommands.Copy.Execute(Nothing, dgDataGrid)
      Catch ex As Exception
         Map.refMyStatusBarVM.SetOutputMessage("Copy to clipboard impossible", 3, EgtWPFLib5.MSG_TYPE.INFO)
      End Try
      'x.Remove(TotRowVm)
      Map.refMyStatusBarVM.SetOutputMessage("Copied to clipboard", 3, EgtWPFLib5.MSG_TYPE.INFO)
      dgDataGrid.UnselectAllCells()
   End Sub

End Module