VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "IteratorLinkLaw"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private docID_ As Long
Private row_ As Long
Private sheet_ As Excel.Worksheet
Private db_ As InfoDatabase

Private Sub Class_Initialize()
  docID_ = HT03_INVALID_GID
  row_ = HT03_INVALID_ROW
End Sub

Public Sub Init(db As InfoDatabase, linkSheet As Excel.Worksheet, gid&)
  Set db_ = db
  Set sheet_ = linkSheet
  docID_ = gid
  row_ = db_.Cache(docID_).linkBase_
End Sub

Public Function Clone() As IteratorLinkLaw
  Set Clone = New IteratorLinkLaw
  Call Clone.Init(db_, sheet_, docID_)
  Call Clone.Increment(row_ - db_.Cache(docID_).linkBase_)
End Function

Public Function IsNull() As Boolean
  IsNull = db_ Is Nothing Or row_ = HT03_INVALID_ROW
End Function

Public Function Increment(Optional nStep& = 1) As Boolean
  Increment = False
  If IsNull Or row_ + nStep <= 0 Then _
    Exit Function
  If CLng(sheet_.Cells(row_ + nStep, S_L_FROM_ID)) <> docID_ Then _
    Exit Function
  row_ = row_ + nStep
  Increment = True
End Function

Public Function ForceSetRow(nRow&)
  row_ = nRow
End Function

Public Function GoFirst()
  row_ = db_.Cache(docID_).linkBase_
End Function

Public Function Find(target$, colN As LinkStruct) As Boolean
  Dim foundRng As Excel.Range
  Set foundRng = sheet_.Columns(colN).Find(target, LookAt:=xlWhole)
  If foundRng Is Nothing Then
    row_ = HT03_INVALID_ROW
    Find = False
  Else
    row_ = foundRng.Row
    Find = True
  End If
  Set foundRng = sheet_.Columns(colN).Find("", LookAt:=xlPart)
End Function

Public Function IsProcessed() As Boolean
  IsProcessed = True
  If Status = T_LINKS_IGNORE Then _
    Exit Function
  
  Dim nDestination&: nDestination = DestinationID
  Dim destStatus As TLawStatus: destStatus = db_.GetLawStatus(nDestination)
  If destStatus = T_LAWS_IGNORE Then _
    Exit Function

  Dim sourceStatus As TLawStatus: sourceStatus = db_.Cache(docID_).status_
  If sourceStatus = T_LAWS_OUTER Or _
    (sourceStatus = T_LAWS_FRINGE And destStatus = T_LAWS_NOTDEFINED) Then _
    Exit Function

  IsProcessed = LType <> T_LINK_DEFAULT And db_.Contains(nDestination)
  If Not IsProcessed And docID_ = nDestination Then _
    IsProcessed = Row = db_.Cache(docID_).linkBase_
End Function

Public Function IsActive() As Boolean
  IsActive = False
  If Status = T_LINKS_IGNORE Or LType = T_LINK_DEFAULT Then _
    Exit Function

  If Not db_.Contains(DestinationID) Or Not db_.Contains(docID_) Then _
    Exit Function

  Dim targetStatus As TLawStatus: targetStatus = db_.GetLawStatus(DestinationID)
  If targetStatus = T_LAWS_IGNORE Then _
    Exit Function

  Dim sourceStatus As TLawStatus: sourceStatus = db_.Cache(docID_).status_
  If sourceStatus = T_LAWS_OUTER Or sourceStatus = T_LAWS_IGNORE Or _
    (sourceStatus = T_LAWS_FRINGE And targetStatus = T_LAWS_NOTDEFINED) Then _
    Exit Function

  IsActive = True
End Function

'===== Let =====
Public Property Let DestinationID(nID&)
  sheet_.Cells(row_, LinkStruct.S_L_TO_ID) = nID
End Property

Public Property Let Status(nStat As TLinkStatus)
   sheet_.Cells(row_, LinkStruct.S_L_STATUS) = nStat
End Property

Public Property Let LType(nType As TLinkType)
  sheet_.Cells(row_, LinkStruct.S_L_TYPE) = nType
End Property

Public Property Let SourceSub(nSub$)
  sheet_.Cells(row_, LinkStruct.S_L_FROM_SUB) = nSub
End Property

'===== Get =====
Public Property Get Row() As Long
  Row = row_
End Property

Public Property Get DestinationID() As Long
  DestinationID = CLng(sheet_.Cells(row_, LinkStruct.S_L_TO_ID))
End Property

Public Property Get DestinationSub() As String
  DestinationSub = sheet_.Cells(row_, LinkStruct.S_L_TO_SUB)
End Property

Public Property Get SourceSub() As String
  SourceSub = sheet_.Cells(row_, LinkStruct.S_L_FROM_SUB)
End Property

Public Property Get Status() As TLinkStatus
  Status = CInt(sheet_.Cells(row_, LinkStruct.S_L_STATUS))
End Property

Public Property Get LType() As TLinkType
  LType = CInt(sheet_.Cells(row_, LinkStruct.S_L_TYPE))
End Property
