Attribute VB_Name = "Main"
' ========== UI subroutines ============
Option Explicit

Public Sub HT03_MergeID(nDelete&, nSubstitute&, sTitle$)
  Call ExecuteLinksSubstitution(Word.Application.ActiveDocument, nDelete, nSubstitute, sTitle)
End Sub

Public Sub HT03_AddOrUpdate()
  Dim iDoc As InfoDocument: Set iDoc = ParseDocument(Word.Application.ActiveDocument)
  If iDoc Is Nothing Then
    Set iDoc = New InfoDocument
    Set iDoc.doc_ = Word.Application.ActiveDocument
  End If
  
  Dim theDB As InfoWordDatabase: Set theDB = AccessWordDatabase
  If Not theDB.source_.Loaded Then _
    Exit Sub
    
  If Not theDB.Contains(iDoc.gid_) Then
    Call AddLawInternal(iDoc, theDB)
  Else
    Call HT03_UpdateMarkup
  End If
End Sub

Public Sub HT03_AddDoc()
  Dim iDoc As InfoDocument: Set iDoc = ParseDocument(Word.Application.ActiveDocument)
  If iDoc Is Nothing Then
    Set iDoc = New InfoDocument
    Set iDoc.doc_ = Word.Application.ActiveDocument
  End If
  
  Dim theDB As InfoWordDatabase: Set theDB = AccessWordDatabase
  If Not theDB.source_.Loaded Then _
    Exit Sub
  
  Call AddLawInternal(iDoc, theDB)
End Sub

Public Sub HT03_UpdateMarkup()
  Dim iDoc As InfoDocument:  Set iDoc = GetActiveDocument
  If iDoc Is Nothing Then _
    Exit Sub
  Dim theDB As InfoWordDatabase: Set theDB = AccessWordDatabase
  If Not theDB.source_.Loaded Then _
    Exit Sub
  
  Dim sFile$: sFile = theDB.source_.Data(iDoc.gid_).FileName
  If sFile <> iDoc.doc_.Name Then
    Call UserInteraction.ShowMessage(EM_DOC_FILE_EXISTS, iDoc.gid_, sFile)
    Exit Sub
  End If
  
  Dim docUI As New API_WordWrapper: Call docUI.SetDocument(iDoc.doc_)
  Call docUI.PauseUI
  
  On Error Resume Next
  Call UpdateLinksIn(iDoc, theDB)
  Call ProcessErrorMessages(UniqueList(ERR_SYNC_DOC, ERR_SYNC_DB, ERR_LINKS_MISSING, ERR_SYNC_LINK_ID))
  
  Call docUI.ResumeUI
End Sub

Public Sub HT03_EditDocument()
  Dim iDoc As InfoDocument:  Set iDoc = GetActiveDocument
  If iDoc Is Nothing Then _
    Exit Sub
  
  Dim theDB As InfoWordDatabase: Set theDB = AccessWordDatabase
  
  Dim iLaw As ItemLaw: Set iLaw = theDB.source_.GetLawInfo(iDoc.gid_)
  If iLaw Is Nothing Then
    Set iLaw = iDoc.GetLawDescription()
    Dim iter As IteratorLaw: Set iter = theDB.source_.Data(iDoc.gid_)
    iLaw.status_ = iter.Status
    iLaw.isDummy_ = iter.IsDummy
  End If
    
  Dim props As ItemDocumentProperties: Set props = PromptLawProperties(iLaw, iDoc.linkSystem_, bEdit:=True)
  If Not props Is Nothing Then _
    Call ProcessLawText(iDoc, theDB, props)
End Sub

Public Sub HT03_NextLink()
  Dim iDoc As InfoDocument:  Set iDoc = GetActiveDocument
  If iDoc Is Nothing Then _
    Exit Sub
    
  Dim theDB As InfoDatabase: Set theDB = AccessDatabase
  If Not theDB.Cache(iDoc.gid_).HasLinks Then
    Call UserInteraction.ShowMessage(EM_DOC_NO_LINKS)
    Exit Sub
  End If
  
  Dim iSelected As Word.Range: Set iSelected = ValidateSelectionRange(iDoc.doc_)
  If iSelected Is Nothing Then _
    Exit Sub
  Dim hLink As Word.Hyperlink: Set hLink = iDoc.FindNextUnresolved(iSelected.End)
  If hLink Is Nothing Then
    Call UserInteraction.ShowMessage(IM_DOC_LINKS_DONE)
  Else
    Call hLink.Range.Select
    Call iDoc.doc_.ActiveWindow.ScrollIntoView(hLink.Range)
  End If
