Imports System.Collections.ObjectModel
Imports EgtWPFLib5
Imports EgtUILib

Public Class AxesPanelVM
   Inherits VMBase

   Private m_AxesList As New ObservableCollection(Of Axis)
   Public ReadOnly Property AxesList As ObservableCollection(Of Axis)
      Get
         Return m_AxesList
      End Get
   End Property

   Sub New()
      ' immposto riferimento in Map
      Map.SetRefAxesPanelVM(Me)
   End Sub

   Friend Sub LoadAxes()
      ' leggo assi da ini
      Dim nIndex As Integer = 1
      Dim Axis As Axis = GetPrivateProfileAxis(S_AXES, nIndex.ToString(), CurrentMachine.sMachIniFile)
      ' leggo assi lineari
      While Not IsNothing(Axis)
         AxesList.Add(Axis)
         nIndex += 1
         Axis = GetPrivateProfileAxis(S_AXES, nIndex, CurrentMachine.sMachIniFile)
      End While
   End Sub

   Private Function GetPrivateProfileAxis(IpAppName As String, IpKeyName As String, IpFileName As String) As Axis
      Dim sAxis As String = ""
      EgtUILib.GetPrivateProfileString(IpAppName, IpKeyName, "", sAxis, IpFileName)
      Dim sAxisValues() As String = sAxis.Split(","c)
      If Not sAxisValues.Count >= 3 Then Return Nothing
      For Each Value In sAxisValues
         Value = Value.Trim()
      Next
      Dim nId As Integer = 0
      Dim Type As Axis.AxisTypes = Axis.AxisTypes.NULL
      Integer.TryParse(sAxisValues(1), nId)
      Select Case sAxisValues(0).ToLower()
         Case "l"
            Type = Axis.AxisTypes.LINEAR
         Case "r"
            Type = Axis.AxisTypes.ROTATIONAL
         Case Else
            Type = Axis.AxisTypes.NULL
      End Select
      Return New Axis(nId, Type, sAxisValues(2))
   End Function

   Friend Sub AxisCoordinatesCallbackDlg(AxisValue As Double, AxisIndex As Integer)
      Dim Axis As Axis = m_AxesList.FirstOrDefault(Function(x) x.nId = AxisIndex)
      If Not IsNothing(Axis) Then
         Axis.SetValue(AxisValue)
      End If
   End Sub

   Private Sub SetAxisValue(VarName As String, VarValue As String)
      Dim Axis As Axis = m_AxesList.FirstOrDefault(Function(x) x.sName = VarName)
      If Not IsNothing(Axis) Then
         Axis.SetValue(VarValue)
      End If
   End Sub

End Class

Public Class Axis
   Inherits VMBase

   Public Enum AxisTypes As Integer
      NULL = 0
      LINEAR = 1
      ROTATIONAL = 2
   End Enum

   Private m_nId As String
   Public ReadOnly Property nId As String
      Get
         Return m_nId
      End Get
   End Property

   Private m_sName As String
   Public ReadOnly Property sName As String
      Get
         Return m_sName
      End Get
   End Property

   Private m_Type As AxisTypes
   Public ReadOnly Property Type As AxisTypes
      Get
         Return m_Type
      End Get
   End Property

   Private m_dValue As Double
   Public ReadOnly Property sValue As String
      Get
         Dim nDec As Integer = 0
         If CurrentMachine.NCType = NCTypes.TPA Then
            nDec = -3
         Else
            Select Case Type
               Case AxisTypes.LINEAR
                  nDec = -3
               Case AxisTypes.ROTATIONAL
                  nDec = -4
               Case Else
                  nDec = 0
            End Select
         End If
         Return DoubleToString(m_dValue, nDec)
      End Get
   End Property

   Friend Sub SetValue(value As Double)
      If CurrentMachine.NCType = NCTypes.TPA Then
         m_dValue = value
      Else
         Select Case Type
            Case AxisTypes.LINEAR
               m_dValue = value / 1000
            Case AxisTypes.ROTATIONAL
               m_dValue = value / 10000
            Case Else
               m_dValue = value
         End Select
      End If
      NotifyPropertyChanged(NameOf(sValue))
   End Sub

   Public Sub New(nId As String, Type As AxisTypes, sName As String)
      m_nId = nId
      m_Type = Type
      m_sName = sName
   End Sub

End Class
