Attribute VB_Name = "ManualSubs"
Option Private Module
Option Explicit

Public Sub RunClearAll()
  If UserInteraction.AskQuestion(QM_DELETE_SELF_CONFIRM) Then
    Call ClearAll
    Call UserInteraction.ShowMessage(IM_DELETE_LAW_OK)
  End If
End Sub

Public Sub UpdateParsedData()
  Dim theDB As InfoDatabase: Set theDB = AccessDatabase
  
  Dim wordAppl As New API_WordWrapper
  Call wordAppl.CreateApplication(bIsVisible:=False)
  
  Call CSE_ProgressBar.Init("Загрузка данных из документов", maxVal:=theDB.Count - 1, canInterrupt:=True)
  Call CSE_ProgressBar.Show
  
  Dim uiWrap As New API_XLWrapper: Call uiWrap.SetDocument(ThisWorkbook)
  Call uiWrap.PauseUI
  
  Dim iLaw As IteratorLaw: Set iLaw = theDB.Laws
  Do While Not iLaw.IsDone
    If CSE_ProgressBar.Interrupted Then _
      GoTo EXIT_LOOP
    
    CSE_ProgressBar.DescriptionTB = iLaw.FileName
    If iLaw.FileName <> HT03_FILE_NOT_LOADED And iLaw.HasInfoBlanks Then _
      Call UpdateParsedDataFor(iLaw, wordAppl)
    
    Call iLaw.Increment
    Call CSE_ProgressBar.IncrementA
  Loop
  
EXIT_LOOP:
  Call uiWrap.ResumeUI
  Call Unload(CSE_ProgressBar)
  Call wordAppl.ReleaseApplication
End Sub

Public Sub ParseTitles()
  Dim theDB As InfoDatabase: Set theDB = AccessDatabase
  
  Call CSE_ProgressBar.Init("Загрузка данных из документов", maxVal:=theDB.Count - 1, canInterrupt:=True)
  Call CSE_ProgressBar.Show
  
  Dim uiWrap As New API_XLWrapper: Call uiWrap.SetDocument(ThisWorkbook)
  Call uiWrap.PauseUI
  
  Dim iLaw As IteratorLaw: Set iLaw = theDB.Laws
  Do While Not iLaw.IsDone
    If CSE_ProgressBar.Interrupted Then _
      GoTo EXIT_LOOP
    
    CSE_ProgressBar.DescriptionTB = iLaw.Title
    Call UpdateParsedTitle(iLaw, iLaw.Title)
    
    Call iLaw.Increment
    Call CSE_ProgressBar.IncrementA
  Loop
  
EXIT_LOOP:
  Call uiWrap.ResumeUI
  Call Unload(CSE_ProgressBar)
End Sub

' ============
Private Function UpdateParsedDataFor(target As IteratorLaw, wordAppl As API_WordWrapper)
  If wordAppl.OpenDocument(GetDocumentsLocation(ThisWorkbook) & "\" & target.FileName, bReadOnly:=True) Is Nothing Then _
    Exit Function
  
  Dim sTitle$: sTitle = wordAppl.Document.Paragraphs(1).Range.Text
  target.IsDummy = CheckIfDummy(wordAppl.Document)
  
  Call UpdateParsedTitle(target, sTitle)
  
SAFE_EXIT:
  Call wordAppl.ReleaseDocument(bCloseApplication:=False)
End Function

Private Function UpdateParsedTitle(target As IteratorLaw, sTitle$)
  Dim theData As ItemLaw: Set theData = ParseLawTitle(sTitle)
  If theData Is Nothing Then _
    Exit Function
  
  If target.TypeText = vbNullString Then _
    target.TypeText = theData.typeTxt_
  If target.Creator = vbNullString Then _
    target.Creator = theData.creator_
  If target.CreationDate = vbNullString Then _
    target.CreationDate = theData.date_
  If target.Number = vbNullString Then _
    target.Number = theData.number_
  If target.Header = vbNullString Then _
    target.Header = theData.header_
  If target.Attachment = vbNullString Then _
    target.Attachment = theData.attach_
  target.IsScoped = theData.isScoped_
End Function

Private Function CheckIfDummy(target As Word.Document) As Boolean
  With target.Range.Find
    .Text = HT03_DUMMY_DOCUMENT_DISCLAIMER
    CheckIfDummy = .Execute
  End With
End Function