End Sub

Public Sub HT03_ChangeLType()
  Dim iDoc As InfoDocument:  Set iDoc = GetActiveDocument
  If iDoc Is Nothing Then _
    Exit Sub
  
  Dim iSelected As Word.Range: Set iSelected = ValidateSelectionRange(iDoc.doc_)
  If iSelected Is Nothing Then _
    Exit Sub
  If iSelected.Hyperlinks.Count = 0 Then
    Call UserInteraction.ShowMessage(EM_LINK_COUNT)
    Exit Sub
  End If
  
  On Error Resume Next
  Dim bResult As Boolean: bResult = ModifyLinkType(iSelected, iDoc, AccessWordDatabase)
  Call ProcessErrorMessages(UniqueList(ERR_INVALID_LINK_NUMBER, ERR_SYNC_DB, ERR_SYNC_LINK_ID))
  If Not bResult Then _
    Call UserInteraction.ShowMessage(EM_NO_VALID_LINKS)
End Sub

Public Sub HT03_ChangeStatus()
  Dim iDoc As InfoDocument:  Set iDoc = GetActiveDocument
  If iDoc Is Nothing Then _
    Exit Sub
    
  Dim iSelected As Word.Range: Set iSelected = ValidateSelectionRange(iDoc.doc_)
  If iSelected Is Nothing Then _
    Exit Sub
  If iSelected.Hyperlinks.Count = 0 Then
    Call UserInteraction.ShowMessage(EM_LINK_COUNT)
    Exit Sub
  End If
  
  On Error Resume Next
  Call ToggleLinksStatusIn(iSelected, iDoc, AccessWordDatabase)
  Call ProcessErrorMessages(UniqueList(ERR_INVALID_LINK_NUMBER, ERR_SYNC_DB, ERR_SYNC_LINK_ID))
End Sub

Public Sub HT03_QueryLink()
  Dim iDoc As InfoDocument:  Set iDoc = GetActiveDocument
  If iDoc Is Nothing Then _
    Exit Sub
  
  Dim iSelected As Word.Range: Set iSelected = ValidateSelectionRange(iDoc.doc_)
  If iSelected Is Nothing Then _
    Exit Sub
  Dim sLink$: sLink = iDoc.GetParagraphLink(iSelected)
  If sLink = vbNullString Then
    Call UserInteraction.ShowMessage(EM_NO_BOOKMARKS)
  Else
    Call AddToClipboard(sLink)
    Call UserInteraction.ShowMessage(IM_QUERY_LINK, sLink)
  End If
End Sub

Public Sub HT03_ExpressLinks()
  Dim iDoc As InfoDocument:  Set iDoc = GetActiveDocument
  If iDoc Is Nothing Then _
    Exit Sub
    
  Dim theDB As InfoWordDatabase: Set theDB = AccessWordDatabase
  If Not theDB.source_.Cache(iDoc.gid_).HasLinks Then
    Call UserInteraction.ShowMessage(EM_DOC_NO_LINKS)
    Exit Sub
  End If
  
  Call CSE_ProgressBar.Init("Обработка ссылок", sHeader:="", maxVal:=iDoc.doc_.Hyperlinks.Count, curVal:=1)
  Call CSE_ProgressBar.ShowModeless
  Call theDB.ExpressLinks(iDoc)
  Call Unload(CSE_ProgressBar)
  
  Call HT03_UpdateMarkup
End Sub

Public Sub HT03_ExpressDocuments()
  Dim iDoc As InfoDocument:  Set iDoc = GetActiveDocument
  If iDoc Is Nothing Then _
    Exit Sub
  Dim iSelected As Word.Range: Set iSelected = ValidateSelectionRange(iDoc.doc_)
  If iSelected Is Nothing Then
    Exit Sub
  ElseIf iSelected.Hyperlinks.Count = 0 Then
    Call UserInteraction.ShowMessage(EM_LINK_COUNT)
    Exit Sub
  End If

  Call DummyDocDlg.Show
  If DummyDocDlg.isCancelled_ Then _
    Exit Sub
  Dim newType As TLawType: newType = DummyDocDlg.GetType
  Call Unload(DummyDocDlg)
  
  Dim theDB As InfoWordDatabase: Set theDB = AccessWordDatabase
  
  Call CSE_ProgressBar.Init("Добавление справочных документов", maxVal:=iSelected.Hyperlinks.Count, curVal:=0)
  Call CSE_ProgressBar.ShowModeless
  Dim docs As Scripting.Dictionary: Set docs = theDB.ExpressDocuments(newType, iSelected)
  Call Unload(CSE_ProgressBar)
  
  If docs.Count <> 0 Then
    On Error Resume Next
    Call iDoc.UpdateFormatFilter(docs, theDB.source_)
    Call ProcessErrorMessages(UniqueList(ERR_SYNC_LINK_ID))
  End If
  Call UserInteraction.ShowMessage(IM_DOC_EXPRESS_ADD, docs.Count)
