Imports EgtBEAMWALL.Core
Imports Mysqlx
Imports Org.BouncyCastle.Crypto
Imports System.ComponentModel

Public Class RawPartListV

   Sub New()

      ' This call is required by the designer.
      InitializeComponent()

      ' Add any initialization after the InitializeComponent() call.
      MyComparer.SetDataGrid(MachGroupDataGrid)
      AddHandler MachGroupDataGrid.ResetSort, AddressOf MachGroupDataGrid_ResetSort
   End Sub

   ' funzione che permette di selezionare un RawPart anche quando gia' selezionato
   Private Sub RawPartRow_PreviewMouseDown(sender As Object, e As MouseButtonEventArgs)
      If TypeOf sender Is DataGridRow Then
         Dim Row As DataGridRow = DirectCast(sender, DataGridRow)
         Dim RowVM As MyMachGroupVM = DirectCast(Row.DataContext, MyMachGroupVM)
         ' se gia' selezionato rifaccio selezione perche' potrbbe essere selezionato un BTLPart
         If Not IsNothing(Row) AndAlso Row.IsSelected Then
            Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup = Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup
         End If
      End If
   End Sub

   Private Sub MachGroupVMList_Sorting(sender As Object, e As DataGridSortingEventArgs)
      ' aggiorno direziona colonna
      Dim column As DataGridColumn = e.Column
      column.SortDirection = If((column.SortDirection <> ListSortDirection.Ascending), ListSortDirection.Ascending, ListSortDirection.Descending)
      ' ricalcolo colonne sort
      Dim datagrid As DataGrid = sender
      MyComparer.RefreshColumns()
      ' lancio sort custom
      Map.refSupervisorMachGroupPanelVM.RefreshMachGroupVMSort()
      e.Handled = True
   End Sub

   'Private Sub MachGroupVMList_Sorting(sender As Object, e As DataGridSortingEventArgs)
   '   Dim column As DataGridColumn = e.Column
   '   Dim datagrid As DataGrid = sender
   '   Dim comparer As IComparer = Nothing
   '   e.Handled = True
   '   Dim direction As ListSortDirection = If((column.SortDirection <> ListSortDirection.Ascending), ListSortDirection.Ascending, ListSortDirection.Descending)
   '   column.SortDirection = direction
   '   Dim m_ColumnList As New List(Of DataGridColumn)
   '   Dim m_EgtColumnList As New List(Of EgtDataGridColumn)
   '   For Each col In datagrid.Columns
   '      If Not IsNothing(col.SortDirection) Then
   '         m_ColumnList.Add(col)
   '         m_EgtColumnList.Add(Map.refRawPartListVM.RawPartColumns(col.DisplayIndex))
   '      End If
   '   Next
   '   Dim lcv As ListCollectionView = CType(CollectionViewSource.GetDefaultView(datagrid.ItemsSource), ListCollectionView)
   '   comparer = New MyComparer(Map.refRawPartListVM.RawPartColumns(column.DisplayIndex), m_ColumnList, m_EgtColumnList, direction)
   '   lcv.CustomSort = comparer
   'End Sub

   Private Sub MachGroupDataGrid_ResetSort()
      MyComparer.RefreshColumns()
      Map.refSupervisorMachGroupPanelVM.RefreshMachGroupVMSort()
   End Sub

End Class

