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

Private data_() As Long

Private baseRow_ As Long
Private itemCount_ As Long

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

Private Sub Class_Initialize()
  docID_ = HT03_INVALID_GID
End Sub

Public Sub Init(db As InfoDatabase, linkSheet As Excel.Worksheet, gid&, ByRef cachedData() As Long)
  data_ = cachedData
  Set db_ = db
  Set sheet_ = linkSheet
  docID_ = gid
  
  baseRow_ = db_.Cache(docID_).linkBase_
  nItem_ = 1
  itemCount_ = UBound(data_, 1) - LBound(data_, 1) + 1
End Sub

Public Function Increment(Optional nStep& = 1) As Boolean
  Increment = nItem_ + nStep >= 0 And nItem_ + nStep <= itemCount_
  If Increment Then _
    nItem_ = nItem_ + nStep
End Function

Public Function GoFirst()
  nItem_ = 1
End Function

Public Function IsFirst() As Boolean
  IsFirst = nItem_ = 1
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

Public Function FlipStatus()
  Status = IIf(Status = T_LINKS_IGNORE, T_LINKS_ADD, T_LINKS_IGNORE)
End Function

'===== Let =====
Public Property Let DestinationID(nID&)
  sheet_.Cells(Row, LinkStruct.S_L_TO_ID) = nID
  data_(nItem_, S_CL_DESTINATION_ID) = nID
End Property

Public Property Let Status(nStat As TLinkStatus)
   sheet_.Cells(Row, LinkStruct.S_L_STATUS) = nStat
   data_(nItem_, S_CL_STATUS) = nStat
End Property

Public Property Let LType(nType As TLinkType)
  sheet_.Cells(Row, LinkStruct.S_L_TYPE) = nType
  data_(nItem_, S_CL_TYPE) = nType
End Property

'===== Get =====
Public Property Get Row() As Long
  Row = baseRow_ + nItem_ - 1
End Property

Public Property Get Count() As Long
  Count = itemCount_
End Property

Public Property Get DestinationID() As Long
  DestinationID = data_(nItem_, S_CL_DESTINATION_ID)
End Property

Public Property Get LType() As TLinkType
  LType = data_(nItem_, S_CL_TYPE)
End Property

Public Property Get Status() As TLinkStatus
  Status = data_(nItem_, S_CL_STATUS)
End Property
