Imports System.Reflection

Public Class BTLPartListV

   Private m_BTLPartListVM As BTLPartListVM

   Private m_SelectBtn As MouseButton = MouseButton.Left
   Private m_HighlightBtn As MouseButton = MouseButton.Right

   Private m_NameDataGridType As Type
   Private s_isDraggingSelectionField As FieldInfo
   Private s_endDraggingMethod As MethodInfo

   Sub New()
      ' This call is required by the designer.
      InitializeComponent()
      ' Add any initialization after the InitializeComponent() call.
      m_BTLPartListVM = DataContext
      m_NameDataGridType = BTLPart_DataGrid.GetType()
      s_isDraggingSelectionField = m_NameDataGridType.GetField("_isDraggingSelection", BindingFlags.Instance Or BindingFlags.NonPublic)
      s_endDraggingMethod = m_NameDataGridType.GetMethod("EndDragging", BindingFlags.Instance Or BindingFlags.NonPublic)
      SetSelButton(MouseButton.Right)
   End Sub

   Public Sub SetSelButton(SelButton As MouseButton)
      Select Case SelButton
         Case MouseButton.Right
            m_SelectBtn = MouseButton.Right
            m_HighlightBtn = MouseButton.Left
         Case Else      ' MouseButton.Left
            m_SelectBtn = MouseButton.Left
            m_HighlightBtn = MouseButton.Right
      End Select
   End Sub

   Public Sub DataGrid_PreviewMouseDown(sender As Object, e As MouseButtonEventArgs)
      If e.ChangedButton = MouseButton.Left Then
         If e.ChangedButton = m_SelectBtn Then
            If (Keyboard.Modifiers And ModifierKeys.Shift) = ModifierKeys.Shift OrElse (Keyboard.Modifiers And ModifierKeys.Control) = ModifierKeys.Control Then
               e.Handled = True
            Else
               Map.refProjectVM.BTLStructureVM.SetSelectionType(BTLStructureVM.SelectionTypes.SELECT_)
            End If
         ElseIf e.ChangedButton = m_HighlightBtn AndAlso (Map.refProjectVM.BTLStructureVM.SelectionType = BTLStructureVM.SelectionTypes.SELECT_ AndAlso
                ((Keyboard.Modifiers And ModifierKeys.Shift) = ModifierKeys.Shift OrElse (Keyboard.Modifiers And ModifierKeys.Control) = ModifierKeys.Control)) Then
            e.Handled = True
         ElseIf e.ChangedButton = m_HighlightBtn Then
            Map.refProjectVM.BTLStructureVM.SetSelectionType(BTLStructureVM.SelectionTypes.HIGHLIGHT)
         End If
      End If
      'If e.ChangedButton = Input.MouseButton.Left Then
      '   If (Keyboard.Modifiers And ModifierKeys.Shift) = ModifierKeys.Shift OrElse (Keyboard.Modifiers And ModifierKeys.Control) = ModifierKeys.Control Then
      '      e.Handled = True
      '   Else
      '      m_MainWindowVM.SetMouseButton("Left")
      '   End If
      'ElseIf e.ChangedButton = Input.MouseButton.Right Then
      '   m_MainWindowVM.SetMouseButton("Right")
      'End If
   End Sub

   Public Sub DataGrid_PreviewMouseUp(sender As Object, e As MouseButtonEventArgs)
      If e.ChangedButton = MouseButton.Right Then
         If e.ChangedButton = m_SelectBtn Then
            If (Keyboard.Modifiers And ModifierKeys.Shift) = ModifierKeys.Shift OrElse (Keyboard.Modifiers And ModifierKeys.Control) = ModifierKeys.Control Then
               e.Handled = True
            Else
               Map.refProjectVM.BTLStructureVM.SetSelectionType(BTLStructureVM.SelectionTypes.SELECT_)
            End If
         ElseIf e.ChangedButton = m_HighlightBtn Then
            Map.refProjectVM.BTLStructureVM.SetSelectionType(BTLStructureVM.SelectionTypes.HIGHLIGHT)
         End If
         'Else
         'If e.ChangedButton = m_HighlightBtn Then
         '   Dim cell As DataGridCell = sender
         '   If (Not IsNothing(cell) And Not cell.IsEditing) Then
         '      Dim row As DataGridRow = FindVisualParent(Of DataGridRow)(cell)
         '      If Not IsNothing(row) Then
         '         row.IsSelected = Not row.IsSelected
         '         e.Handled = True
         '      End If
         '   End If
         '   m_MainWindowVM.SetMouseButton("Highlight")
         'End If
      End If
   End Sub

   Public Shared Function FindVisualParent(Of T As DependencyObject)(ByVal child As DependencyObject) As T
      Dim parentObject As DependencyObject = VisualTreeHelper.GetParent(child)
      If parentObject Is Nothing Then Return Nothing
      Dim parent As T = TryCast(parentObject, T)

      If parent IsNot Nothing Then
         Return parent
      Else
         Return FindVisualParent(Of T)(parentObject)
      End If
   End Function

   Public Sub DataGrid_PreviewMouseMove(sender As Object, e As MouseEventArgs)
      If m_SelectBtn = MouseButton.Left AndAlso e.LeftButton = MouseButtonState.Pressed Then
         If CBool(If(s_isDraggingSelectionField?.GetValue(BTLPart_DataGrid), False)) Then s_endDraggingMethod.Invoke(BTLPart_DataGrid, New Object(-1) {})
      End If
   End Sub

   Private Sub PartList_PreviewMouseDown(sender As Object, e As MouseButtonEventArgs)
      If TypeOf sender Is DataGridRow Then
         Dim Row As DataGridRow = DirectCast(sender, DataGridRow)
         If Not Row.IsSelected Then Return
         Select Case e.ChangedButton
            Case MouseButton.Left
               If (Keyboard.Modifiers And ModifierKeys.Shift) = ModifierKeys.Shift OrElse (Keyboard.Modifiers And ModifierKeys.Control) = ModifierKeys.Control Then
                  Return
               Else
                  BTLPart_DataGrid.SelectedItem = Nothing
                  BTLPart_DataGrid.SelectedItem = Row.DataContext
               End If

            Case MouseButton.Right


         End Select
         'If Keyboard.Modifiers = ModifierKeys.Control AndAlso Not IsNothing(Row) AndAlso Row.IsSelected AndAlso Map.refProjectVM.BTLStructureVM.BTLPartVMList.LongCount(Function(x) x.IsSelected) = 1 Then
         '   e.Handled = True
         '   Return
         'ElseIf Not Keyboard.Modifiers = ModifierKeys.Control AndAlso Not Keyboard.Modifiers = ModifierKeys.Shift AndAlso Not IsNothing(Row) Then
         '   For Each BTLPart In Map.refProjectVM.BTLStructureVM.BTLPartVMList
         '      If Not BTLPart Is Row.DataContext AndAlso BTLPart.IsSelected = True Then
         '         BTLPart.SetIsSelected(False)
         '      End If
         '   Next
         'End If
         'If Not Keyboard.Modifiers = ModifierKeys.Control AndAlso Not IsNothing(Row) AndAlso Row.IsSelected Then
         '   Map.refProjectVM.BTLStructureVM.SetSelBTLPart(Map.refProjectVM.BTLStructureVM.SelBTLPart)
         'End If
      End If
      'If TypeOf sender Is DataGridRow Then
      '   Dim Row As DataGridRow = DirectCast(sender, DataGridRow)
      '   If Keyboard.Modifiers = ModifierKeys.Control AndAlso Not IsNothing(Row) AndAlso Row.IsSelected AndAlso Map.refProjectVM.BTLStructureVM.BTLPartVMList.LongCount(Function(x) x.IsSelected) = 1 Then
      '      e.Handled = True
      '      Return
      '   ElseIf Not Keyboard.Modifiers = ModifierKeys.Control AndAlso Not Keyboard.Modifiers = ModifierKeys.Shift AndAlso Not IsNothing(Row) Then
      '      For Each BTLPart In Map.refProjectVM.BTLStructureVM.BTLPartVMList
      '         If Not BTLPart Is Row.DataContext AndAlso BTLPart.IsSelected = True Then
      '            BTLPart.SetIsSelected(False)
      '         End If
      '      Next
      '   End If
      '   If Not Keyboard.Modifiers = ModifierKeys.Control AndAlso Not IsNothing(Row) AndAlso Row.IsSelected Then
      '      Map.refProjectVM.BTLStructureVM.SetSelBTLPart(Map.refProjectVM.BTLStructureVM.SelBTLPart)
      '   End If
      'End If
   End Sub

   Private Sub PartList_PreviewMouseUp(sender As Object, e As MouseButtonEventArgs)
      If TypeOf sender Is DataGridRow Then
         Dim Row As DataGridRow = DirectCast(sender, DataGridRow)
         If Not Row.IsSelected Then Return
         Select Case e.ChangedButton
            Case MouseButton.Right
               If (Keyboard.Modifiers And ModifierKeys.Shift) = ModifierKeys.Shift OrElse (Keyboard.Modifiers And ModifierKeys.Control) = ModifierKeys.Control Then
                  Row.IsSelected = Not Row.IsSelected
               Else
                  BTLPart_DataGrid.SelectedItem = Nothing
                  BTLPart_DataGrid.SelectedItem = Row.DataContext
                  ' metto focus su cella selezionata per evitare errori di focus del sistema normale
                  Dim dep As DependencyObject = CType(e.OriginalSource, DependencyObject)
                  While (dep IsNot Nothing) AndAlso Not (TypeOf dep Is DataGridCell)
                     dep = VisualTreeHelper.GetParent(dep)
                  End While
                  If dep Is Nothing Then Return
                  If TypeOf dep Is DataGridCell Then
                     Dim cell As DataGridCell = TryCast(dep, DataGridCell)
                     cell.Focus()
                  End If
               End If
         End Select
         'If Keyboard.Modifiers = ModifierKeys.Control AndAlso Not IsNothing(Row) AndAlso Row.IsSelected AndAlso Map.refProjectVM.BTLStructureVM.BTLPartVMList.LongCount(Function(x) x.IsSelected) = 1 Then
         '   e.Handled = True
         '   Return
         'ElseIf Not Keyboard.Modifiers = ModifierKeys.Control AndAlso Not Keyboard.Modifiers = ModifierKeys.Shift AndAlso Not IsNothing(Row) Then
         '   For Each BTLPart In Map.refProjectVM.BTLStructureVM.BTLPartVMList
         '      If Not BTLPart Is Row.DataContext AndAlso BTLPart.IsSelected = True Then
         '         BTLPart.SetIsSelected(False)
         '      End If
         '   Next
         'End If
         'If Not Keyboard.Modifiers = ModifierKeys.Control AndAlso Not IsNothing(Row) AndAlso Row.IsSelected Then
         '   Map.refProjectVM.BTLStructureVM.SetSelBTLPart(Map.refProjectVM.BTLStructureVM.SelBTLPart)
         'End If
      End If

   End Sub

End Class