Friend Class MyComparer
   Implements IComparer

   Private Shared DataGrid As DataGrid
   Friend Shared Sub SetDataGrid(value As DataGrid)
      DataGrid = value
   End Sub

   Private Shared m_ColumnList As New List(Of DataGridColumn)
   Private Shared m_EgtColumnList As New List(Of EgtDataGridColumn)

   Friend Shared Sub RefreshColumns()
      m_ColumnList.Clear()
      m_EgtColumnList.Clear()
      For Each col In DataGrid.Columns
         If Not IsNothing(col.SortDirection) Then
            m_ColumnList.Add(col)
            m_EgtColumnList.Add(Map.refRawPartListVM.RawPartColumns(col.DisplayIndex))
         End If
      Next
      Map.refRawPartListVM.SetRawPartListSorted(m_ColumnList.Count > 0)
   End Sub


   Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
      Dim First As MyMachGroupVM = DirectCast(x, MyMachGroupVM)
      Dim Second As MyMachGroupVM = DirectCast(y, MyMachGroupVM)
      Dim bFirstProduced As Boolean = First.nProduction_State = Core.ItemState.WIP OrElse First.nProduction_State = Core.ItemState.Produced
      Dim bSecondProduced As Boolean = Second.nProduction_State = Core.ItemState.WIP OrElse Second.nProduction_State = Core.ItemState.Produced
      Dim bFirstInQueue As Boolean = First.bToBeProduced
      Dim bSecondInQueue As Boolean = Second.bToBeProduced
      ' verifico se gia' prodotti
      If bFirstProduced AndAlso bSecondProduced Then
         If First.nProductionIndex > Second.nProductionIndex Then
            Return 1
         ElseIf First.nProductionIndex < Second.nProductionIndex Then
            Return -1
         Else
            Return 0
         End If
      ElseIf bFirstProduced Then
         Return -1
      ElseIf bSecondProduced Then
         Return 1
      ElseIf bFirstInQueue AndAlso bSecondInQueue Then
         If First.nProductionIndex > Second.nProductionIndex Then
            Return 1
         ElseIf First.nProductionIndex < Second.nProductionIndex Then
            Return -1
         Else
            Return 0
         End If
      ElseIf bFirstInQueue Then
         Return -1
      ElseIf bSecondInQueue Then
         Return 1
      Else
         ' verifico ordine per colonne
         Dim ColumnIndex = 0
         While ColumnIndex <= m_EgtColumnList.Count - 1
            Dim nDir As ListSortDirection = If(m_ColumnList(ColumnIndex).SortDirection, 1, -1)
            Dim nCheck As Integer = Check(First, Second, nDir, m_EgtColumnList(ColumnIndex))
            If nCheck <> 0 Then
               Return nCheck
            End If
            ColumnIndex += 1
         End While
         ' se non trovo colonne, metto in ordine indice di produzione
         If First.nProductionIndex > Second.nProductionIndex Then
            Return 1
         ElseIf First.nProductionIndex < Second.nProductionIndex Then
            Return -1
         Else
            Return 0
         End If
      End If
   End Function

   Private Function Check(First As MyMachGroupVM, Second As MyMachGroupVM, nDirection As Integer, Column As EgtDataGridColumn) As Integer
      Select Case Column.Name
         Case COL_NAME
            If First.nName > Second.nName Then
               Return 1 * nDirection
            ElseIf First.nName < Second.nName Then
               Return -1 * nDirection
            Else
               Return 0
            End If
         Case COL_STARTCUT
            If First.Id > Second.Id Then
               Return 1 * nDirection
            ElseIf First.Id < Second.Id Then
               Return -1 * nDirection
            Else
               Return 0
            End If
         Case COL_W
            If First.dW > Second.dW Then
               Return 1 * nDirection
            ElseIf First.dW < Second.dW Then
               Return -1 * nDirection
            Else
               Return 0
            End If
         Case COL_H
            If First.dH > Second.dH Then
               Return 1 * nDirection
            ElseIf First.dH < Second.dH Then
               Return -1 * nDirection
            Else
               Return 0
            End If
         Case COL_L
            If First.dL > Second.dL Then
               Return 1 * nDirection
            ElseIf First.dL < Second.dL Then
               Return -1 * nDirection
            Else
               Return 0
            End If
         Case COL_POSZ
               'If First.posz > Second.dL Then
               '   Return 1 * nDirection
               'ElseIf First.dL < Second.dL Then
               '   Return -1 * nDirection
               'Else
               '   Return 0
               'End If
         Case COL_MATERIAL
            If First.sMATERIAL > Second.sMATERIAL Then
               Return 1 * nDirection
            ElseIf First.sMATERIAL < Second.sMATERIAL Then
               Return -1 * nDirection
            Else
               Return 0
            End If
         Case COL_USAGE
            If First.dUsage > Second.dUsage Then
               Return 1 * nDirection
            ElseIf First.dUsage < Second.dUsage Then
               Return -1 * nDirection
            Else
               Return 0
            End If
         Case COL_WASTE
            If First.dWaste > Second.dWaste Then
               Return 1 * nDirection
            ElseIf First.dWaste < Second.dWaste Then
               Return -1 * nDirection
            Else
               Return 0
            End If
      End Select
      Return 0
   End Function

End Class


'Friend Class MyComparer
'   Implements IComparer

