Imports System.Collections.ObjectModel
Imports EgtUILib
Imports EgtWPFLib5
Imports EgtBEAMWALL.Core

Public Class ChangeParameterWndVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Friend Event m_CloseWindow(bDialogResult As Boolean)

   Private m_PRCList As New ObservableCollection(Of PRC)
   Public ReadOnly Property PRCList As ObservableCollection(Of PRC)
      Get
         Return m_PRCList
      End Get
   End Property

   Private m_SelPRC As PRC = Nothing
   Public Property SelPRC As PRC
      Get
         Return m_SelPRC
      End Get
      Set(value As PRC)
         If value IsNot m_SelPRC Then
            m_SelPRC = value
            ' aggiorno lista parametri
            m_ParamList.Clear()
            Dim ParamIndex As Integer = 1
            Dim NewBTLParam As BTLParamM = Nothing
            ' leggo tutti i P della feature
            While BTLIniFile.GetBeamPrivateProfileParam(m_SelPRC.nGRP, m_SelPRC.nPRC, True, ParamIndex, Nothing, NewBTLParam)
               m_ParamList.Add(New Parameters(NewBTLParam))
               ParamIndex += 1
            End While
            ' aggiungo bDO come parametro
            m_ParamList.Add(New Parameters(BTLParamM.CreateNewBTLParam(), True))
            ' seleziono parametro vuoto per dargli un valore iniziale ed evitare che visualizzi sia text che check
            m_SelParam = New Parameters(BTLParamM.CreateNewBTLParam())
            NotifyPropertyChanged(NameOf(SelParam))
         End If
      End Set
   End Property

   Private m_ParamList As New ObservableCollection(Of Parameters)
   Public Property ParamList As ObservableCollection(Of Parameters)
      Get
         Return m_ParamList
      End Get
      Set(value As ObservableCollection(Of Parameters))
         m_ParamList = value
      End Set
   End Property
   Private m_SelParam As Parameters = New Parameters(BTLParamM.CreateNewBTLParam())  ' False, BTLParamType.STRING_, "")    ' per dargli un valore iniziale ed evitare che visualizzi sia text che check
   Public Property SelParam As Parameters
      Get
         Return m_SelParam
      End Get
      Set(value As Parameters)
         m_SelParam = value
         NotifyPropertyChanged("SelParam")
      End Set
   End Property

   ' Definizione comandi
   Private m_cmdOk As ICommand

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Sub New()
      Dim TempPRCList As New ObservableCollection(Of Integer)
      GetBeamPrivateProfileProcessList(GRPType.T, TempPRCList)
      m_PRCList.Clear()
      For Each PRC In TempPRCList
         If Not IsNothing(Map.refProjectVM.BTLStructureVM) AndAlso Map.refProjectVM.BTLStructureVM.BTLPartVMList.Any(Function(x) x.BTLFeatureVMList.Any(Function(y) y.nPRC = PRC AndAlso CalcBeamPrivateProfileGRP(y.nSelGRP) = 1)) Then
            m_PRCList.Add(New PRC(GRPType.T, PRC, GetBeamPrivateProfileName(GRPType.T, PRC)))
         End If
      Next
      GetBeamPrivateProfileProcessList(GRPType.L, TempPRCList)
      For Each PRC In TempPRCList
         If Not IsNothing(Map.refProjectVM.BTLStructureVM) AndAlso Map.refProjectVM.BTLStructureVM.BTLPartVMList.Any(Function(x) x.BTLFeatureVMList.Any(Function(y) y.nPRC = PRC AndAlso CalcBeamPrivateProfileGRP(y.nSelGRP) = 0)) Then
            m_PRCList.Add(New PRC(GRPType.L, PRC, GetBeamPrivateProfileName(GRPType.L, PRC)))
         End If
      Next
   End Sub

#End Region    ' CONSTRUCTOR

#Region "COMMANDS"

