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

Public Class AddRawPartWndVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Public Enum EditTypes As Integer
      ONLY_TEXT = 1
      ONLY_COMBO = 2
      TEXT_AND_COMBO = 3
   End Enum

   Friend Event m_CloseWindow(bDialogResult As Boolean)

   ' flag che indica se trave o parete
   Private m_nProjectType As BWType = BWType.BEAM
   Private m_EditType As EditTypes = EditTypes.ONLY_TEXT

   ' lista delle variabili da mostrare
   Private m_VariableList_View As CollectionView = Nothing
   Private m_VariableList As ObservableCollection(Of Variable)
   Public ReadOnly Property VariableList As ObservableCollection(Of Variable)
      Get
         Return m_VariableList
      End Get
   End Property

   Private m_DimensionsList As List(Of SParam)
   Public Property DimensionsList As List(Of SParam)
      Get
         Return m_DimensionsList
      End Get
      Set(value As List(Of SParam))
         m_DimensionsList = value
      End Set
   End Property

   Private m_SelDimension As SParam
   Public Property SelDimension As SParam
      Get
         Return m_SelDimension
      End Get
      Set(value As SParam)
         m_SelDimension = value
         If m_EditType = EditTypes.ONLY_COMBO OrElse m_EditType = EditTypes.TEXT_AND_COMBO Then
            ' aggiorno textbox
            Select Case m_nProjectType
               Case BWType.BEAM
                  m_VariableList(0).SetValue(value.dL)
               Case BWType.WALL
                  m_VariableList(0).SetValue(value.dW)
                  m_VariableList(1).SetValue(value.dL)
            End Select
         End If
      End Set
   End Property

   Private m_DimensionsList_Visibility As Visibility
   Public ReadOnly Property DimensionsList_Visibility As Visibility
      Get
         Return If(m_EditType = EditTypes.ONLY_COMBO OrElse m_EditType = EditTypes.TEXT_AND_COMBO, Visibility.Visible, Visibility.Collapsed)
      End Get
   End Property

   ' Definizione comandi
   Private m_cmdOk As ICommand

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Sub New(MachineType As Integer, dL As Double, dW As Double, dPosx As Double)
      m_nProjectType = MachineType
      m_EditType = EditTypes.ONLY_TEXT
      Select Case m_nProjectType
         Case ConstBeam.MachineType.BEAM
            m_VariableList = New ObservableCollection(Of Variable)({New Variable(Variable.VariableType.LENGTH, EgtMsg(61935), dL),                ' lunghezza
                                                                    New Variable(Variable.VariableType.LENGTH, EgtMsg(61816), dPosx)})            ' ritaglio iniziale
         Case ConstBeam.MachineType.WALL
            m_VariableList = New ObservableCollection(Of Variable)({New Variable(Variable.VariableType.LENGTH, EgtMsg(61935), dL),                ' lunghezza
                                                                    New Variable(Variable.VariableType.LENGTH, EgtMsg(61936), dW)})               ' larghezza
      End Select
   End Sub

   Sub New(EditType As EditTypes, ProjectType As BWType, DimensionsList As List(Of SParam), dL As Double, dW As Double, dPosx As Double, dOffset As Double, dKerf As Double)
      m_EditType = EditType
      m_nProjectType = ProjectType
      Select Case ProjectType
         Case BWType.BEAM
            m_VariableList = New ObservableCollection(Of Variable)({New Variable(Variable.VariableType.LENGTH, EgtMsg(61935), dL + dPosx),        ' lunghezza + ritaglio iniziale
                                                                    New Variable(Variable.VariableType.LENGTH, EgtMsg(61816), dPosx),             ' ritaglio iniziale
                                                                    New Variable(Variable.VariableType.LENGTH, EgtMsg(61937), dOffset)})          ' offset
         Case BWType.WALL
            m_VariableList = New ObservableCollection(Of Variable)({New Variable(Variable.VariableType.LENGTH, EgtMsg(61936), dW),                ' larghezza
                                                                    New Variable(Variable.VariableType.LENGTH, EgtMsg(61935), dL),                ' lunghezza
                                                                    New Variable(Variable.VariableType.LENGTH, EgtMsg(61938), dKerf),             ' kerf
                                                                    New Variable(Variable.VariableType.LENGTH, EgtMsg(61937), dOffset)})          ' offset
      End Select
      m_DimensionsList = DimensionsList
      If m_DimensionsList.Count > 0 Then
         Dim LastMaterial As SParam = WarehouseHelper.GetLastMaterial(DimensionsList(0).SectXMat)
         If IsNothing(LastMaterial) Then
            SelDimension = m_DimensionsList(0)
         Else
            Dim TempDimension As SParam = m_DimensionsList.FirstOrDefault(Function(x) x.SectXMat = LastMaterial.SectXMat AndAlso x.dL = LastMaterial.dL AndAlso x.dW = LastMaterial.dW AndAlso x.nQuantity = LastMaterial.nQuantity)
            If Not IsNothing(TempDimension) Then
               SelDimension = TempDimension
            Else
               SelDimension = m_DimensionsList(0)
            End If
         End If
      End If
      ' riduco lista parametri a solo quelli che utilizzati
      m_VariableList_View = CollectionViewSource.GetDefaultView(m_VariableList)
      m_VariableList_View.Filter = AddressOf VariableListFilter
      NotifyPropertyChanged(NameOf(DimensionsList_Visibility))
   End Sub

   Private Function VariableListFilter(Variable As Object) As Boolean
      Dim CurrVariable As Variable = DirectCast(Variable, Variable)
      Dim nVariableIndex As Integer = m_VariableList.IndexOf(Variable)
      Select Case m_nProjectType
         Case BWType.BEAM
            If m_EditType = EditTypes.ONLY_TEXT OrElse m_EditType = EditTypes.TEXT_AND_COMBO Then
               Return True
            Else
               Return nVariableIndex = 1 OrElse nVariableIndex = 2
            End If
         Case BWType.WALL
            If m_EditType = EditTypes.ONLY_TEXT OrElse m_EditType = EditTypes.TEXT_AND_COMBO Then
               Return True
            Else
               Return nVariableIndex = 2 OrElse nVariableIndex = 3
            End If
      End Select
      Return False
   End Function

