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

' TODO:
' Public Function PrepareDocumentText(doc As Word.Document, iMods As ItemDocumentModification)

Private wordApp_ As API_WordWrapper
Private doc_ As Word.Document

Public Function Setup()
  ' Mandatory setup function
  Set wordApp_ = New API_WordWrapper
  Call wordApp_.SetApplication(ThisDocument.Application)
  Set doc_ = wordApp_.NewDocument
End Function

Public Function Teardown()
  ' Mandatory teardown function
  Call wordApp_.ReleaseDocument
End Function

Public Function t_ParseHyperlink()
  On Error GoTo PROPAGATE_ERROR
  
  Dim tRange As Word.Range: Set tRange = WordAddLine("test", doc_.Range)
  Dim hLink As Word.Hyperlink
  
  Call Dev_NewCase("Invalid address")
  Set hLink = tRange.Hyperlinks.Add(tRange, "invalid")
  
  Call Dev_ExpectNothing(ParseHyperlink(hLink))
  Call Dev_ExpectFalse(IsHyperlinkValid(hLink))

  Call Dev_NewCase("Valid address")
  Dim sAddress$: sAddress = "http://mobileonline.garant.ru/document/redirect/1337/2"
  hLink.Address = "http://mobileonline.garant.ru/document/redirect/1337/2"
  Call Dev_ExpectTrue(IsHyperlinkValid(hLink))
  Dim iLink As ItemHyperlink: Set iLink = ParseHyperlink(hLink)
  Call Dev_ExpectEQ(PT_MOBILE, iLink.LinkSystem)
  Call Dev_ExpectEQ(1337, iLink.DocumentID)
  Call Dev_ExpectEQ("sub_2", iLink.BookmarkName)

  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_GetFirstValidHyperlinkIn()
  On Error GoTo PROPAGATE_ERROR
  
  Dim rTargetArea As Word.Range
  
  Call WordAddLine("test1" & vbNewLine & "test2" & vbNewLine & "test3" & vbNewLine & "test4", doc_.Range)
  Call Dev_ExpectNothing(GetFirstValidHyperlinkIn(doc_.Range), "No hyperlinks")
  
  Dim sAddress2$: sAddress2 = "garantf1://1337.0/"
  Dim sAddress3$: sAddress3 = "garantf1://1338.12/"
  Dim hLink1 As Word.Hyperlink: Set hLink1 = doc_.Hyperlinks.Add(doc_.Paragraphs(1).Range, "invalid")
  Dim hLink2 As Word.Hyperlink: Set hLink2 = doc_.Hyperlinks.Add(doc_.Paragraphs(2).Range, sAddress2)
  Dim hLink3 As Word.Hyperlink: Set hLink2 = doc_.Hyperlinks.Add(doc_.Paragraphs(3).Range, sAddress3)
  
  Call Dev_ExpectNothing(GetFirstValidHyperlinkIn(hLink1.Range), "Invalid address")
  Call Dev_ExpectEQ(hLink2.Address, GetFirstValidHyperlinkIn(hLink2.Range).Address, "Full range of single link")
  Call Dev_ExpectEQ(hLink2.Address, GetFirstValidHyperlinkIn(doc_.Range).Address, "Skip invalid link")
  
  Call Dev_NewCase("Partial hit")
  Set rTargetArea = doc_.Range(hLink2.Range.Words.Last.Start, doc_.Range.End)
  Call Dev_ExpectEQ(hLink2.Address, GetFirstValidHyperlinkIn(rTargetArea).Address, "Full range of single link")
  
  Call Dev_NewCase("Do no wrap")
  Set rTargetArea = doc_.Range.Paragraphs(4).Range
  Call Dev_ExpectNothing(GetFirstValidHyperlinkIn(rTargetArea))

  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_ScanLawIDs()
  On Error GoTo PROPAGATE_ERROR
  
  Call WordAddLine("test1" & vbNewLine & "test2" & vbNewLine & "test3" & vbNewLine & "test4", doc_.Range)
  Call Dev_ExpectEQ(0, ScanLawIDs(doc_.Range).Count, "No hyperlinks")
  
  Call doc_.Hyperlinks.Add(doc_.Paragraphs(1).Range, "invalid123")
  Call doc_.Hyperlinks.Add(doc_.Paragraphs(2).Range, "garantf1://1337.0/")
  Call doc_.Hyperlinks.Add(doc_.Paragraphs(3).Range, "garantf1://1338.12/")
  Call doc_.Hyperlinks.Add(doc_.Paragraphs(4).Range, "garantf1://1337.12/")
  
  Dim ids As New Scripting.Dictionary
  Call ids.Add(1337, 0)
  Call ids.Add(1338, 0)
  
  Call Dev_ExpectEQ(ids, ScanLawIDs(doc_.Range), "Scan multiple ids")

  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_GetLinkStatusStyle()
  On Error GoTo PROPAGATE_ERROR
  
  ' ===== T_LAWS_NOTDEFINED
  Call Dev_ExpectEQ(Style0A, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_NOTDEFINED, T_LINK_DEFAULT))
  Call Dev_ExpectEQ(Style0A, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_NOTDEFINED, T_LINK_ASPECT))
  Call Dev_ExpectEQ(Style0A, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_NOTDEFINED, T_LINK_FOUNDATION))
  Call Dev_ExpectEQ(Style0A, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_NOTDEFINED, T_LINK_DEFINITION))
  
  Call Dev_ExpectEQ(Style4A, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_NOTDEFINED, T_LINK_DEFAULT))
  Call Dev_ExpectEQ(Style4A, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_NOTDEFINED, T_LINK_ASPECT))
  Call Dev_ExpectEQ(Style4A, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_NOTDEFINED, T_LINK_FOUNDATION))
  Call Dev_ExpectEQ(Style4A, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_NOTDEFINED, T_LINK_DEFINITION))
  
  ' ====== T_LAWS_FULL
  Call Dev_ExpectEQ(Style10, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_FULL, T_LINK_DEFAULT))
  Call Dev_ExpectEQ(Style11, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_FULL, T_LINK_ASPECT))
  Call Dev_ExpectEQ(Style12, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_FULL, T_LINK_FOUNDATION))
  Call Dev_ExpectEQ(Style13, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_FULL, T_LINK_DEFINITION))
  
  Call Dev_ExpectEQ(Style10, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_FULL, T_LINK_DEFAULT))
  Call Dev_ExpectEQ(Style11, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_FULL, T_LINK_ASPECT))
  Call Dev_ExpectEQ(Style12, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_FULL, T_LINK_FOUNDATION))
  Call Dev_ExpectEQ(Style13, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_FULL, T_LINK_DEFINITION))
  
  ' ====== T_LAWS_FRINGE
  Call Dev_ExpectEQ(Style20, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_FRINGE, T_LINK_DEFAULT))
  Call Dev_ExpectEQ(Style21, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_FRINGE, T_LINK_ASPECT))
  Call Dev_ExpectEQ(Style22, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_FRINGE, T_LINK_FOUNDATION))
  Call Dev_ExpectEQ(Style23, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_FRINGE, T_LINK_DEFINITION))
  
  Call Dev_ExpectEQ(Style20, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_FRINGE, T_LINK_DEFAULT))
  Call Dev_ExpectEQ(Style21, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_FRINGE, T_LINK_ASPECT))
  Call Dev_ExpectEQ(Style22, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_FRINGE, T_LINK_FOUNDATION))
  Call Dev_ExpectEQ(Style23, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_FRINGE, T_LINK_DEFINITION))
  
  ' ====== T_LAWS_OUTER
  Call Dev_ExpectEQ(Style30, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_OUTER, T_LINK_DEFAULT))
  Call Dev_ExpectEQ(Style31, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_OUTER, T_LINK_ASPECT))
  Call Dev_ExpectEQ(Style32, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_OUTER, T_LINK_FOUNDATION))
  Call Dev_ExpectEQ(Style33, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_OUTER, T_LINK_DEFINITION))
  
  Call Dev_ExpectEQ(Style30, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_OUTER, T_LINK_DEFAULT))
  Call Dev_ExpectEQ(Style31, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_OUTER, T_LINK_ASPECT))
  Call Dev_ExpectEQ(Style32, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_OUTER, T_LINK_FOUNDATION))
  Call Dev_ExpectEQ(Style33, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_OUTER, T_LINK_DEFINITION))
  
  ' ====== T_LAWS_IGNORE
  Call Dev_ExpectEQ(Style4A, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_IGNORE, T_LINK_DEFAULT))
  Call Dev_ExpectEQ(Style4A, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_IGNORE, T_LINK_ASPECT))
  Call Dev_ExpectEQ(Style4A, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_IGNORE, T_LINK_FOUNDATION))
  Call Dev_ExpectEQ(Style4A, GetLinkStatusStyle(T_LAWS_FULL, T_LAWS_IGNORE, T_LINK_DEFINITION))
  
  Call Dev_ExpectEQ(Style4A, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_IGNORE, T_LINK_DEFAULT))
  Call Dev_ExpectEQ(Style4A, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_IGNORE, T_LINK_ASPECT))
  Call Dev_ExpectEQ(Style4A, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_IGNORE, T_LINK_FOUNDATION))
  Call Dev_ExpectEQ(Style4A, GetLinkStatusStyle(T_LAWS_FRINGE, T_LAWS_IGNORE, T_LINK_DEFINITION))
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_CreateBookmarkFor()
  On Error GoTo PROPAGATE_ERROR
  
  Call WordAddLine("test1" & vbNewLine & "test2" & vbNewLine & "test3" & vbNewLine & "test4", doc_.Range)
  Dim hLink1 As Word.Hyperlink: Set hLink1 = doc_.Hyperlinks.Add(doc_.Paragraphs(1).Range, "invalid123")
  Dim hLink2 As Word.Hyperlink: Set hLink2 = doc_.Hyperlinks.Add(doc_.Paragraphs(2).Range, "garantf1://1337.0/")
  
  Call Dev_NewCase("Invalid link")
  Call Dev_ExpectEQ("lnk_1", CreateBookmarkFor(hLink1.Range))
  Call Dev_ExpectEQ(doc_.Bookmarks(1).Range.Text, hLink1.Range.Text)
  
  Call Dev_NewCase("Valid link")
  Call Dev_ExpectEQ("lnk_2", CreateBookmarkFor(hLink2.Range))
  Call Dev_ExpectEQ(doc_.Bookmarks(2).Range.Text, hLink2.Range.Text)
  
  Call Dev_NewCase("BM already exists")
  Call Dev_ExpectEQ("lnk_3", CreateBookmarkFor(hLink2.Range))
  Call Dev_ExpectEQ(doc_.Bookmarks(3).Range.Text, hLink2.Range.Text)

  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function
