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

Public Class BTLParamVM
   Inherits VMBase

   Protected m_BTLParamM As BTLParamM
   Public ReadOnly Property BTLParamM As BTLParamM
      Get
         Return m_BTLParamM
      End Get
   End Property

   Private m_BTLFeatureM As BTLFeatureM

   Public ReadOnly Property bIsP As Boolean
      Get
         Return m_BTLParamM.bIsP
      End Get
   End Property

   Public Property bIsModified As Boolean
      Get
         Return m_BTLParamM.bIsModified
      End Get
      Set(value As Boolean)
         m_BTLParamM.bIsModified = value
      End Set
   End Property

   Public ReadOnly Property Custom_Visibility As Visibility
      Get
         If bIsP Then
            Return Visibility.Collapsed
         Else
            Return Visibility.Visible
         End If
      End Get
   End Property

   ' parametri da struttura
   Public Property sName As String
      Get
         Return m_BTLParamM.sName
      End Get
      Set(value As String)
         m_BTLParamM.sName = value
      End Set
   End Property

   Public ReadOnly Property nId As Integer
      Get
         Return m_BTLParamM.nId
      End Get
   End Property

   Public Property sDescription As String
      Get
         Return m_BTLParamM.sDescription
      End Get
      Set(value As String)
         m_BTLParamM.sDescription = value
      End Set
   End Property

   Public ReadOnly Property sMin As String
      Get
         Select Case m_BTLParamM.nType
            Case BTLParamType.DOUBLE_
               Return DoubleToString(m_BTLParamM.dMin, 3)
            Case BTLParamType.LENGTH
               Return LenToString(m_BTLParamM.dMin, 3)
            Case Else
               Return ""
         End Select
      End Get
   End Property

   Public ReadOnly Property dMin As Double
      Get
         Return m_BTLParamM.dMin
      End Get
   End Property

   Public ReadOnly Property sMax As String
      Get
         Select Case m_BTLParamM.nType
            Case BTLParamType.DOUBLE_
               Return DoubleToString(m_BTLParamM.dMax, 3)
            Case BTLParamType.LENGTH
               Return LenToString(m_BTLParamM.dMax, 3)
            Case Else
               Return ""
         End Select
      End Get
   End Property

   Public ReadOnly Property dMax As Double
      Get
         Return m_BTLParamM.dMax
      End Get
   End Property

   ' parametri da geometria
   Public Property sValue As String
      Get
         Select Case m_BTLParamM.nType
            Case BTLParamType.DOUBLE_
               Return DoubleToString(m_BTLParamM.dValue, 3)
            Case BTLParamType.LENGTH
               Return LenToString(m_BTLParamM.dValue, 3)
            Case Else      ' stringhe
               Return m_BTLParamM.sValue
         End Select
         Return If(m_BTLParamM.nType = BTLParamType.LENGTH, LenToString(m_BTLParamM.dValue, 3), DoubleToString(m_BTLParamM.dValue, 3))
      End Get
      Set(value As String)
         Dim dNewValue As Double
         ' trasformo valori
         Select Case m_BTLParamM.nType
            Case BTLParamType.DOUBLE_
               StringToDoubleAdv(value, dNewValue, True)
            Case BTLParamType.LENGTH
               StringToLenAdv(value, dNewValue, True)
            Case Else
               ' per string non faccio nulla
         End Select
         Select Case m_BTLParamM.nType
            Case BTLParamType.DOUBLE_, BTLParamType.LENGTH
               ' verifico che sia compreso tra minimo e massimo
               If dNewValue < m_BTLParamM.dMin OrElse dNewValue > m_BTLParamM.dMax Then
                  MessageBox.Show(EgtMsg(61856), EgtMsg(30007))
                  NotifyPropertyChanged("sValue")
                  Return
               End If
               UpdateParamValue(dNewValue, "")
            Case Else
               UpdateParamValue(0, value)
         End Select
         NotifyPropertyChanged(NameOf(bCustom))
      End Set
   End Property
   Public Property dValue As Double
      Get
         Return m_BTLParamM.dValue
      End Get
      Set(value As Double)
         m_BTLParamM.dValue = value
         NotifyPropertyChanged(NameOf(sValue))
      End Set
   End Property

   Public Property sDefault As String
      Get
         Select Case m_BTLParamM.nType
            Case BTLParamType.DOUBLE_
               Return DoubleToString(m_BTLParamM.dDefault, 3)
            Case BTLParamType.LENGTH
               Return LenToString(m_BTLParamM.dDefault, 3)
            Case Else
               Return ""
         End Select
      End Get
      Set(value As String)
         Dim dNewValue As Double
         ' trasformo valori
         Select Case m_BTLParamM.nType
            Case BTLParamType.DOUBLE_
               StringToDoubleAdv(value, dNewValue, True)
            Case BTLParamType.LENGTH
               StringToLenAdv(value, dNewValue, True)
            Case Else
               ' per string non faccio nulla
         End Select
         Select Case m_BTLParamM.nType
            Case BTLParamType.DOUBLE_, BTLParamType.LENGTH
               ' verifico che sia compreso tra minimo e massimo
               If dNewValue < m_BTLParamM.dMin OrElse dNewValue > m_BTLParamM.dMax Then
                  MessageBox.Show(EgtMsg(61856), EgtMsg(30007))
                  NotifyPropertyChanged(NameOf(sDefault))
                  Return
               End If
               m_BTLParamM.dDefault = dNewValue
               m_BTLParamM.bIsModified = True
            Case Else
               m_BTLParamM.dDefault = 0
               m_BTLParamM.bIsModified = True
         End Select
      End Set
   End Property

   Public ReadOnly Property nType As BTLParamType
      Get
         Return m_BTLParamM.nType
      End Get
   End Property

   Protected m_sDrawPath As String
   Public ReadOnly Property sDrawPath As String
      Get
         Return Map.refMainWindowVM.MainWindowM.sResourcesRoot & "\Parameters\" & m_BTLFeatureM.sDescGRP & "_" & m_BTLParamM.sName & "_" & m_BTLFeatureM.nSelGRP & ".gif"
      End Get
   End Property

   Public Property bCustom As Boolean
      Get
         Return m_BTLParamM.bCustom
      End Get
      Set(value As Boolean)
         ' Se de-checko il parametro settiamo il suo valore a quello di default
         Dim bFound As Boolean = False
         If Not value Then
            ' Cerco il parametro Q direttamente nel file ini
            Dim GRPType As Integer = Me.m_BTLFeatureM.nSelGRP
            Dim PRC As Integer = Me.m_BTLFeatureM.nPRC
            Dim ParamIndex As String = 1
            Dim NewBTLParam As BTLParamM = Nothing
            While BTLIniFile.GetBeamPrivateProfileParam(GRPType, PRC, False, ParamIndex, Nothing, NewBTLParam)
               Dim QBTLParam As BTLParamVM = New BTLParamVM(NewBTLParam)
               If QBTLParam.sName = Me.sName Then
                  Me.sValue = QBTLParam.sDefault
                  NotifyPropertyChanged(NameOf(sValue))
                  m_BTLParamM.bCustom = value
                  ' setto nel DB che il parametro attuale è custom o meno
                  EgtSetInfo(m_BTLFeatureM.nFeatureId, m_BTLParamM.sName & "A", If(value, "1", ""))
                  NotifyPropertyChanged(NameOf(bCustom))
                  Exit While
               End If
               ParamIndex += 1
            End While
         End If
      End Set
   End Property

   Public ReadOnly Property sBWType As String
      Get
         Return m_BTLParamM.sBWType
      End Get
   End Property