#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
      For Each Variable In m_VariableList
         If (Variable.nType = Variable.VariableType.STRING_ AndAlso IsNothing(Variable.sValue)) OrElse
            ((Variable.nType = Variable.VariableType.DOUBLE_ OrElse Variable.nType = Variable.VariableType.LENGTH) AndAlso IsNothing(Variable.dValue)) Then
            MessageBox.Show(If(m_nProjectType = MachineType.BEAM, EgtMsg(61854), EgtMsg(61855)), EgtMsg(30007))
            Return
         End If
      Next
      ' se non ci sono problemi esco restituendo vero
      RaiseEvent m_CloseWindow(True)
      'If Not IsNothing(m_dL) AndAlso m_dL > 0 AndAlso
      '   Not IsNothing(m_dPOSX) AndAlso m_dPOSX >= 0 Then
      '   RaiseEvent m_CloseWindow(True)
      'Else
      '   MessageBox.Show("Errore! Impossibile creare una barra con questi parametri", "Errore")
      'End If
   End Sub

#End Region ' Ok

#End Region    ' COMMANDS

End Class

Public Class Variable
   Inherits VMBase

   Public Enum VariableType As Integer
      DOUBLE_ = 1
      STRING_ = 2
      COMBO = 3
      LENGTH = 4
   End Enum

   Private m_nType As VariableType
   Public ReadOnly Property nType As VariableType
      Get
         Return m_nType
      End Get
   End Property

   Private m_sMsg As String
   Public Property sMsg As String
      Get
         Return m_sMsg
      End Get
      Set(value As String)
         m_sMsg = value
      End Set
   End Property

   Private m_dValue As Double = 0
   Friend ReadOnly Property dValue As Double
      Get
         Return m_dValue
      End Get
   End Property

   Private m_sValue As String = ""
   Public Property sValue As String
      Get
         Select Case m_nType
            Case VariableType.STRING_
               Return m_sValue
            Case VariableType.LENGTH
               Return LenToString(m_dValue, 3)
            Case Else         ' VariableType.DOUBLE_
               Return DoubleToString(m_dValue, 3)
         End Select
      End Get
      Set(value As String)
         Select Case m_nType
            Case VariableType.STRING_
               m_sValue = value
            Case VariableType.LENGTH
               Dim dTempValue As Double
               If StringToLenAdv(value, dTempValue, True) Then
                  m_dValue = dTempValue
               Else
                  NotifyPropertyChanged("sValue")
               End If
            Case Else         ' VariableType.DOUBLE_
               Dim dTempValue As Double
               If StringToDoubleAdv(value, dTempValue, True) Then
                  m_dValue = dTempValue
               Else
                  NotifyPropertyChanged("sValue")
               End If
         End Select
      End Set
   End Property

   Sub New(Type As VariableType, Msg As String, Value As Double)
      m_nType = Type
      m_sMsg = Msg
      m_dValue = Value
   End Sub

   Friend Sub SetValue(dValue As Double)
      m_dValue = dValue
      NotifyPropertyChanged(NameOf(sValue))
   End Sub

End Class