'   Dim m_Direction As ListSortDirection
'   Dim m_Column As EgtDataGridColumn
'   Dim m_ColumnList As List(Of DataGridColumn)
'   Dim m_EgtColumnList As New List(Of EgtDataGridColumn)

'   Sub New(Column As EgtDataGridColumn, ColumnList As List(Of DataGridColumn), EgtColumnList As List(Of EgtDataGridColumn), Direction As ListSortDirection)
'      m_Column = Column
'      m_ColumnList = ColumnList
'      m_EgtColumnList = EgtColumnList
'      m_Direction = Direction
'   End Sub

'   Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
'      Dim First As MyMachGroupVM = DirectCast(x, MyMachGroupVM)
'      Dim Second As MyMachGroupVM = DirectCast(y, MyMachGroupVM)
'      Dim bFirstProduced As Boolean = First.nProduction_State = Core.ItemState.WIP OrElse First.nProduction_State = Core.ItemState.Produced
'      Dim bSecondProduced As Boolean = Second.nProduction_State = Core.ItemState.WIP OrElse Second.nProduction_State = Core.ItemState.Produced
'      Dim nDirection As Integer = If(m_Direction, 1, -1)
'      If bFirstProduced AndAlso bSecondProduced Then
'         If First.nProductionIndex > Second.nProductionIndex Then
'            Return 1
'         ElseIf First.nProductionIndex < Second.nProductionIndex Then
'            Return -1
'         Else
'            Return 0
'         End If
'      ElseIf bFirstProduced Then
'         Return -1
'      ElseIf bSecondProduced Then
'         Return 1
'      Else
'         Dim ColumnIndex = 0
'         While ColumnIndex <= m_EgtColumnList.Count - 1
'            Dim nDir As ListSortDirection = If(m_ColumnList(ColumnIndex).SortDirection, 1, -1)
'            Dim nCheck As Integer = Check(First, Second, nDir, m_EgtColumnList(ColumnIndex))
'            If nCheck <> 0 Then
'               Return nCheck
'            End If
'            ColumnIndex += 1
'         End While
'         Return 0
'      End If
'   End Function

'   Private Function Check(First As MyMachGroupVM, Second As MyMachGroupVM, nDirection As Integer, Column As EgtDataGridColumn) As Integer
'      Select Case Column.Name
'         Case COL_NAME
'            If First.nName > Second.nName Then
'               Return 1 * nDirection
'            ElseIf First.nName < Second.nName Then
'               Return -1 * nDirection
'            Else
'               Return 0
'            End If
'         Case COL_STARTCUT
'            If First.Id > Second.Id Then
'               Return 1 * nDirection
'            ElseIf First.Id < Second.Id Then
'               Return -1 * nDirection
'            Else
'               Return 0
'            End If
'         Case COL_W
'            If First.dW > Second.dW Then
'               Return 1 * nDirection
'            ElseIf First.dW < Second.dW Then
'               Return -1 * nDirection
'            Else
'               Return 0
'            End If
'         Case COL_H
'            If First.dH > Second.dH Then
'               Return 1 * nDirection
'            ElseIf First.dH < Second.dH Then
'               Return -1 * nDirection
'            Else
'               Return 0
'            End If
'         Case COL_L
'            If First.dL > Second.dL Then
'               Return 1 * nDirection
'            ElseIf First.dL < Second.dL Then
'               Return -1 * nDirection
'            Else
'               Return 0
'            End If
'         Case COL_POSZ
'               'If First.posz > Second.dL Then
'               '   Return 1 * nDirection
'               'ElseIf First.dL < Second.dL Then
'               '   Return -1 * nDirection
'               'Else
'               '   Return 0
'               'End If
'         Case COL_MATERIAL
'            If First.sMATERIAL > Second.sMATERIAL Then
'               Return 1 * nDirection
'            ElseIf First.sMATERIAL < Second.sMATERIAL Then
'               Return -1 * nDirection
'            Else
'               Return 0
'            End If
'         Case COL_USAGE
'            If First.dUsage > Second.dUsage Then
'               Return 1 * nDirection
'            ElseIf First.dUsage < Second.dUsage Then
'               Return -1 * nDirection
'            Else
'               Return 0
'            End If
'         Case COL_WASTE
'            If First.dWaste > Second.dWaste Then
'               Return 1 * nDirection
'            ElseIf First.dWaste < Second.dWaste Then
'               Return -1 * nDirection
'            Else
'               Return 0
'            End If
'      End Select
'   End Function