#Region "CONSTRUCTOR"

   Sub New(BTLParamM As BTLParamM, BTLFeatureM As BTLFeatureM)
      m_BTLParamM = BTLParamM
      m_BTLFeatureM = BTLFeatureM
   End Sub

   Sub New(BTLParamM As BTLParamM)
      m_BTLParamM = BTLParamM
      m_BTLFeatureM = Nothing
   End Sub

   '' new per double e length
   'Sub New(ParentFeature As BTLFeatureVM, IsP As Boolean, nType As BTLParamType, sParamName As String, dMin As Double, dMax As Double, dDefault As Double, sDescription As String)
   '   MyBase.New(ParentFeature, IsP, nType, sParamName, dMin, dMax, dDefault, sDescription)
   'End Sub

   '' new per stringhe
   'Sub New(ParentFeature As BTLFeatureVM, IsP As Boolean, nType As BTLParamType, sParamName As String, sDescription As String)
   '   MyBase.New(ParentFeature, IsP, nType, sParamName, sDescription)
   'End Sub

   '' new per combobox
   'Sub New(ParentFeature As BTLFeatureVM, IsP As Boolean, nType As BTLParamType, sParamName As String, nMin As Integer, nMax As Integer, nDefault As Integer, sDescription As String, ValueList As List(Of String))
   '   MyBase.New(ParentFeature, IsP, nType, sParamName, nMin, nMax, nDefault, sDescription, ValueList)
   'End Sub

   '' new per parametro vuoto
   'Sub New(IsP As Boolean, nType As BTLParamType, sParamName As String)
   '   MyBase.New(IsP, nType, sParamName)
   'End Sub

