VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "ActiveStateExporter"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' ===== Export active laws and links ======
Option Explicit

Private out_ As Excel.Workbook
Private lawOut_ As Excel.Worksheet
Private linkOut_ As Excel.Worksheet
Private db_ As InfoDatabase

Public Function Init(outWB As Excel.Workbook, theData As InfoDatabase)
  Set out_ = outWB
  Set db_ = theData
  
  Set lawOut_ = out_.Sheets(1)
  Set linkOut_ = out_.Sheets.Add(After:=lawOut_)

  Call CreateHeaders
End Function

Public Function ExportLaws() As Scripting.Dictionary
  Dim lawIDs As New Scripting.Dictionary
  Dim iLaw As IteratorLaw: Set iLaw = db_.Laws
  Dim nLaw&: nLaw = 1
  Dim nRow&: nRow = HT03_DEFAULT_FIRST_LINE
  Do While Not iLaw.IsDone
    If iLaw.IsActive Then
      lawOut_.Cells(nRow, S_OD_ID) = nLaw
      lawOut_.Cells(nRow, S_OD_LEVEL) = DefaultLawLevelFor(iLaw.DType)
      lawOut_.Cells(nRow, S_OD_TITLE) = iLaw.Title
      lawOut_.Cells(nRow, S_OD_TYPE) = iLaw.DType
      lawOut_.Cells(nRow, S_OD_GID) = iLaw.ID
      Call lawIDs.Add(iLaw.ID, nLaw)
      nLaw = nLaw + 1
      nRow = nRow + 1
    End If
    Call iLaw.Increment
    Call CSE_ProgressBar.IncrementA
    Call CSE_ProgressBar.IncrementB
  Loop
  
  Set ExportLaws = lawIDs
End Function

Public Function ExportLinks(lawIDs As Scripting.Dictionary)
  Dim nRow&: nRow = HT03_DEFAULT_FIRST_LINE
  Dim iLink As IteratorLinkAll: Set iLink = db_.Links
  Do While Not iLink.IsDone
    If Not iLink.IsActive Then _
      GoTo NEXT_LINK
    If iLink.SourceID = iLink.DestinationID Then _
      GoTo NEXT_LINK
    If Not lawIDs.Exists(iLink.SourceID) Or Not lawIDs.Exists(iLink.DestinationID) Then _
      GoTo NEXT_LINK
    
    linkOut_.Cells(nRow, S_OL_SOURCE) = lawIDs(iLink.SourceID)
    linkOut_.Cells(nRow, S_OL_TARGET) = lawIDs(iLink.DestinationID)
    linkOut_.Cells(nRow, S_OL_TYPE) = iLink.LType
    nRow = nRow + 1

NEXT_LINK:
    Call iLink.Increment
    Call CSE_ProgressBar.IncrementA
    Call CSE_ProgressBar.IncrementB
  Loop
  Call linkOut_.Range("A:E").RemoveDuplicates(Columns:=Array(1, 2, 3), Header:=xlYes)
End Function

' ==============
Private Function CreateHeaders()
  With lawOut_
    .Name = "Документы"
    .Cells(1, S_OD_ID) = "ID"
    .Cells(1, S_OD_LEVEL) = "Уровень"
    .Cells(1, S_OD_TITLE) = "Название"
    .Cells(1, S_OD_TYPE) = "Тип"
    .Cells(1, S_OD_GID) = "ID Реестра"
  End With
  With linkOut_
    .Name = "Связи"
    .Cells(1, S_OL_SOURCE) = "Source"
    .Cells(1, S_OL_TARGET) = "Target"
    .Cells(1, S_OL_TYPE) = "Тип связи"
  End With
End Function
