Imports System.IO
Imports EgtUILib
Imports EgtWPFLib5

Public Class BTLStructureM

   Private m_nBTLInfoId As Integer = 0
   Public ReadOnly Property nBTLInfoId As Integer
      Get
         Return m_nBTLInfoId
      End Get
   End Property

   Private m_nAsseBaseId As Integer = GDB_ID.NULL
   Public ReadOnly Property nAsseBaseId As Integer
      Get
         Return m_nAsseBaseId
      End Get
   End Property

   ' pezzi da BTL
   Public m_BTLPartMList As List(Of BTLPartM)
   Public Property BTLPartMList As List(Of BTLPartM)
      Get
         Return m_BTLPartMList
      End Get
      Set(value As List(Of BTLPartM))
         m_BTLPartMList = value
      End Set
   End Property

   ' grezzi da BTL
   Public m_BTLRawPartList As List(Of BTLRawPart)

#Region "Parametri generici"

   Public m_sPROJNUM As String
   Public Property sPROJNUM As String
      Get
         Return m_sPROJNUM
      End Get
      Set(value As String)
         m_sPROJNUM = value
      End Set
   End Property

   Public m_sPROJNAME As String
   Public Property sPROJNAME As String
      Get
         Return m_sPROJNAME
      End Get
      Set(value As String)
         m_sPROJNAME = value
      End Set
   End Property

   Public m_sPROJPART As String
   Public Property sPROJPART As String
      Get
         Return m_sPROJPART
      End Get
      Set(value As String)
         m_sPROJPART = value
      End Set
   End Property

   Public m_sPROJGUID As String
   Public Property sPROJGUID As String
      Get
         Return m_sPROJGUID
      End Get
      Set(value As String)
         m_sPROJGUID = value
      End Set
   End Property

   Public m_nPROJTYPE As Integer
   Public Property nPROJTYPE As Integer
      Get
         Return m_nPROJTYPE
      End Get
      Set(value As Integer)
         m_nPROJTYPE = value
      End Set
   End Property

   Public m_sLISTNAME As String
   Public Property sLISTNAME As String
      Get
         Return m_sLISTNAME
      End Get
      Set(value As String)
         m_sLISTNAME = value
      End Set
   End Property

   Public m_sCUSTOMER As String
   Public Property sCUSTOMER As String
      Get
         Return m_sCUSTOMER
      End Get
      Set(value As String)
         m_sCUSTOMER = value
      End Set
   End Property

   Public m_sARCHITECT As String
   Public Property sARCHITECT As String
      Get
         Return m_sARCHITECT
      End Get
      Set(value As String)
         m_sARCHITECT = value
      End Set
   End Property

   Public m_sEDITOR As String
   Public Property sEDITOR As String
      Get
         Return m_sEDITOR
      End Get
      Set(value As String)
         m_sEDITOR = value
      End Set
   End Property

   Public m_sDELIVDATE As String
   Public Property sDELIVDATE As String
      Get
         Return m_sDELIVDATE
      End Get
      Set(value As String)
         m_sDELIVDATE = value
      End Set
   End Property

   Public m_sEXPDATE As String
   Public Property sEXPDATE As String
      Get
         Return m_sEXPDATE
      End Get
      Set(value As String)
         m_sEXPDATE = value
      End Set
   End Property

   Public m_sEXPTIME As String
   Public Property sEXPTIME As String
      Get
         Return m_sEXPTIME
      End Get
      Set(value As String)
         m_sEXPTIME = value
      End Set
   End Property

   Public m_sEXPRELEASE As String
   Public Property sEXPRELEASE As String
      Get
         Return m_sEXPRELEASE
      End Get
      Set(value As String)
         m_sEXPRELEASE = value
      End Set
   End Property

   Public m_sLANGUAGE As String
   Public Property sLANGUAGE As String
      Get
         Return m_sLANGUAGE
      End Get
      Set(value As String)
         m_sLANGUAGE = value
      End Set
   End Property

   Public m_ocRANGE As New List(Of Object)({New IdNameStruct(Range.STANDARD, EgtMsg(61710)),
                                                               New IdNameStruct(Range.EXTENDED, EgtMsg(61711))})
   Public ReadOnly Property ocRANGE As List(Of Object)
      Get
         Return m_ocRANGE
      End Get
   End Property

   Public m_SelRANGE As Integer
   Public Property SelRANGE As Integer
      Get
         Return m_SelRANGE
      End Get
      Set(value As Integer)
         m_SelRANGE = value
      End Set
   End Property

   Public m_ocPROCESSINGQUALITY As New List(Of Object)({New IdNameStruct(ProcessingQuality.AUTOMATIC, EgtMsg(61658)),
                                                                          New IdNameStruct(ProcessingQuality.VISIBLE, EgtMsg(61659)),
                                                                          New IdNameStruct(ProcessingQuality.FAST, EgtMsg(61660))})
   Public ReadOnly Property ocPROCESSINGQUALITY As List(Of Object)
      Get
         Return m_ocPROCESSINGQUALITY
      End Get
   End Property

   Public m_SelPROCESSINGQUALITY As Integer
   Public Property SelPROCESSINGQUALITY As Integer
      Get
         Return m_SelPROCESSINGQUALITY
      End Get
      Set(value As Integer)
         m_SelPROCESSINGQUALITY = value
      End Set
   End Property

   Public m_sCOMPUTERNAME As String
   Public Property sCOMPUTERNAME As String
      Get
         Return m_sCOMPUTERNAME
      End Get
      Set(value As String)
         m_sCOMPUTERNAME = value
      End Set
   End Property

   Public m_sUSER As String
   Public Property sUSER As String
      Get
         Return m_sUSER
      End Get
      Set(value As String)
         m_sUSER = value
      End Set
   End Property

   Public m_sSRCFILE As String
   Public Property sSRCFILE As String
      Get
         Return m_sSRCFILE
      End Get
      Set(value As String)
         m_sSRCFILE = value
      End Set
   End Property

   Public m_sEXPFILE As String
   Public Property sEXPFILE As String
      Get
         Return m_sEXPFILE
      End Get
      Set(value As String)
         m_sEXPFILE = value
      End Set
   End Property

   Public m_ocRECESS As New List(Of Object)({New IdNameStruct(Recess.AUTOMATIC, EgtMsg(61658)),
                                                                New IdNameStruct(Recess.MANUAL, EgtMsg(61661))})
   Public ReadOnly Property ocRECESS As List(Of Object)
      Get
         Return m_ocRECESS
      End Get
   End Property

   Public m_SelRECESS As Integer
   Public Property SelRECESS As Integer
      Get
         Return m_SelRECESS
      End Get
      Set(value As Integer)
         m_SelRECESS = value
      End Set
   End Property

   Public m_sUSERATTRIBUTE As String
   Public Property sUSERATTRIBUTE As String
      Get
         Return m_sUSERATTRIBUTE
      End Get
      Set(value As String)
         m_sUSERATTRIBUTE = value
      End Set
   End Property

