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

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

Public Sub Init(db As InfoDatabase, linkSheet As Excel.Worksheet, Optional nRow& = HT03_DEFAULT_FIRST_LINE)
  Set db_ = db
  Set sheet_ = linkSheet
  row_ = nRow
End Sub

Public Function Increment(Optional nStep& = 1) As Boolean
  Increment = row_ + nStep >= 0
  If Increment Then _
    row_ = row_ + nStep
End Function

Public Function GoFirst()
  row_ = HT03_DEFAULT_FIRST_LINE
End Function

Public Function GoLast()
  row_ = sheet_.Columns(S_L_FROM_ID).Find(vbNullString, LookAt:=xlWhole).Row - 1
  If row_ < HT03_DEFAULT_FIRST_LINE Then _
    row_ = HT03_DEFAULT_FIRST_LINE
End Function

Public Function GotoInsertion()
  row_ = sheet_.Columns(S_L_FROM_ID).Find(vbNullString, LookAt:=xlWhole).Row
End Function

Public Function IsDone() As Boolean
  IsDone = sheet_.Cells(row_, LinkStruct.S_L_TO_ID) = vbNullString
End Function

Public Function RemoveRow()
  Call sheet_.Rows(row_).Delete
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 nSource&: nSource = SourceID
  Dim sourceStatus As TLawStatus: sourceStatus = db_.Cache(nSource).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 nSource = nDestination Then _
    IsProcessed = Row = db_.Cache(nSource).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(SourceID) 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(SourceID).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 ImportData(src As Object)
  SourceID = src.SourceID
  SourceSub = src.SourceSub
  DestinationID = src.DestinationID
  DestinationSub = src.DestinationSub
  LType = src.LType
  Status = src.Status
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 SourceID(nID&)
  sheet_.Cells(row_, LinkStruct.S_L_FROM_ID) = nID
End Property

Public Property Let DestinationSub(nSub$)
  sheet_.Cells(row_, LinkStruct.S_L_TO_SUB) = nSub
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 SourceID() As Long
  SourceID = CLng(sheet_.Cells(row_, LinkStruct.S_L_FROM_ID))
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