#End Region    ' CONSTRUCTOR

   Friend Sub UpdateParamValue(dNewValue As Double, sNewValue As String, Optional bDraw As Boolean = True)
      ' verifico se solido attivo
      Dim bShowSolid As Boolean = Map.refShowBeamPanelVM.ShowSolid_IsChecked
      If bShowSolid Then Map.refProjectVM.BTLStructureVM.ShowSolid(GDB_ID.NULL, False, False)
      ' Recupero i parametri attuali della feature
      Dim vPar() As Double = Nothing
      Dim sPar As String = String.Empty
      Dim vParQ() As String = Nothing
      m_BTLFeatureM.CalcParamArray(vPar, sPar, vParQ)
      ' se P
      If m_BTLParamM.bIsP Then
         ' modifico parametro cambiato con value
         Select Case m_BTLParamM.nType
            Case BTLParamType.DOUBLE_, BTLParamType.LENGTH
               vPar(m_BTLParamM.nId - 1) = dNewValue
            Case BTLParamType.STRING_
               sPar = sNewValue
         End Select
         Dim bOK As Boolean = m_BTLFeatureM.UpdateParams(m_BTLFeatureM.nSelGRP, m_BTLFeatureM.nPRC, m_BTLFeatureM.nSelSIDE, m_BTLFeatureM.sDES,
                                                         m_BTLFeatureM.nPRID, m_BTLFeatureM.frFRAME, vPar, sPar, vParQ)
         If bOK Then
            Select Case m_BTLParamM.nType
               Case BTLParamType.DOUBLE_, BTLParamType.LENGTH
                  m_BTLParamM.dValue = dNewValue
                  NotifyPropertyChanged(NameOf(sValue))
               Case BTLParamType.STRING_
                  m_BTLParamM.sValue = sNewValue
                  NotifyPropertyChanged(NameOf(sValue))
            End Select
         Else
            NotifyPropertyChanged(NameOf(sValue))
         End If
         ' se Q
      Else
         For Ind = 0 To vParQ.Length() - 1
            If vParQ(Ind).StartsWith(m_BTLParamM.sName) Then
               vParQ(Ind) = m_BTLParamM.sName & ":" & DoubleToString(dNewValue, 3)
            End If
         Next
         Dim bOK As Boolean = m_BTLFeatureM.UpdateParams(m_BTLFeatureM.nSelGRP, m_BTLFeatureM.nPRC, m_BTLFeatureM.nSelSIDE, m_BTLFeatureM.sDES,
                                                         m_BTLFeatureM.nPRID, m_BTLFeatureM.frFRAME, vPar, sPar, vParQ)

         If bOK Then
            m_BTLParamM.bCustom = True
            ' se sono in ottimizzazione 
            If Map.refMainMenuVM.SelPage = Pages.VIEW Then
               ' setto nel progetto il fatto che il parametro attuale è custom o meno
               EgtSetInfo(m_BTLFeatureM.nFeatureId, m_BTLParamM.sName & "A", If(bCustom, "1", ""))
               NotifyPropertyChanged(NameOf(bCustom))
            End If
            m_BTLParamM.dValue = dNewValue
            ' modifica provvisoria per mancanza aggiornamento su freecontour
            If m_BTLFeatureM.IsFreeContour Then
               EgtSetInfo(m_BTLFeatureM.nFeatureId, sName, dNewValue)
            End If
            NotifyPropertyChanged(NameOf(dValue))
         Else
            NotifyPropertyChanged(NameOf(dValue))
         End If
      End If

      If bShowSolid Then Map.refProjectVM.BTLStructureVM.ShowSolid(GDB_ID.NULL, True, False)
      If bDraw Then EgtDraw()

   End Sub

End Class