#Region "Ok"

   Public ReadOnly Property Ok_Command As ICommand
      Get
         If m_cmdOk Is Nothing Then
            m_cmdOk = New Command(AddressOf Ok)
         End If
         Return m_cmdOk
      End Get
   End Property

   Public Sub Ok()
      'verifico che tutti i campi contengano un valore valido
      If Not IsNothing(m_SelPRC) AndAlso
         Not IsNothing(m_SelParam) AndAlso m_SelParam.sName <> "" Then
         RaiseEvent m_CloseWindow(True)
      Else
         MessageBox.Show(EgtMsg(61858), EgtMsg(30007))
      End If
   End Sub

#End Region ' Ok

#End Region    ' COMMANDS

End Class

Public Class Parameters
   Inherits BTLParamVM

   Private m_dActualValue As Double
   Private m_sActualValue As String
   Public ReadOnly Property dActualValue As Double
      Get
         Return m_dActualValue
      End Get
   End Property
   Public Property sActualValue As String
      Get
         Select Case m_BTLParamM.nType
            Case BTLParamType.DOUBLE_
               Return DoubleToString(m_dActualValue, 3)
            Case BTLParamType.LENGTH
               Return LenToString(m_dActualValue, 3)
            Case Else             ' BTLParamType.STRING_
               Return m_sActualValue
         End Select
      End Get
      Set(value As String)
         Select Case m_BTLParamM.nType
            Case BTLParamType.DOUBLE_
               StringToDoubleAdv(value, m_dActualValue, True)
            Case BTLParamType.LENGTH
               StringToLenAdv(value, m_dActualValue, True)
            Case Else             ' BTLParamType.STRING_
               m_sActualValue = value
         End Select
      End Set
   End Property

   Private m_dNewValue As Double
   Private m_sNewValue As String
   Public ReadOnly Property dNewValue As Double
      Get
         Return m_dNewValue
      End Get
   End Property
   Public Property sNewValue As String
      Get
         Select Case m_BTLParamM.nType
            Case BTLParamType.DOUBLE_
               Return DoubleToString(m_dNewValue, 3)
            Case BTLParamType.LENGTH
               Return LenToString(m_dNewValue, 3)
            Case Else             ' BTLParamType.STRING_
               Return m_sNewValue
         End Select
      End Get
      Set(value As String)
         Select Case m_BTLParamM.nType
            Case BTLParamType.DOUBLE_
               StringToDoubleAdv(value, m_dNewValue, True)
            Case BTLParamType.LENGTH
               StringToLenAdv(value, m_dNewValue, True)
            Case Else             ' BTLParamType.STRING_
               m_sNewValue = value
         End Select
      End Set
   End Property

   Private m_bActualValue As Boolean
   Public Property bActualValue As Boolean
      Get
         Return m_bActualValue
      End Get
      Set(value As Boolean)
         m_bActualValue = value
      End Set
   End Property

   Private m_bNewValue As Boolean
   Public Property bNewValue As Boolean
      Get
         Return m_bNewValue
      End Get
      Set(value As Boolean)
         m_bNewValue = value
      End Set
   End Property

   Private m_bIsCheckBox As Boolean = False
   Public Property IsTextBox_Visibility As Visibility
      Get
         Return If(m_bIsCheckBox, Visibility.Collapsed, Visibility.Visible)
      End Get
      Set(value As Visibility)
         m_bIsCheckBox = Not value
      End Set
   End Property
   Public Property IsCheckBox_Visibility As Visibility
      Get
         Return If(m_bIsCheckBox, Visibility.Visible, Visibility.Collapsed)
      End Get
      Set(value As Visibility)
         m_bIsCheckBox = value
      End Set
   End Property

   Public ReadOnly Property sNameAndDescription As String
      Get
         Return If(m_bIsCheckBox, "Attivazione", sName & " " & sDescription)
      End Get
   End Property

   Sub New(BTLParamM As BTLParamM, Optional IsCheckBox As Boolean = False)
      MyBase.New(BTLParamM)
      m_bIsCheckBox = IsCheckBox
      If IsCheckBox Then m_BTLParamM.SetType(BTLParamType.CHECKBOX)
   End Sub

   'Shared Operator =(ByVal S1 As Parameters, ByVal S2 As BTLParam) As Boolean
   '   Return (S1.m_dH <> S2.m_dH OrElse S1.m_dW <> S2.m_dW)
   'End Operator

End Class