Imports EgtUILib
Imports EgtWPFLib5
Imports EgtBEAMWALL.Core

Module WarehouseHelper

   Public Enum WarehouseTypes As Integer
      BASIC = 1
      MEDIUM = 2
      ADVANCED = 3
   End Enum

   Public Enum RawPartDefs As Integer
      NULL = 0
      EXPLICIT = 1
      FROMWAREHOUSE = 2
   End Enum

   ' funzione che restituisce il tipo di warehouse
   Public Function GetWarehouseType() As WarehouseType
      Return GetMainPrivateProfileInt(S_WAREHOUSE, EgtBEAMWALL.Core.ConstIni.K_TYPE, 2)
   End Function

   ' funzione che restituisce la path dell'ini del warehouse
   Public Function GetWarehouseIniPath() As String
      Dim sWarehouseIniPath As String = Map.refMainWindowVM.MainWindowM.sWarehouseDir
      Select Case GetWarehouseType()
         Case WarehouseType.BASIC
            Return sWarehouseIniPath & "\" & WH_BASIC_INI_FILE_NAME
         Case WarehouseType.MEDIUM
            Return sWarehouseIniPath & "\" & WH_MEDIUM_INI_FILE_NAME
      End Select
      Return ""
   End Function

   ' funzione che restituisce l'offset
   Public Function GetOffset(Type As BWType) As Double
      If Type = BWType.BEAM Then
         Dim dOffset As Double = GenInterface.GetPrivateProfileDouble(WRH_BEAM, WRH_OFFSET, 0, GetWarehouseIniPath())
         Return Math.Max(dOffset, CurrentMachine.dMinOffset)
      Else
         Return GenInterface.GetPrivateProfileDouble(WRH_WALL, WRH_OFFSET, 0, GetWarehouseIniPath())
      End If
   End Function

   ' funzione che restituisce l'offset dato spessore pareti
   Public Function GetWallOffsetByWidth(dHeight As Double) As Double
      Dim dOffset As Double = 0
      Dim nRangeIndex As Integer = 1
      Dim sRange As String = ""
      While GenInterface.GetPrivateProfileString(WRH_WALL, WRH_RANGE & nRangeIndex, "", sRange, GetWarehouseIniPath()) >= 0 AndAlso Not String.IsNullOrWhiteSpace(sRange)
         Dim sRanges() As String = sRange.Split(","c)
         If sRanges.Count() >= 2 Then
            Dim dMaxThickness As Double = 0
            StringToDoubleAdv(sRanges(1), dMaxThickness)
            If dHeight <= dMaxThickness Then
               If StringToDoubleAdv(sRanges(0), dOffset) Then Return dOffset
            End If
         End If
         nRangeIndex += 1
      End While
      Return 0
   End Function

   Public Function SetOffset(Type As BWType, Offset As Double) As Boolean
      Return GenInterface.WritePrivateProfileString(If(Type = BWType.BEAM, WRH_BEAM, WRH_WALL), WRH_OFFSET, Offset, GetWarehouseIniPath())
   End Function

#Region "Beam"

   ' funzione che restituisce l'offset iniziale
   Public Function GetStartOffset() As Double
      Return GenInterface.GetPrivateProfileDouble(WRH_BEAM, WRH_STARTOFFSET, 0, GetWarehouseIniPath())
   End Function

   Public Function SetStartOffset(StartOffset As Double) As Boolean
      Return GenInterface.WritePrivateProfileString(WRH_BEAM, WRH_STARTOFFSET, StartOffset, GetWarehouseIniPath())
   End Function

#End Region   ' Beam

#Region "Wall"

   ' funzione che restituisce l'offset
   Public Function GetKerf() As Double
      Return GenInterface.GetPrivateProfileDouble(WRH_WALL, WRH_KERF, 0, GetWarehouseIniPath())
   End Function

   Public Function SetKerf(Kerf As Double) As Boolean
      Return GenInterface.WritePrivateProfileString(WRH_WALL, WRH_KERF, Kerf, GetWarehouseIniPath())
   End Function