End Sub

Public Sub HT03_RejectDocument()
  Dim iDoc As InfoDocument:  Set iDoc = GetActiveDocument
  If iDoc Is Nothing Then _
    Exit Sub
  Dim iSelected As Word.Range: Set iSelected = ValidateSelectionRange(iDoc.doc_)
  If iSelected Is Nothing Then _
    Exit Sub
  
  Dim nCount&: nCount = IgnoreLawsIn(iSelected, iDoc, AccessDatabase)
  If nCount = 0 Then
    Call UserInteraction.ShowMessage(EM_LINK_COUNT)
  Else
    Call UserInteraction.ShowMessage(IM_DOC_IGNORED, nCount)
  End If
End Sub

Public Sub HT03_AddLink()
  Dim iDoc As InfoDocument: Set iDoc = GetActiveDocument
  If iDoc Is Nothing Then _
    Exit Sub
  Dim iSelected As Word.Range: Set iSelected = EnsureNoLinksSelected(iDoc.doc_)
  If iSelected Is Nothing Then _
    Exit Sub
  
  Dim theDB As InfoWordDatabase: Set theDB = AccessWordDatabase
  If Not theDB.source_.HasLinksFor(iDoc.gid_) Then
    Call UserInteraction.ShowMessage(EM_HAS_NO_LINKS)
    Exit Sub
  End If
  
  Dim newLnk As ItemLink: Set newLnk = PromptNewLink(iSelected, iDoc, theDB.source_)
  If newLnk Is Nothing Then _
    Exit Sub
  
  On Error Resume Next
  Call AddLinkTo(iSelected, newLnk, iDoc, theDB)
  Call ProcessErrorMessages(UniqueList(ERR_ADD_LINK_DOC_FAILED, ERR_ADD_LINK_DB_FAILED, ERR_LINK_SELF))
End Sub

Public Sub HT03_AddNew()
  Dim iDoc As InfoDocument: Set iDoc = GetActiveDocument
  If iDoc Is Nothing Then _
    Exit Sub
  Dim iSelected As Word.Range: Set iSelected = EnsureNoLinksSelected(iDoc.doc_)
  If iSelected Is Nothing Then _
    Exit Sub
  
  Dim theDB As InfoWordDatabase: Set theDB = AccessWordDatabase
  If Not theDB.source_.HasLinksFor(iDoc.gid_) Then
    Call UserInteraction.ShowMessage(EM_HAS_NO_LINKS)
    Exit Sub
  End If
  
  Dim newLnk As ItemLink: Set newLnk = PromptLinkToNewDoc(iSelected, iDoc, theDB.source_)
  If newLnk Is Nothing Then _
    Exit Sub
    
  On Error Resume Next
  Call AddLinkTo(iSelected, newLnk, iDoc, theDB)
  Call ProcessErrorMessages(UniqueList(ERR_ADD_LINK_DOC_FAILED, ERR_ADD_LINK_DB_FAILED, ERR_LINK_SELF))
End Sub

Public Sub HT03_RemoveLink()
  Dim iDoc As InfoDocument: Set iDoc = GetActiveDocument
  If iDoc Is Nothing Then _
    Exit Sub
    
  Dim iSelected As Word.Range: Set iSelected = ValidateSelectionRange(iDoc.doc_)
  If iSelected Is Nothing Then _
    Exit Sub
  If iSelected.Hyperlinks.Count = 0 Then
    Call UserInteraction.ShowMessage(EM_LINK_COUNT)
    Exit Sub
  ElseIf iSelected.Hyperlinks(1).Range.Start = 0 Then
    Call UserInteraction.ShowMessage(EM_REMOVE_FIRST_LINK)
    Exit Sub
  End If
  
  On Error Resume Next
  Dim nCount&: nCount = RemoveLinksIn(iSelected, iDoc, AccessDatabase)
  Call ProcessErrorMessages(UniqueList(ERR_SYNC_LINK_ID, ERR_SYNC_DB))
  
  Call UserInteraction.ShowMessage(IM_REMOVE_LINK_OK, nCount)
