Attribute VB_Name = "DevHelper"
Option Explicit

Public Const TEST_FOLDER_ARTIFACTS = "test"
Public Const TEST_FOLDER_DB = "testAddin"

Public Const TEST_FILE_MINIMAL = "MinimalDoc.docx"
Public Const TEST_FILE_001 = "001.docx"
Public Const TEST_FILE_002 = "002.docx"

Public Function Dev_PrepareSkeleton()
  ' Do nothing
End Function

Public Sub Dev_ManualRunTest()
  Dim sSuite$: sSuite = "s_InfoDocument"
  Dim sTest$: sTest = "t_UpdateLinkTip"
  Dim sMsg$: sMsg = Dev_RunTestDebug(sSuite, sTest)
  Debug.Print sMsg
  Call MsgBox(sMsg)
End Sub

Public Function Dev_GetTestSuite(sName$) As Object
  Select Case sName
    Case "s_DocumentProcessor": Set Dev_GetTestSuite = New s_DocumentProcessor
    Case "s_ItemHyperlink": Set Dev_GetTestSuite = New s_ItemHyperlink
    Case "s_InfoDocument": Set Dev_GetTestSuite = New s_InfoDocument
    Case "s_InfoWordDatabase": Set Dev_GetTestSuite = New s_InfoWordDatabase
    Case "s_AddinAPI": Set Dev_GetTestSuite = New s_AddinAPI
    Case "s_WordDataAccess": Set Dev_GetTestSuite = New s_WordDataAccess
  End Select
End Function

Public Function Dev_GetTestFolder() As String
  Static sFolder$
  If sFolder = vbNullString Then
    Dim fso As New Scripting.FileSystemObject
    sFolder = fso.GetParentFolderName(ThisDocument.Path)
    sFolder = sFolder & "\" & TEST_FOLDER_DB
  End If
  Dev_GetTestFolder = sFolder
End Function

Public Function Dev_GetArtifactFolder() As String
  Static sFolder$
  If sFolder = vbNullString Then
    Dim fso As New Scripting.FileSystemObject
    sFolder = fso.GetParentFolderName(ThisDocument.Path)
    sFolder = sFolder & "\" & TEST_FOLDER_ARTIFACTS
  End If
  Dev_GetArtifactFolder = sFolder
End Function

Public Function Dev_GetDatabaseTemplate() As String
  Static sPath$
  If sPath = vbNullString Then
    Dim fso As New Scripting.FileSystemObject
    sPath = fso.GetParentFolderName(ThisDocument.Path)
    sPath = sPath & "\" & HT03_DBNAME
  End If
  Dev_GetDatabaseTemplate = sPath$
End Function

Public Function Dev_SetupDatabaseFile() As String
  Dim sTemplate$: sTemplate = Dev_GetDatabaseTemplate
  Dim sTarget$: sTarget = Dev_GetTestFolder & "\" & HT03_DBNAME
  
  Call EnsureFolderExists(Dev_GetTestFolder & "\" & HT03_DEFAULT_WORKDIR)
  Dim fso As New Scripting.FileSystemObject
  Call fso.CopyFile(sTemplate, sTarget)
  
  Dev_SetupDatabaseFile = sTarget
End Function

Public Function Dev_GetDatabase() As API_XLWrapper
  Set Dev_GetDatabase = New API_XLWrapper
  Call Dev_GetDatabase.CreateApplication
  Call Dev_GetDatabase.OpenDocument(Dev_SetupDatabaseFile)
End Function

Public Function Dev_SetupThreeLinks(iDoc As InfoDocument)
  iDoc.linkSystem_ = PT_LOCAL
  Call iDoc.UpdateStyles(ThisDocument.FullName)
  
  Dim doc As Word.Document: Set doc = iDoc.doc_
  Call doc.Range.InsertAfter("fillerStart" & vbNewLine & _
                              "11 test1 11" & vbNewLine & _
                              "22 test2 22" & vbNewLine & _
                              "33 test3 33" & vbNewLine & _
                              "fillerEnd" & vbNewLine)
  
  Dim iLink As New ItemLink
  Call iLink.Init(0, "", 1337, "sub_1", T_LINK_ASPECT)
  Call Dev_AssertTrue(iDoc.AddHyperlink(doc.Paragraphs(2).Range.Words(2), iLink, Style10))
  Call iLink.Init(0, "", 1338, "sub_2", T_LINK_DEFAULT)
  Call Dev_AssertTrue(iDoc.AddHyperlink(doc.Paragraphs(3).Range.Words(2), iLink, Style10))
  Call iLink.Init(0, "", 1339, "sub_3", T_LINK_DEFINITION)
  Call Dev_AssertTrue(iDoc.AddHyperlink(doc.Paragraphs(4).Range.Words(2), iLink, Style10))
End Function

Public Function GetDummyData() As ItemLaw
  Set GetDummyData = New ItemLaw
  With GetDummyData
    .gid_ = GenerateGID
    .status_ = T_LAWS_FULL
    .type_ = T_LAW_05_CODEX
    .fileName_ = "fileName" & CStr(.gid_) & ".docx"
    .title_ = "title_"
    .typeTxt_ = "typeTxt_"
    .creator_ = "creator_"
    .date_ = "date_"
    .number_ = "number_"
    .header_ = "header_"
    .attach_ = "attach_"
    .isScoped_ = True
    .isDummy_ = False
  End With
End Function

Public Function GenerateLinks(Optional nCount& = 0) As String()
  Dim lnkCount&: lnkCount = nCount
  If lnkCount = 0 Then _
    lnkCount = Int((19 * Rnd) + 1)
  Dim linksData() As String
  ReDim linksData(1 To lnkCount, LinkTransferStruct.[_First] To LinkTransferStruct.[_Last])
  
  Dim nItem&
  For nItem = 1 To lnkCount Step 1
    linksData(nItem, S_TL_DESTINATION_ID) = CStr(GenerateGID)
    linksData(nItem, S_TL_DESTINATION_SUB) = GenerateSub
    linksData(nItem, S_TL_SOURCE_SUB) = GenerateSub
  Next nItem
  
  GenerateLinks = linksData
End Function

Public Function GenerateGID() As Long
  GenerateGID = Int(9999 * Rnd) + 1000000
End Function

Public Function GenerateSub() As String
  GenerateSub = "sub_" & Int(Rnd * 100)
End Function
