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

Private cLinkDoc_(TLinkType.[_First] To TLinkType.[_Last]) As Long
Private cLinkTotal_(TLinkType.[_First] To TLinkType.[_Last]) As Long
Private cDocType_(1 To TLawType.[_Last]) As Long

Private addTBA_ As Boolean
Private dest_ As InfoDatabase

Private outIgnore_ As Excel.Worksheet
Private ignoreCount_ As Long

Private outLaws_ As Excel.Worksheet

Public Function Init(dest As InfoDatabase, addTBA As Boolean)
  Set dest_ = dest
  Set outIgnore_ = ThisWorkbook.Sheets(SHT_IGNORE)
  Set outLaws_ = ThisWorkbook.Sheets(SHT_DOCS)
  addTBA_ = addTBA
End Function

Public Function LoadDocs(ByRef iSource As IteratorLaw)
  ignoreCount_ = 0
  Do While Not iSource.IsDone
    If iSource.IsActive Then
      Call dest_.SetItem(iSource.Data)
      cDocType_(iSource.DType) = cDocType_(iSource.DType) + 1
    ElseIf iSource.Status = T_LAWS_IGNORE Then
      Call AddIgnored(iSource)
    End If
    Call iSource.Increment
    Call CSE_ProgressBar.IncrementA(DOCS_PROGRESS_FACTOR)
    Call CSE_ProgressBar.IncrementB
  Loop
  Call UpdateDocCounts
End Function

Public Function LoadLinks(ByRef iSource As IteratorLinkAll)
  Dim iDestination As IteratorLinkAll: Set iDestination = dest_.Links
  Call iDestination.GotoInsertion

  Dim docID&: docID = HT03_INVALID_GID
  Do While Not iSource.IsDone
    iSource.IsActive
    If Not CheckLink(iSource) Then _
      GoTo NEXT_LINK
      
    If docID <> iSource.SourceID Then
      If docID <> HT03_INVALID_GID Then
        Call OuputDocStats(docID)
        Call AggregateTotals
      End If
      docID = iSource.SourceID
      dest_.Data(docID).LinkBase = iDestination.Row
    End If
    
    Call iDestination.ImportData(iSource)
    cLinkDoc_(iSource.LType) = cLinkDoc_(iSource.LType) + 1
    Call iDestination.Increment
    
NEXT_LINK:
    Call iSource.Increment
    Call CSE_ProgressBar.IncrementA
    Call CSE_ProgressBar.IncrementB
  Loop

  If docID <> HT03_INVALID_GID Then
    Call OuputDocStats(docID)
    Call AggregateTotals
  End If
  
  Call UpdateLinkCounts
End Function

' ==========
Private Function CheckLink(iLink As IteratorLinkAll) As Boolean
  CheckLink = False
  
  Dim srcID&: srcID = iLink.SourceID
  Dim destID&: destID = iLink.DestinationID
  If srcID = destID Then _
    Exit Function
  If iLink.Status = T_LINKS_IGNORE Then _
    Exit Function
  If Not addTBA_ Then _
    If iLink.LType = T_LINK_DEFAULT Then _
      Exit Function
  
  If Not dest_.Contains(srcID) Or Not dest_.Contains(destID) Then _
    Exit Function
  
  Dim sourceStatus As TLawStatus: sourceStatus = dest_.Cache(srcID).status_
  If sourceStatus = T_LAWS_OUTER Then _
    Exit Function
  
  If sourceStatus = T_LAWS_FRINGE Then _
    If dest_.Cache(destID).status_ = T_LAWS_NOTDEFINED Then _
      Exit Function

  CheckLink = True
End Function

Private Function AddIgnored(source As IteratorLaw)
  Dim outRow&: outRow = HT03_DEFAULT_FIRST_LINE + ignoreCount_
  outIgnore_.Cells(outRow, S_I_GARANT_ID) = source.ID
  outIgnore_.Cells(outRow, S_I_TYPE) = source.DType
  outIgnore_.Cells(outRow, S_I_FILENAME) = source.FileName
  outIgnore_.Cells(outRow, S_I_TITLE) = source.Title
  
  ignoreCount_ = ignoreCount_ + 1
End Function

Private Function UpdateDocCounts()
  ThisWorkbook.Names(STATS_DOCS_IGNORE).RefersToRange = ignoreCount_
  Dim totalCount&
  Dim nType&
  For nType = 1 To TLawType.[_Last]
    ThisWorkbook.Names(STATS_DOCS_TOTAL).RefersToRange.Offset(nType - TLawType.[_Last] - 1) = cDocType_(nType)
    totalCount = totalCount + cDocType_(nType)
  Next nType
  ThisWorkbook.Names(STATS_DOCS_TOTAL).RefersToRange = totalCount
End Function

Private Function UpdateLinkCounts()
  ThisWorkbook.Names(STATS_LNK_DEFAULT).RefersToRange = cLinkTotal_(T_LINK_DEFAULT)
  ThisWorkbook.Names(STATS_LNK_ASPECT).RefersToRange = cLinkTotal_(T_LINK_ASPECT)
  ThisWorkbook.Names(STATS_LNK_DEFINE).RefersToRange = cLinkTotal_(T_LINK_DEFINITION)
  ThisWorkbook.Names(STATS_LNK_FOUND).RefersToRange = cLinkTotal_(T_LINK_FOUNDATION)
  ThisWorkbook.Names(STATS_LNK_TOTAL).RefersToRange = cLinkTotal_(T_LINK_DEFAULT) _
                                                      + cLinkTotal_(T_LINK_ASPECT) _
                                                      + cLinkTotal_(T_LINK_DEFINITION) _
                                                      + cLinkTotal_(T_LINK_FOUNDATION)
End Function

Private Function OuputDocStats(target&)
  Dim nRow&: nRow = dest_.Cache(target).row_
  outLaws_.Cells(nRow, SS_LINKS_DEFAULT) = cLinkDoc_(T_LINK_DEFAULT)
  outLaws_.Cells(nRow, SS_LINKS_ASPECT) = cLinkDoc_(T_LINK_ASPECT)
  outLaws_.Cells(nRow, SS_LINKS_DEFINE) = cLinkDoc_(T_LINK_DEFINITION)
  outLaws_.Cells(nRow, SS_LINKS_FOUND) = cLinkDoc_(T_LINK_FOUNDATION)
  outLaws_.Cells(nRow, SS_LINKS_TOTAL) = cLinkDoc_(T_LINK_DEFAULT) _
                                      + cLinkDoc_(T_LINK_FOUNDATION) _
                                      + cLinkDoc_(T_LINK_ASPECT) _
                                      + cLinkDoc_(T_LINK_DEFINITION)
End Function

Private Function AggregateTotals()
  Dim nType&
  For nType = TLinkType.[_First] To TLinkType.[_Last]
    cLinkTotal_(nType) = cLinkTotal_(nType) + cLinkDoc_(nType)
    cLinkDoc_(nType) = 0
  Next nType
End Function