End Sub

Public Sub HT03_FollowLink()
  Dim selectedLinks As Word.Hyperlinks: Set selectedLinks = ActiveDocument.Application.Selection.Hyperlinks
  If selectedLinks.Count <> 1 Then
    Call UserInteraction.ShowMessage(EM_LINK_SINGLE)
    Exit Sub
  End If
  
  Dim hLink As Word.Hyperlink: Set hLink = selectedLinks(1)
  If Not IsHyperlinkValid(hLink) Then
    Call UserInteraction.ShowMessage(EM_LINK_INVALID)
    Exit Sub
  End If
  
  On Error Resume Next
  Call AccessWordDatabase.OpenHyperlink(hLink)
  Call ProcessErrorMessages(UniqueList(ERR_LINK_NO_FILE, ERR_BOOKMARK_INVALID))
End Sub

Public Sub HT03_Registry()
  Dim dbDoc As Excel.Workbook: Set dbDoc = GetDatabaseDocument(Word.Application.ActiveDocument)
  If dbDoc Is Nothing Then _
    Exit Sub
  
  Call dbDoc.Activate
  dbDoc.Application.WindowState = xlMaximized
  Call AppActivate(dbDoc.Application.Caption)
End Sub

Public Sub HT03_ReloadIndex()
  Call AccessDatabase.Recreate
  Call MsgBox("Индекс загружен", vbInformation)
End Sub

Public Sub HT03_Help()
  Dim wordWrap As New API_WordWrapper
  Dim helpDoc As Word.Document: Set helpDoc = wordWrap.OpenDocument(HELP_FILE_PATH, bReadOnly:=True)
  If helpDoc Is Nothing Then
    Call UserInteraction.ShowMessage(EM_HELP_FILE_INVALID, HELP_FILE_PATH)
    Exit Sub
  End If
  
  With helpDoc
    .ActiveWindow.View.ReadingLayout = False
    Call .ActiveWindow.ScrollIntoView(helpDoc.Bookmarks(HELP_WORD_BOOKMARK).Range)
    Call .Application.Activate
  End With
End Sub

Public Sub HT03_ReloadLinks()
  Dim iDoc As InfoDocument:  Set iDoc = GetActiveDocument
  If iDoc Is Nothing Then _
    Exit Sub
  Dim theDB As InfoWordDatabase: Set theDB = AccessWordDatabase
  If Not theDB.source_.Loaded Then _
    Exit Sub
    
  If Not UserInteraction.AskQuestion(QM_RELOAD_CONFIRM) Then _
    Exit Sub
    
  Call ReloadLinks(iDoc, theDB)
  Call HT03_UpdateMarkup
End Sub

' ===========================
Private Function GetActiveDocument() As InfoDocument
  Dim iDoc As New InfoDocument: Set iDoc = ParseDocument(Word.Application.ActiveDocument)
  If iDoc Is Nothing Then
    Call UserInteraction.ShowMessage(EM_DOC_NOT_ADDED)
    Exit Function
  End If
  
  Dim theDB As InfoDatabase: Set theDB = AccessDatabase
  If Not theDB.Loaded Then _
    Exit Function
  If Not theDB.Contains(iDoc.gid_) Then
    Call UserInteraction.ShowMessage(EM_DOC_NOT_ADDED)
    Exit Function
  End If
  
  If theDB.Data(iDoc.gid_).FileName = HT03_FILE_NOT_LOADED Then
    Call UserInteraction.ShowMessage(EM_DOC_NOT_ADDED)
    Exit Function
  End If
  
  Set GetActiveDocument = iDoc
End Function

Private Function AddLawInternal(ByRef iDoc As InfoDocument, theDB As InfoWordDatabase)
  Dim iLaw As ItemLaw: Set iLaw = PrepareNewLaw(iDoc, theDB)
  If iLaw Is Nothing Then _
    Exit Function
    
  If Not theDB.source_.FindByFileName(iDoc.doc_.Name) Is Nothing Then
    Call UserInteraction.ShowMessage(EM_FILENAME_TAKEN)
    Exit Function
  End If
  
  Dim theProps As ItemDocumentProperties: Set theProps = PromptLawProperties(iLaw, iDoc.linkSystem_, bEdit:=False)
  If theProps Is Nothing Then _
    Exit Function
    
  Call AddNewLaw(iDoc, theDB, theProps)
End Function