#End Region ' Parametri generici

#Region "MESSAGES"

   ' Info del pezzo
   Public ReadOnly Property PDN_Msg As String
      Get
         Return EgtMsg(61601)
      End Get
   End Property

   Public ReadOnly Property DO_Msg As String
      Get
         Return EgtMsg(61602)
      End Get
   End Property

   Public ReadOnly Property NAM_Msg As String
      Get
         Return EgtMsg(61603)
      End Get
   End Property

   Public ReadOnly Property L_Msg As String
      Get
         Return EgtMsg(61604)
      End Get
   End Property

   Public ReadOnly Property W_Msg As String
      Get
         Return EgtMsg(61605)
      End Get
   End Property

   Public ReadOnly Property H_Msg As String
      Get
         Return EgtMsg(61606)
      End Get
   End Property

   Public ReadOnly Property MAT_Msg As String
      Get
         Return EgtMsg(61607)
      End Get
   End Property

   Public ReadOnly Property CNT_Msg As String
      Get
         Return EgtMsg(61608)
      End Get
   End Property

   Public ReadOnly Property TBP_Msg As String
      Get
         Return EgtMsg(61609)
      End Get
   End Property

   Public ReadOnly Property DON_Msg As String
      Get
         Return EgtMsg(61610)
      End Get
   End Property

   Public ReadOnly Property ROT_Msg As String
      Get
         Return EgtMsg(61611)
      End Get
   End Property

   Public ReadOnly Property GRP_Msg As String
      Get
         Return EgtMsg(61612)
      End Get
   End Property

   Public ReadOnly Property UNT_Msg As String
      Get
         Return EgtMsg(61613)
      End Get
   End Property

