Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports EgtUILib

Public Class VacuumMachineButtonUC
   Implements INotifyPropertyChanged

   ' Costanti per nomi stati dei bottoni
   Private Const BTN_STATE_NOTHING As String = "Nothing"

   Private Enum MachineButtonType As Integer
      NOSTATE = 0
      TWOSTATE = 1
      THREESTATE = 2
      DOUBLECOMMAND = 3
      PRESSEDCOMMAND = 4
   End Enum

   ' Dichiarazione delle Page UserControl
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
   Private m_DirectCutPage As DirectCutPageUC
   Friend m_CN As CN_generico
   Private m_bFirst As Boolean = True

   ' Creazione converter da String a ImageSource
   Dim ImageConverter As New ImageSourceConverter

   Private m_ButtonList As New ObservableCollection(Of MachineButton)
   Public ReadOnly Property ButtonList As ObservableCollection(Of MachineButton)
      Get
         Return m_ButtonList
      End Get
   End Property

   Private Sub ManualAxesMove_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
      Me.DataContext = Me
   End Sub

   Private Sub ManualAxesMove_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
      If m_bFirst Then
         m_DirectCutPage = m_MainWindow.m_DirectCutPageUC
         m_bFirst = False

         ' Lettura configurazione bottoni da Ini di macchina
         Dim m_nCount As Integer = 1
         Dim bFoundedBtn As Boolean = True
         While bFoundedBtn
            Dim sNameBtn As String = K_BUTTON & (m_nCount).ToString()
            Dim sMachineButtonType As String = String.Empty
            Dim sTImageName As String = String.Empty
            Dim sTLuaScriptName As String = String.Empty
            Dim sFImageName As String = String.Empty
            Dim sFLuaScriptName As String = String.Empty
            Dim sStateFlag As String = String.Empty
            bFoundedBtn = GetPrivateProfileMachineButtons(S_VACUUMMACHBUTTONS, sNameBtn, sMachineButtonType, sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag, m_MainWindow.GetMachIniFile())
            If bFoundedBtn Then
               m_nCount += 1
               Dim ButtonToAdd As MachineButton = Nothing
               Select Case sMachineButtonType
                  Case MachineButtonType.NOSTATE
                     ButtonToAdd = New TwoStateButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
                  Case MachineButtonType.TWOSTATE
                     ButtonToAdd = New TwoStateButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
                  Case MachineButtonType.THREESTATE
                     ButtonToAdd = New ThreeStateButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
                  Case MachineButtonType.DOUBLECOMMAND
                     ButtonToAdd = New DoubleCommandButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
                  Case MachineButtonType.PRESSEDCOMMAND
                     ButtonToAdd = New PressedCommandButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
               End Select
               If Not IsNothing(ButtonToAdd) Then m_ButtonList.Add(ButtonToAdd)
            End If
         End While
         NotifyPropertyChanged("ButtonList")
      End If
   End Sub

   Friend Sub VacuumUpChanged(VacuumUpState As Boolean)
      Dim VacuumUpButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUMUP Then
            VacuumUpButton = MachineButton
         End If
      Next
      If Not IsNothing(VacuumUpButton) Then
         VacuumUpButton.SetIsChecked(VacuumUpState)
      End If
   End Sub

   Friend Sub VacuumDownChanged(VacuumDownState As Boolean)
      Dim VacuumDownButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUMDOWN Then
            VacuumDownButton = MachineButton
         End If
      Next
      If Not IsNothing(VacuumDownButton) Then
         VacuumDownButton.SetIsChecked(VacuumDownState)
      End If
   End Sub

   Friend Sub Vacuum2UpChanged(Vacuum2UpState As Boolean)
      Dim Vacuum2UpButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUM2UP Then
            Vacuum2UpButton = MachineButton
         End If
      Next
      If Not IsNothing(Vacuum2UpButton) Then
         Vacuum2UpButton.SetIsChecked(Vacuum2UpState)
      End If
   End Sub

   Friend Sub Vacuum2DownChanged(Vacuum2DownState As Boolean)
      Dim Vacuum2DownButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUM2DOWN Then
            Vacuum2DownButton = MachineButton
         End If
      Next
      If Not IsNothing(Vacuum2DownButton) Then
         Vacuum2DownButton.SetIsChecked(Vacuum2DownState)
      End If
   End Sub

   Friend Sub Vacuum0Changed(Vacuum0State As Boolean)
      Dim Vacuum0Button As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUM0 Then
            Vacuum0Button = MachineButton
         End If
      Next
      If Not IsNothing(Vacuum0Button) Then
         Vacuum0Button.SetIsChecked(Vacuum0State)
      End If
   End Sub

   Friend Sub Vacuum90Changed(Vacuum90State As Boolean)
      Dim VacuumRotButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUM90 Then
            VacuumRotButton = MachineButton
         End If
      Next
      If Not IsNothing(VacuumRotButton) Then
         VacuumRotButton.SetIsChecked(Vacuum90State)
      End If
   End Sub

   Friend Sub Vacuum1Changed(Vacuum1State As Boolean)
      Dim Vacuum1StateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUM1STATE Then
            Vacuum1StateButton = MachineButton
         End If
      Next
      If Not IsNothing(Vacuum1StateButton) Then
         Vacuum1StateButton.SetIsChecked(Vacuum1State)
      End If
   End Sub

   Friend Sub Vacuum2Changed(Vacuum2State As Boolean)
      Dim Vacuum2StateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUM2STATE Then
            Vacuum2StateButton = MachineButton
         End If
      Next
      If Not IsNothing(Vacuum2StateButton) Then
         Vacuum2StateButton.SetIsChecked(Vacuum2State)
      End If
   End Sub

   Friend Sub Vacuum3Changed(Vacuum3State As Boolean)
      Dim Vacuum3StateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUM3STATE Then
            Vacuum3StateButton = MachineButton
         End If
      Next
      If Not IsNothing(Vacuum3StateButton) Then
         Vacuum3StateButton.SetIsChecked(Vacuum3State)
      End If
   End Sub

   Friend Sub Vacuum4Changed(Vacuum4State As Boolean)
      Dim Vacuum4StateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUM4STATE Then
            Vacuum4StateButton = MachineButton
         End If
      Next
      If Not IsNothing(Vacuum4StateButton) Then
         Vacuum4StateButton.SetIsChecked(Vacuum4State)
      End If
   End Sub

   Friend Sub Vacuum5Changed(Vacuum5State As Boolean)
      Dim Vacuum5StateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUM5STATE Then
            Vacuum5StateButton = MachineButton
         End If
      Next
      If Not IsNothing(Vacuum5StateButton) Then
         Vacuum5StateButton.SetIsChecked(Vacuum5State)
      End If
   End Sub

   Friend Sub Vacuum6Changed(Vacuum6State As Boolean)
      Dim Vacuum6StateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUM6STATE Then
            Vacuum6StateButton = MachineButton
         End If
      Next
      If Not IsNothing(Vacuum6StateButton) Then
         Vacuum6StateButton.SetIsChecked(Vacuum6State)
      End If
   End Sub

   Friend Sub Vacuum135Changed(Vacuum135State As Boolean)
      Dim Vacuum135StateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUM135STATE Then
            Vacuum135StateButton = MachineButton
         End If
      Next
      If Not IsNothing(Vacuum135StateButton) Then
         Vacuum135StateButton.SetIsChecked(Vacuum135State)
      End If
   End Sub

   Friend Sub Vacuum246Changed(Vacuum246State As Boolean)
      Dim Vacuum246StateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUM246STATE Then
            Vacuum246StateButton = MachineButton
         End If
      Next
      If Not IsNothing(Vacuum246StateButton) Then
         Vacuum246StateButton.SetIsChecked(Vacuum246State)
      End If
   End Sub

   Friend Sub OpenAllChanged(OpenAllState As Boolean)
      Dim OpenAllStateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_OPENALLSTATE Then
            OpenAllStateButton = MachineButton
         End If
      Next
      If Not IsNothing(OpenAllStateButton) Then
         OpenAllStateButton.SetIsChecked(OpenAllState)
      End If
   End Sub

   Friend Sub CloseAllChanged(CloseAllState As Boolean)
      Dim CloseAllStateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_CLOSEALLSTATE Then
            CloseAllStateButton = MachineButton
         End If
      Next
      If Not IsNothing(CloseAllStateButton) Then
         CloseAllStateButton.SetIsChecked(CloseAllState)
      End If
   End Sub

   Friend Sub Open123Changed(Open123State As Boolean)
      Dim Open123StateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_OPEN123STATE Then
            Open123StateButton = MachineButton
         End If
      Next
      If Not IsNothing(Open123StateButton) Then
         Open123StateButton.SetIsChecked(Open123State)
      End If
   End Sub

   Friend Sub Open456Changed(Open456State As Boolean)
      Dim Open456StateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_OPEN456STATE Then
            Open456StateButton = MachineButton
         End If
      Next
      If Not IsNothing(Open456StateButton) Then
         Open456StateButton.SetIsChecked(Open456State)
      End If
   End Sub

   Friend Sub Close123Changed(Close123State As Boolean)
      Dim Close123StateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_CLOSE123STATE Then
            Close123StateButton = MachineButton
         End If
      Next
      If Not IsNothing(Close123StateButton) Then
         Close123StateButton.SetIsChecked(Close123State)
      End If
   End Sub

   Friend Sub Close456Changed(Close456State As Boolean)
      Dim Close456StateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_CLOSE456STATE Then
            Close456StateButton = MachineButton
         End If
      Next
      If Not IsNothing(Close456StateButton) Then
         Close456StateButton.SetIsChecked(Close456State)
      End If
   End Sub

   Friend Sub BlowChanged(BlowState As Boolean)
      Dim BlowStateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_BLOWSTATE Then
            BlowStateButton = MachineButton
         End If
      Next
      If Not IsNothing(BlowStateButton) Then
         BlowStateButton.SetIsChecked(BlowState)
      End If
   End Sub

   Friend Sub VacuumOnChanged(VacuumState As Boolean)
      Dim VacuumOnButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUMON Then
            VacuumOnButton = MachineButton
         End If
      Next
      If Not IsNothing(VacuumOnButton) Then
         VacuumOnButton.SetIsChecked(VacuumState)
      End If
   End Sub

   Friend Sub VacuumOffChanged(VacuumState As Boolean)
      Dim VacuumOffButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_VACUUMOFF Then
            VacuumOffButton = MachineButton
         End If
      Next
      If Not IsNothing(VacuumOffButton) Then
         VacuumOffButton.SetIsChecked(VacuumState)
      End If
   End Sub

   Friend Sub BypassChanged(BypassState As Boolean)
      Dim PressedBypassStateButton As PressedCommandButton = Nothing
      Dim TwoBypassStateButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_BYPASSTATE Then
            If TypeOf MachineButton Is TwoStateButton Then
               TwoBypassStateButton = MachineButton
               If Not IsNothing(TwoBypassStateButton) Then
                  TwoBypassStateButton.SetIsChecked(BypassState)
               End If
            ElseIf TypeOf MachineButton Is PressedCommandButton Then
               PressedBypassStateButton = MachineButton
               If Not IsNothing(PressedBypassStateButton) Then
                  PressedBypassStateButton.SetIsPressed(BypassState)
               End If
            End If
         End If
      Next
   End Sub

   Public Function GetPrivateProfileMachineButtons(
                         ByVal lpAppName As String,
                         ByVal lpKeyName As String,
                         ByRef lpMachineButtonType As String,
                         ByRef lpTImageName As String,
                         ByRef lpTLuaScriptName As String,
                         ByRef lpFImageName As String,
                         ByRef lpFLuaScriptName As String,
                         ByRef lpStateFlag As String,
                         ByVal lpFileName As String) As Boolean

      Dim sVal As String = String.Empty
      GetPrivateProfileString(lpAppName, lpKeyName, "", sVal, lpFileName)
      Dim sItems() As String = sVal.Split(",".ToCharArray)
      If sItems.Count() >= 3 Then
         lpMachineButtonType = sItems(0)
         lpTImageName = sItems(1)
         lpTLuaScriptName = sItems(2)
         If sItems.Count() >= 4 Then
            lpStateFlag = sItems(3)
            If sItems.Count() >= 6 Then
               lpFImageName = sItems(4)
               lpFLuaScriptName = sItems(5)
            End If
         Else
            lpStateFlag = BTN_STATE_NOTHING
         End If
         Return True
      End If
      Return False
   End Function

   Private Sub ManualAxesMove_UnLoaded(sender As Object, e As RoutedEventArgs) Handles Me.Unloaded
   End Sub

   Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

   Public Sub NotifyPropertyChanged(propName As String)
      RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
   End Sub

#Region "EVENTS"

   Private Sub PressedCommandButton_PreviewMouseUp(sender As Object, e As MouseButtonEventArgs)
      Dim Button As Button = DirectCast(sender, Button)
      If TypeOf Button.DataContext Is PressedCommandButton Then
         Dim PressedButton As PressedCommandButton = DirectCast(Button.DataContext, PressedCommandButton)
         PressedButton.OnMouseUp()
      End If
   End Sub

   Private Sub PressedCommandButton_PreviewMouseDown(sender As Object, e As MouseButtonEventArgs)
      Dim Button As Button = DirectCast(sender, Button)
      If TypeOf Button.DataContext Is PressedCommandButton Then
         Dim PressedButton As PressedCommandButton = DirectCast(Button.DataContext, PressedCommandButton)
         PressedButton.OnMouseDown()
      End If
   End Sub

#End Region

End Class