'   'Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
'   '   Dim First As MyMachGroupVM = DirectCast(x, MyMachGroupVM)
'   '   Dim Second As MyMachGroupVM = DirectCast(y, MyMachGroupVM)
'   '   Dim bFirstProduced As Boolean = First.nProduction_State = Core.ItemState.WIP OrElse First.nProduction_State = Core.ItemState.Produced
'   '   Dim bSecondProduced As Boolean = Second.nProduction_State = Core.ItemState.WIP OrElse Second.nProduction_State = Core.ItemState.Produced
'   '   Dim nDirection As Integer = If(m_Direction, 1, -1)
'   '   If bFirstProduced AndAlso bSecondProduced Then
'   '      If First.nProductionIndex > Second.nProductionIndex Then
'   '         Return 1
'   '      ElseIf First.nProductionIndex < Second.nProductionIndex Then
'   '         Return -1
'   '      Else
'   '         Return 0
'   '      End If
'   '   ElseIf bFirstProduced Then
'   '      Return -1
'   '   ElseIf bSecondProduced Then
'   '      Return 1
'   '   Else
'   '      Select Case m_Column.Name
'   '         Case COL_NAME
'   '            If First.nName > Second.nName Then
'   '               Return 1 * nDirection
'   '            ElseIf First.nName < Second.nName Then
'   '               Return -1 * nDirection
'   '            Else
'   '               Return 0
'   '            End If
'   '         Case COL_STARTCUT
'   '            If First.Id > Second.Id Then
'   '               Return 1 * nDirection
'   '            ElseIf First.Id < Second.Id Then
'   '               Return -1 * nDirection
'   '            Else
'   '               Return 0
'   '            End If
'   '         Case COL_W
'   '            If First.dW > Second.dW Then
'   '               Return 1 * nDirection
'   '            ElseIf First.dW < Second.dW Then
'   '               Return -1 * nDirection
'   '            Else
'   '               Return 0
'   '            End If
'   '         Case COL_H
'   '            If First.dH > Second.dH Then
'   '               Return 1 * nDirection
'   '            ElseIf First.dH < Second.dH Then
'   '               Return -1 * nDirection
'   '            Else
'   '               Return 0
'   '            End If
'   '         Case COL_L
'   '            If First.dL > Second.dL Then
'   '               Return 1 * nDirection
'   '            ElseIf First.dL < Second.dL Then
'   '               Return -1 * nDirection
'   '            Else
'   '               Return 0
'   '            End If
'   '         Case COL_POSZ
'   '            'If First.posz > Second.dL Then
'   '            '   Return 1 * nDirection
'   '            'ElseIf First.dL < Second.dL Then
'   '            '   Return -1 * nDirection
'   '            'Else
'   '            '   Return 0
'   '            'End If
'   '         Case COL_MATERIAL
'   '            If First.sMATERIAL > Second.sMATERIAL Then
'   '               Return 1 * nDirection
'   '            ElseIf First.sMATERIAL < Second.sMATERIAL Then
'   '               Return -1 * nDirection
'   '            Else
'   '               Return 0
'   '            End If
'   '         Case COL_USAGE
'   '            If First.dUsage > Second.dUsage Then
'   '               Return 1 * nDirection
'   '            ElseIf First.dUsage < Second.dUsage Then
'   '               Return -1 * nDirection
'   '            Else
'   '               Return 0
'   '            End If
'   '         Case COL_WASTE
'   '            If First.dWaste > Second.dWaste Then
'   '               Return 1 * nDirection
'   '            ElseIf First.dWaste < Second.dWaste Then
'   '               Return -1 * nDirection
'   '            Else
'   '               Return 0
'   '            End If
'   '      End Select
'   '      'Dim dFirstValue As Double
'   '      'If First.nProductionIndex > Second.nProductionIndex Then
'   '      '   Return 1 * nDirection
'   '      'ElseIf First.nProductionIndex < Second.nProductionIndex Then
'   '      '   Return -1 * nDirection
'   '      'Else
'   '      '   Return 0
'   '      'End If
'   '   End If

'   '   Throw New NotImplementedException()
'   'End Function

'End Class