#End Region ' MESSAGES

#Region "CONSTRUCTOR"

   Protected Sub New()
      m_BTLPartMList = New List(Of BTLPartM)
   End Sub

   Public Shared Function CreateNewBTLStructure() As BTLStructureM
      Return New BTLStructureM
   End Function

   Public Shared Function CreateBTLStructure(ProjId As Integer) As BTLStructureM
      Dim NewBTLStructure As New BTLStructureM
      Dim sValue As String = String.Empty
      ' se devo filtrare un progetto
      If ProjId > 0 Then
         ' cerco tra i layer BTLInfo
         Dim nBTLInfoLayerId As Integer = EgtGetFirstNameInGroup(GDB_ID.ROOT, BTLINFO)
         While nBTLInfoLayerId <> GDB_ID.NULL
            ' verifico se il layer appartiene al ProjId
            Dim nBTLInfoLayerProjId As Integer
            EgtGetInfo(nBTLInfoLayerId, BTL_PRT_PROJ, nBTLInfoLayerProjId)
            If nBTLInfoLayerProjId = ProjId Then
               NewBTLStructure.m_nBTLInfoId = nBTLInfoLayerId
               Exit While
            End If
            nBTLInfoLayerId = EgtGetNextName(nBTLInfoLayerId, BTLINFO)
         End While
         ' cerco tra gli AsseBase
         Dim nAsseBaseLayerId As Integer = EgtGetFirstNameInGroup(GDB_ID.ROOT, ASSEBASE)
         While nAsseBaseLayerId <> GDB_ID.NULL
            ' verifico se il layer appartiene al ProjId
            Dim nAsseBaseLayerProjId As Integer
            EgtGetInfo(nAsseBaseLayerId, BTL_PRT_PROJ, nAsseBaseLayerProjId)
            If nAsseBaseLayerProjId = ProjId Then
               NewBTLStructure.m_nAsseBaseId = nAsseBaseLayerId
               Exit While
            End If
            nAsseBaseLayerId = EgtGetNextName(nAsseBaseLayerId, ASSEBASE)
         End While
      Else
         NewBTLStructure.m_nAsseBaseId = EgtGetFirstNameInGroup(GDB_ID.ROOT, ASSEBASE)
         NewBTLStructure.m_nBTLInfoId = EgtGetFirstNameInGroup(GDB_ID.ROOT, BTLINFO)
      End If
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_PROJNUM, NewBTLStructure.m_sPROJNUM)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_PROJNAME, NewBTLStructure.m_sPROJNAME)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_PROJPART, NewBTLStructure.m_sPROJPART)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_PROJGUID, NewBTLStructure.m_sPROJGUID)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_PROJTYPE, NewBTLStructure.m_nPROJTYPE)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_LISTNAME, NewBTLStructure.m_sLISTNAME)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_CUSTOMER, NewBTLStructure.m_sCUSTOMER)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_ARCHITECT, NewBTLStructure.m_sARCHITECT)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_EDITOR, NewBTLStructure.m_sEDITOR)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_DELIVDATE, NewBTLStructure.m_sDELIVDATE)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_EXPDATE, NewBTLStructure.m_sEXPDATE)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_EXPTIME, NewBTLStructure.m_sEXPTIME)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_EXPRELEASE, NewBTLStructure.m_sEXPRELEASE)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_LANGUAGE, NewBTLStructure.m_sLANGUAGE)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_RANGE, sValue)
      NewBTLStructure.m_SelRANGE = IdNameStruct.IndFromId(IdNameStruct.IdFromName(sValue, NewBTLStructure.ocRANGE), NewBTLStructure.ocRANGE)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_PRT_PROCESSINGQUALITY, sValue)
      NewBTLStructure.m_SelPROCESSINGQUALITY = IdNameStruct.IndFromId(IdNameStruct.IdFromName(sValue, NewBTLStructure.ocPROCESSINGQUALITY), NewBTLStructure.ocPROCESSINGQUALITY)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_COMPUTERNAME, NewBTLStructure.m_sCOMPUTERNAME)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_USER, NewBTLStructure.m_sUSER)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_SRCFILE, NewBTLStructure.m_sSRCFILE)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_EXPFILE, NewBTLStructure.m_sEXPFILE)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_PRT_RECESS, sValue)
      NewBTLStructure.m_SelRECESS = IdNameStruct.IndFromId(IdNameStruct.IdFromName(sValue, NewBTLStructure.ocRECESS), NewBTLStructure.ocRECESS)
      EgtGetInfo(NewBTLStructure.m_nBTLInfoId, BTL_GEN_USERATTRIBUTE, NewBTLStructure.m_sUSERATTRIBUTE)
      ' setto il tipo di progetto (Beam o Wall) nel file ini del BTL
      If NewBTLStructure.m_nPROJTYPE <> BWType.NULL Then BTLIniFile.m_nBTLBWType = NewBTLStructure.m_nPROJTYPE
      ' recupero i part
      NewBTLStructure.m_BTLPartMList = LoadParts(ProjId)
      'Dim nPartId As Integer = EgtGetFirstPart()
      'While nPartId <> GDB_ID.NULL
      '   NewBTLStructure.m_BTLPartMList.Add(BTLPartM.CreateBTLPart(nPartId))
      '   nPartId = EgtGetNextPart(nPartId)
      'End While
      Return NewBTLStructure
   End Function

   Private Shared Function LoadParts(ProjId As Integer) As List(Of BTLPartM)
      Dim TempList As New List(Of BTLPartM)
      Dim nPartId As Integer = EgtGetFirstPart()
      While nPartId <> GDB_ID.NULL
         ' se devo filtrare un progetto
         If ProjId > 0 Then
            ' verifico se il pezzo appartiene al ProjId
            Dim nPartProjId As Integer
            EgtGetInfo(nPartId, BTL_PRT_PROJ, nPartProjId)
            If nPartProjId = ProjId Then
               TempList.Add(BTLPartM.CreateBTLPart(nPartId))
            End If
         Else
            ' altrimenti carico tutti
            TempList.Add(BTLPartM.CreateBTLPart(nPartId))
         End If
         nPartId = EgtGetNextPart(nPartId)
      End While
      Return TempList
   End Function

