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

' TODO:
' Public Function ProcessLawText(target As InfoDocument, theDB As InfoWordDatabase, props As ItemDocumentProperties)
' Public Function UpdateLinksIn(iDoc As InfoDocument, theDB As InfoDatabase)
' Public Function ToggleLinksStatusIn(target As Word.Range, iDoc As InfoDocument, theDB As InfoWordDatabase)
' Public Function IgnoreLawsIn(target As Word.Range, iDoc As InfoDocument, theDB As InfoDatabase) As Long
' Public Function RemoveLinksIn(iDoc As InfoDocument, target As Word.Range)
  ' Range with only invalid links
' Public Function AddLinkTo(target As Word.Range, iLink As ItemLink, iDoc As InfoDocument, theDB As InfoWordDatabase)
' Public Function AddNewLaw(iDoc As InfoDocument, theDB As InfoWordDatabase, theProps As ItemDocumentProperties)
  ' Check if folder doesnt exist

Private fso_ As Scripting.FileSystemObject

Private db_ As InfoWordDatabase
Private dbApp_ As API_XLWrapper

Private wordApp_ As API_WordWrapper
Private iDoc_ As InfoDocument

Public Function Setup()
  ' Mandatory setup function
  Set fso_ = New Scripting.FileSystemObject
  On Error Resume Next
  Call fso_.DeleteFolder(Dev_GetTestFolder)
  On Error GoTo 0
  Call EnsureFolderExists(Dev_GetTestFolder & "\" & HT03_DEFAULT_WORKDIR)
  
  Set db_ = New InfoWordDatabase
  Set dbApp_ = Dev_GetDatabase
  Dim oDatabase As New InfoDatabase: Call oDatabase.Init(dbApp_.Document, Dev_GetTestFolder & "\" & HT03_DEFAULT_WORKDIR)
  Call db_.Init(oDatabase)
  
  Set iDoc_ = New InfoDocument
  Set wordApp_ = New API_WordWrapper
  Call wordApp_.SetApplication(ThisDocument.Application)
End Function

Public Function Teardown()
  ' Mandatory teardown function
  Call wordApp_.ReleaseDocument
  Call dbApp_.ReleaseDocument
  Call fso_.DeleteFolder(Dev_GetTestFolder)
End Function

Public Function t_PrepareNewLaw()
  On Error GoTo PROPAGATE_ERROR
  
  Call Dev_NewCase("New law")
  Dim doc As Word.Document: Set doc = wordApp_.NewDocument
  doc.Range.Text = "Title"
  Set iDoc_.doc_ = doc
  iDoc_.gid_ = 1337
  Dim iLaw As ItemLaw: Set iLaw = PrepareNewLaw(iDoc_, db_)
  Call Dev_AssertNotNothing(iLaw)
  Call Dev_ExpectEQ("Title", iLaw.title_)
  Call Dev_ExpectEQ(iDoc_.gid_, iLaw.gid_)
  Call Dev_ExpectEQ(T_LAWS_FULL, iLaw.status_)
  Call Dev_ExpectFalse(iLaw.isDummy_)
  Call Dev_ExpectEQ("0001 Title.docx", iLaw.fileName_)
  
  Call Dev_NewCase("Replace not loaded file")
  Call iLaw.InitDummy(iLaw.gid_)
  Call db_.source_.SetItem(iLaw.Clone)
  Set iLaw = PrepareNewLaw(iDoc_, db_)
  Call Dev_ExpectEQ("Title", iLaw.title_)
  Call Dev_ExpectEQ(iDoc_.gid_, iLaw.gid_)
  Call Dev_ExpectEQ(T_LAWS_FULL, iLaw.status_)
  Call Dev_ExpectFalse(iLaw.isDummy_)
  Call Dev_ExpectEQ("0001 Title.docx", iLaw.fileName_)
  
  Call Dev_NewCase("Already added")
  Call db_.source_.SetItem(iLaw.Clone)
  Call wordApp_.SaveAs(db_.source_.docsLocation_ & "\" & iLaw.fileName_)
  Call Dev_MockUI.ExpectMessage(EM_FILE_ALREADY_ADDED)
  Call Dev_ExpectNothing(PrepareNewLaw(iDoc_, db_))
  Call Dev_MockUI.ResetExpectations
  
  Call Dev_NewCase("Has existing dummy file")
  Call wordApp_.SaveAs(Dev_GetTestFolder & "\" & doc.Name)
  db_.source_.Data(iDoc_.gid_).IsDummy = True
  db_.source_.Data(iDoc_.gid_).Status = T_LAWS_FRINGE
  db_.source_.Data(iDoc_.gid_).DType = T_LAW_02_INTERNATIONAL
  Call Dev_MockUI.ExpectQuestion(QM_DELETE_BEFORE_ADD_DUMMY, bAnswerYes:=True)
  Set iLaw = PrepareNewLaw(iDoc_, db_)
  Call Dev_AssertNotNothing(iLaw)
  Call Dev_MockUI.ResetExpectations
  Call Dev_ExpectEQ("Title", iLaw.title_)
  Call Dev_ExpectEQ(iDoc_.gid_, iLaw.gid_)
  Call Dev_ExpectFalse(iLaw.isDummy_)
  Call Dev_ExpectEQ("0001 Title.docx", iLaw.fileName_)
  Call Dev_ExpectEQ(T_LAWS_FRINGE, iLaw.status_)
  Call Dev_ExpectEQ(T_LAW_02_INTERNATIONAL, iLaw.type_)
  Call Dev_ExpectFalse(fso_.FileExists(db_.source_.docsLocation_ & "\" & iLaw.fileName_), "Delete previous file")
  Call Dev_ExpectFalse(db_.source_.Contains(iDoc_.gid_), "Delete previous data entry")
  
  Call Dev_NewCase("Has existing non-dummy file")
  Call db_.source_.SetItem(iLaw.Clone)
  db_.source_.Data(iDoc_.gid_).Status = T_LAWS_FRINGE
  db_.source_.Data(iDoc_.gid_).DType = T_LAW_02_INTERNATIONAL
  Call wordApp_.SaveAs(db_.source_.docsLocation_ & "\" & iLaw.fileName_)
  Call wordApp_.SaveAs(Dev_GetTestFolder & "\" & doc.Name)
  Call Dev_MockUI.ExpectQuestion(QM_DELETE_BEFORE_ADD, bAnswerYes:=True)
  Set iLaw = PrepareNewLaw(iDoc_, db_)
  Call Dev_AssertNotNothing(iLaw)
  Call Dev_MockUI.ResetExpectations
  Call Dev_ExpectEQ("Title", iLaw.title_)
  Call Dev_ExpectEQ(iDoc_.gid_, iLaw.gid_)
  Call Dev_ExpectFalse(iLaw.isDummy_)
  Call Dev_ExpectEQ("0001 Title.docx", iLaw.fileName_)
  Call Dev_ExpectEQ(T_LAWS_FRINGE, iLaw.status_)
  Call Dev_ExpectEQ(T_LAW_02_INTERNATIONAL, iLaw.type_)
  Call Dev_ExpectFalse(fso_.FileExists(db_.source_.docsLocation_ & "\" & iLaw.fileName_), "Delete previous file")
  Call Dev_ExpectFalse(db_.source_.Contains(iDoc_.gid_), "Delete previous data entry")
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function