#End Region   ' Wall

   ' funzione che restituisce le dimensioni correnti
   Public Function GetCurrentDimensions(Type As BWType, ByRef dW As Double, ByRef dL As Double) As Boolean
      If Type = BWType.BEAM Then
         Dim nCurrentL As Integer = EgtUILib.GenInterface.GetPrivateProfileInt(WRH_BEAM, WRH_CURRENT & WRH_L, 1, GetWarehouseIniPath())
         If nCurrentL <= 0 Then Return False
         dL = GenInterface.GetPrivateProfileInt(WRH_BEAM, WRH_L & nCurrentL, 0, GetWarehouseIniPath())
         Return True
      Else
         Dim nCurrentS As Integer = EgtUILib.GenInterface.GetPrivateProfileInt(WRH_WALL, WRH_CURRENT & WRH_S, nCurrentS, GetWarehouseIniPath())
         Dim sPanelDim As String = ""
         Dim sPanelDims() As String
         GenInterface.GetPrivateProfileString(WRH_WALL, WRH_S & nCurrentS, "", sPanelDim, GetWarehouseIniPath())
         If String.IsNullOrWhiteSpace(sPanelDim) Then Return False
         sPanelDims = sPanelDim.Split(","c)
         If Not sPanelDims.Count = 2 OrElse Not StringToDoubleAdv(sPanelDims(0), dW) OrElse Not StringToDoubleAdv(sPanelDims(1), dL) Then Return False
         Return True
      End If
   End Function

   ' funzione che restituisce la quantita' generica di grezzi
   Public Function GetQuantity(Type As BWType) As Integer
      Dim nQuantity As Integer = 0
      Integer.TryParse(EgtUILib.GenInterface.GetPrivateProfileDouble(If(Type = BWType.BEAM, WRH_BEAM, WRH_WALL), WRH_QUANTITY, 0, GetWarehouseIniPath()), nQuantity)
      Return nQuantity
   End Function

   ' funzione che restituisce la quantita' di grezzi per sezione
   Public Function GetDimensionsAndQuantityBySection(Type As BWType, ByRef dW As Double, dH As Double, ByRef dL As Double, Material As String, nQuantity As Integer) As Boolean
      Dim nIndex As Integer = 1
      Dim sSection As String = ""
      While GenInterface.GetPrivateProfileString(If(Type = BWType.BEAM, WRH_BEAM, WRH_WALL), If(Type = BWType.BEAM, WRH_L, WRH_S) & nIndex, "", sSection, GetWarehouseIniPath()) > 0
         If String.IsNullOrWhiteSpace(sSection) Then Return False
         Dim sSectionValues() As String = sSection.Split(","c)
         Select Case Type
            Case BWType.BEAM
               If dW = sSectionValues(0) AndAlso dH = sSectionValues(1) AndAlso Material = sSectionValues(2) Then
                  If Not StringToDoubleAdv(sSectionValues(3), dL) Then Return False
                  If Not Integer.TryParse(sSectionValues(4), nQuantity) Then Return False
                  Return True
               End If
            Case BWType.WALL
               If dH = sSectionValues(0) AndAlso Material = sSectionValues(1) Then
                  If Not StringToDoubleAdv(sSectionValues(2), dW) Then Return False
                  If Not StringToDoubleAdv(sSectionValues(3), dL) Then Return False
                  If Not Integer.TryParse(sSectionValues(4), nQuantity) Then Return False
                  Return True
               End If
         End Select
         nIndex += 1
      End While
      Return False
   End Function


   ' funzione che restituisce la quantita' di grezzi per sezione
   Public Function GetDimensionsAndQuantityForList(Type As BWType, ByRef SectionList As List(Of SectionProgress)) As Boolean
      Dim nIndex As Integer = 1
      Dim sSection As String = ""
      While GenInterface.GetPrivateProfileString(If(Type = BWType.BEAM, WRH_BEAM, WRH_WALL), If(Type = BWType.BEAM, WRH_L, WRH_S) & nIndex, "", sSection, GetWarehouseIniPath()) > 0
         If String.IsNullOrWhiteSpace(sSection) Then Continue While
         Dim sSectionValues() As String = sSection.Split(","c)
         Dim dW As Double = 0
         Dim dL As Double = 0
         Dim nQty As Integer = 0
         Dim nActive As Integer = 0
         Dim sSectionData As String = ""
         GetPrivateProfileString(If(Type = BWType.BEAM, S_BEAM_LIST, S_WALL_LIST), sSectionValues(0), "", sSectionData, GetWarehouseIniPath())
         Dim SectionData() As String = sSectionData.Split(","c)
         Select Case Type
            Case BWType.BEAM
               Dim dSectionW As Double = 0 : StringToDoubleAdv( SectionData(0), dSectionW)
               Dim dSectionH As Double = 0 : StringToDoubleAdv( SectionData(1), dSectionH)
               For Each SectionProgress In SectionList
                  If Math.Abs( SectionProgress.Section.dW - dSectionW) < 0.1 AndAlso
                     Math.Abs( SectionProgress.Section.dH - dSectionH) < 0.1 AndAlso SectionProgress.Section.sMaterial.Contains(SectionData(2)) Then
                     If Not StringToDoubleAdv(sSectionValues(1), dL) Then Continue While
                     If Not Integer.TryParse(sSectionValues(2), nQty) Then Continue While
                     If sSectionValues.Count >= 4 AndAlso Integer.TryParse(sSectionValues(3), nActive) Then
                        If nActive > 0 Then SectionProgress.SParamList.Add(New SParam(SectionProgress.Section, dL, nQty, nActive > 0))
                     Else
                        SectionProgress.SParamList.Add(New SParam(SectionProgress.Section, dL, nQty, True))
                        nIndex += 1
                        Continue While
                     End If
                  End If
               Next
            Case BWType.WALL
               Dim dSectionH As Double = 0 : StringToDoubleAdv( SectionData(0), dSectionH)
               For Each SectionProgress In SectionList
                  If Math.Abs( SectionProgress.Section.dH - dSectionH) < 0.1 AndAlso SectionProgress.Section.sMaterial.Contains(SectionData(1)) Then
                     If Not StringToDoubleAdv(sSectionValues(1), dW) Then Continue While
                     If Not StringToDoubleAdv(sSectionValues(2), dL) Then Continue While
                     If Not Integer.TryParse(sSectionValues(3), nQty) Then Continue While
                     If Not Integer.TryParse(sSectionValues(4), nActive) Then Continue While
                     If nActive > 0 Then SectionProgress.SParamList.Add(New SParam(SectionProgress.Section, dW, dL, nQty, nActive > 0))
                  End If
               Next
         End Select
         nIndex += 1
      End While
      Return True
   End Function

   Public Function GetSectionList() As List(Of SParam)
      Dim Sectionlist As New List(Of SParam)
      Dim nIndex As Integer = 1
      Dim CurrSParam As SParam = WarehouseWndVM.GetSParamFromWarehouse(nIndex)
      While Not IsNothing(CurrSParam)
         Sectionlist.Add(CurrSParam)
         nIndex += 1
         CurrSParam = WarehouseWndVM.GetSParamFromWarehouse(nIndex)
      End While
      Return Sectionlist
   End Function

   ' GetSectionList
   'Public Function GetSectionList(Type As BWType, ByRef dW As Double, ByRef dL As Double, Qty As Integer) As Boolean
   '   Dim Sectionlist As List(Of SParam)
   '   Dim Index As Integer = 1
   '   Dim nIndex As Integer = 1
   '   Dim sSection As String = ""
   '   While GenInterface.GetPrivateProfileString(If(Type = BWType.BEAM, WRH_BEAM, WRH_WALL), If(Type = BWType.BEAM, WRH_L, WRH_S) & nIndex, "", sSection, GetWarehouseIniPath()) > 0
   '      If String.IsNullOrWhiteSpace(sSection) Then Return False
   '      Dim sSectionValues() As String = sSection.Split(","c)
   '      Select Case Type
   '         Case BWType.BEAM
   '            If sSectionValues.Count >= 5 Then
   '               Sectionlist.Add(New SectionXMaterial(sSectionValues(0), sSectionValues(1), sSectionValues(3), sSectionValues(2)))
   '            End If
   '            If dW = sSectionValues(0) AndAlso dH = sSectionValues(1) AndAlso Material = sSectionValues(2) Then
   '               If Not StringToDoubleAdv(sSectionValues(3), dL) Then Return False
   '               If Not Integer.TryParse(sSectionValues(4), nQuantity) Then Return False
   '               Return True
   '            End If
   '         Case BWType.WALL
   '            If dH = sSectionValues(1) AndAlso Material = sSectionValues(2) Then
   '               If Not StringToDoubleAdv(sSectionValues(2), dW) Then Return False
   '               If Not StringToDoubleAdv(sSectionValues(3), dL) Then Return False
   '               If Not Integer.TryParse(sSectionValues(4), nQuantity) Then Return False
   '               Return True
   '            End If
   '      End Select
   '      nIndex += 1
   '   End While
   'End Function

   ' funzione che restituisce modalita' di definizione del grezzo
   Public Function GetRawPartDef() As Integer
      Return GetMainPrivateProfileInt(S_NEST, K_RAWPARTDEF, 2)
   End Function

   ' funzione che restituisce la validità della sezione
   Public Function IsSectionValid(SectXMat As SectionXMaterial) As Boolean
      Return SectXMat.dH >= 0 Or
         SectXMat.dL >= 0 Or
         SectXMat.dW >= 0
   End Function

   Friend Function GetLastMaterial(SectXMat As SectionXMaterial) As SParam
      Dim sWarehousePath As String = Map.refMainWindowVM.MainWindowM.sWarehouseDir & "\" & WH_MEDIUM_INI_FILE_NAME
      If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
         Dim ParamIndex As Integer = 1
         Dim sSxMValue As String = ""
         While EgtUILib.GetPrivateProfileString(S_BEAM_LIST, ParamIndex, String.Empty, sSxMValue, sWarehousePath) > 0
            Dim sSectXMatValues() As String = sSxMValue.Split(","c)
            ' creo parametro
            Dim dW As Double = 0
            Dim dH As Double = 0
            Dim sMaterial As String = sSectXMatValues(2)
            StringToLenAdv(sSectXMatValues(0), dW)
            StringToLenAdv(sSectXMatValues(1), dH)
            If SectXMat.dW = dW AndAlso SectXMat.dH = dH AndAlso SectXMat.sMaterial(0) = sMaterial Then
               If sSectXMatValues.Length <= 3 Then Return Nothing
               Dim sValue As String = ""
               If EgtUILib.GetPrivateProfileString(S_BEAM, "L" & sSectXMatValues(3), String.Empty, sValue, sWarehousePath) Then
                  Dim sParamValues() As String = sValue.Split(","c)
                  ' verifico numero minimo di parametri
                  If sParamValues.Count < 2 Then Return Nothing
                  ' cancello spazi
                  For Index = 0 To sParamValues.Count - 1
                     sParamValues(Index) = sParamValues(Index).Trim()
                  Next
                  Dim dL As Double = 0
                  Dim nQty As Integer = 0
                  Dim nActive As Integer = 1
                  StringToLenAdv(sParamValues(1), dL)
                  If sParamValues.Count >= 3 Then Integer.TryParse(sParamValues(2), nQty)
                  If sParamValues.Count >= 4 Then Integer.TryParse(sParamValues(3), nActive)
                  Return New SParam(New SectionXMaterial(dW, dH, dL, sSectXMatValues(2)), dL, nQty, nActive > 0)
               End If
            End If
            ParamIndex += 1
         End While
      Else
         Dim ParamIndex As Integer = 1
         Dim sSxMValue As String = ""
         While EgtUILib.GetPrivateProfileString(S_WALL_LIST, ParamIndex, String.Empty, sSxMValue, sWarehousePath) > 0
            Dim sSectXMatValues() As String = sSxMValue.Split(","c)
            ' creo parametro
            Dim dH As Double = 0
            Dim sMaterial As String = sSectXMatValues(1)
            StringToLenAdv(sSectXMatValues(0), dH)
            If SectXMat.dH = dH AndAlso SectXMat.sMaterial(0) = sMaterial Then
               If sSectXMatValues.Length <= 2 Then Return Nothing
               Dim sValue As String = ""
               If EgtUILib.GetPrivateProfileString(S_WALL, "S" & sSectXMatValues(2), String.Empty, sValue, sWarehousePath) Then
                  Dim sParamValues() As String = sValue.Split(","c)
                  ' verifico numero minimo di parametri
                  If sParamValues.Count < 3 Then Return Nothing
                  ' cancello spazi
                  For Index = 0 To sParamValues.Count - 1
                     sParamValues(Index) = sParamValues(Index).Trim()
                  Next
                  Dim dW As Double = 0
                  Dim dL As Double = 0
                  Dim nQty As Integer = 0
                  Dim nActive As Integer = 1
                  StringToLenAdv(sParamValues(1), dW)
                  StringToLenAdv(sParamValues(2), dL)
                  If sParamValues.Count >= 3 Then Integer.TryParse(sParamValues(3), nQty)
                  If sParamValues.Count >= 4 Then Integer.TryParse(sParamValues(4), nActive)
                  Return New SParam(New SectionXMaterial(dW, dH, dL, sSectXMatValues(1)), dW, dL, nQty, nActive > 0)
               End If
            End If
            ParamIndex += 1
         End While
      End If

      Return Nothing
   End Function

   Friend Sub SetLastMaterial(SectXMat As SectionXMaterial, SParam As SParam)
      Dim sWarehousePath As String = Map.refMainWindowVM.MainWindowM.sWarehouseDir & "\" & WH_MEDIUM_INI_FILE_NAME
      If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
         ' cerco sezione 
         Dim SectIndex As Integer = 1
         Dim sSxMValue As String = ""
         While EgtUILib.GetPrivateProfileString(S_BEAM_LIST, SectIndex, String.Empty, sSxMValue, sWarehousePath) > 0
            Dim sSectXMatValues() As String = sSxMValue.Split(","c)
            ' creo parametro
            Dim dW As Double = 0
            Dim dH As Double = 0
            Dim sMaterial As String = sSectXMatValues(2)
            StringToLenAdv(sSectXMatValues(0), dW)
            StringToLenAdv(sSectXMatValues(1), dH)
            If SectXMat.dW = dW AndAlso SectXMat.dH = dH AndAlso SectXMat.sMaterial(0) = sMaterial Then
               ' recupero indice di SParam
               Dim SParamIndex As Integer = 1
               Dim sValue As String = ""
               While EgtUILib.GetPrivateProfileString(S_BEAM, "L" & SParamIndex, String.Empty, sValue, sWarehousePath)
                  Dim sParamValues() As String = sValue.Split(","c)
                  ' verifico numero minimo di parametri
                  If sParamValues.Count < 2 Then Return
                  ' cancello spazi
                  For Index = 0 To sParamValues.Count - 1
                     sParamValues(Index) = sParamValues(Index).Trim()
                  Next
                  Dim dL As Double = 0
                  Dim nSectIndex As Integer = sParamValues(0)
                  Dim nQty As Integer = 0
                  Dim nActive As Integer = 1
                  StringToLenAdv(sParamValues(1), dL)
                  If sParamValues.Count >= 3 Then Integer.TryParse(sParamValues(2), nQty)
                  If sParamValues.Count >= 4 Then Integer.TryParse(sParamValues(3), nActive)
                  Dim bActive As Boolean = nActive = 1
                  If dL = SParam.dL AndAlso nQty = SParam.nQuantity AndAlso bActive = SParam.bActive Then
                     ' scrivo indice ultimo materiale
                     EgtUILib.WritePrivateProfileString(S_BEAM_LIST, SectIndex, LenToString(dW, 3) & "," &
                                                                LenToString(dH, 3) & "," &
                                                                sMaterial & "," &
                                                                SParamIndex, sWarehousePath)
                  End If
                  SParamIndex += 1
               End While
            End If
            SectIndex += 1
         End While
      Else
         ' cerco sezione 
         Dim SectIndex As Integer = 1
         Dim sSxMValue As String = ""
         While EgtUILib.GetPrivateProfileString(S_WALL_LIST, SectIndex, String.Empty, sSxMValue, sWarehousePath) > 0
            Dim sSectXMatValues() As String = sSxMValue.Split(","c)
            ' creo parametro
            Dim dH As Double = 0
            Dim sMaterial As String = sSectXMatValues(1)
            StringToLenAdv(sSectXMatValues(0), dH)
            If SectXMat.dH = dH AndAlso SectXMat.sMaterial(0) = sMaterial Then
               ' recupero indice di SParam
               Dim SParamIndex As Integer = 1
               Dim sValue As String = ""
               While EgtUILib.GetPrivateProfileString(S_WALL, "S" & SParamIndex, String.Empty, sValue, sWarehousePath)
                  Dim sParamValues() As String = sValue.Split(","c)
                  ' verifico numero minimo di parametri
                  If sParamValues.Count < 3 Then Return
                  ' cancello spazi
                  For Index = 0 To sParamValues.Count - 1
                     sParamValues(Index) = sParamValues(Index).Trim()
                  Next
                  Dim dW As Double = 0
                  Dim dL As Double = 0
                  Dim nSectIndex As Integer = sParamValues(0)
                  Dim nQty As Integer = 0
                  Dim nActive As Integer = 1
                  StringToLenAdv(sParamValues(1), dW)
                  StringToLenAdv(sParamValues(2), dL)
                  If sParamValues.Count >= 4 Then Integer.TryParse(sParamValues(3), nQty)
                  If sParamValues.Count >= 5 Then Integer.TryParse(sParamValues(4), nActive)
                  Dim bActive As Boolean = nActive = 1
                  If dW = SParam.dW AndAlso dL = SParam.dL AndAlso nQty = SParam.nQuantity AndAlso bActive = SParam.bActive Then
                     ' scrivo indice ultimo materiale
                     EgtUILib.WritePrivateProfileString(S_WALL_LIST, SectIndex,
                                                                LenToString(dH, 3) & "," &
                                                                sMaterial & "," &
                                                                SParamIndex, sWarehousePath)
                  End If
                  SParamIndex += 1
               End While
            End If
            SectIndex += 1
         End While
      End If
   End Sub

   Friend Function GetSectIndexFromSection(Type As BWType, Section As SectionXMaterial) As Integer
      Dim nIndex As Integer = 1
      Dim sSectionData As String = ""
      While GetPrivateProfileString(If(Type = BWType.BEAM, S_BEAM_LIST, S_WALL_LIST), nIndex, "", sSectionData, GetWarehouseIniPath()) > 0
         If String.IsNullOrWhiteSpace(sSectionData) Then Continue While
         Dim sSectionValues() As String = sSectionData.Split(","c)
         Select Case Type
            Case BWType.BEAM
               Dim dW As Double = 0
               Dim dH As Double = 0
               Dim sMaterial As String = sSectionValues(2)
               StringToLenAdv(sSectionValues(0), dW)
               StringToLenAdv(sSectionValues(1), dH)
               If Section.dW = dW AndAlso Section.dH = dH AndAlso Section.sMaterial(0) = sMaterial Then
                  ' se l'ho trovato esco dal while, il SectXMatIndex è l'indice che cercavamo
                  Return nIndex
               End If
            Case BWType.WALL
               Dim dH As Double = 0
               Dim sMaterial As String = sSectionValues(1)
               StringToLenAdv(sSectionValues(0), dH)
               If Section.dH = dH AndAlso Section.sMaterial(0) = sMaterial Then
                  ' se l'ho trovato esco dal while, il SectXMatIndex è l'indice che cercavamo
                  Return nIndex
               End If
         End Select
         nIndex += 1
      End While
      Return -1
   End Function

End Module