#End Region    ' CONSTRUCTOR

#Region "METHODS"

   Public Event BTLPartAdded As EventHandler(Of BTLPartAddedEventArgs)

   Public Sub AddBTLPart(BTLPartM As BTLPartM)
      If IsNothing(BTLPartM) Then Return
      If Not m_BTLPartMList.Contains(BTLPartM) Then
         m_BTLPartMList.Add(BTLPartM)
         RaiseEvent BTLPartAdded(Me, New BTLPartAddedEventArgs(BTLPartM))
      End If
   End Sub

   Public Sub RemoveBTLPart(BTLPartM As BTLPartM)
      If IsNothing(BTLPartM) Then Return
      If Not m_BTLPartMList.Contains(BTLPartM) Then Return
      m_BTLPartMList.Remove(BTLPartM)
      'RaiseEvent BTLPartAdded(Me, New BTLPartAddedEventArgs(BTLPartM))
   End Sub

   Public Function GetBTLParts() As List(Of BTLPartM)
      Return New List(Of BTLPartM)(m_BTLPartMList)
   End Function

   Public Function NewPDN() As Integer
      Dim nMaxPDN As Integer = 0
      If Not IsNothing(BTLPartMList) And BTLPartMList.Count > 0 Then
         nMaxPDN = BTLPartMList.Max(Function(x) x.nPDN)
      End If
      Return nMaxPDN + 1
   End Function

   Public Sub UpdateBTLInfoLayer()
      m_nBTLInfoId = EgtGetFirstNameInGroup(GDB_ID.ROOT, BTLINFO)
   End Sub

#End Region    ' METHODS

End Class

Public Class BTLRawPart

End Class

