# ============================================================================
# mupdf-embedpage.test --
#
#	Tests about embedding pages.
#

package require TTXN
  # package under test is tclMuPDF 2.1
package require tclMuPDF 2.1

set thisDir [file dirname [file normalize [info script]]]
set samplesDir [file join $thisDir samples]
set TmpResultDir [file join $thisDir tmpResults]
file mkdir $TmpResultDir
file delete {*}[glob -nocomplain $TmpResultDir/*]

# =============================================================================


# ====================================================
# All the following tests are based on a common setup:
# ====================================================
    set pdf [mupdf::open [file join $samplesDir Book.pdf]]


Name: graft.neg.1
Description: {graft a wrong(invalid) page}
Test: {
    $pdf graft fakePage
}
Expected: error glob {"*" must be a mupdf::Page}


Name: embed.neg.1
Description: {embed missing params}
Test: {
    $pdf embed
}
Expected: error glob {wrong # args: should be "* embed graftKey pageNum ?arg ...?"}

Name: embed.neg.2
Description: {embed wrong graftKey}
Test: {
    $pdf embed fakeKey 0
}
Expected: error glob {invalid graft-key "fakeKey"}

Name: embed.neg.3
Description: {embed invalid pagenumber}
Setup: {
    set stampPdf [mupdf::open [file join $samplesDir Review-Stamps.pdf]]
}
Test: {
    set graftID1 [$pdf graft [$stampPdf getpage 12]]
    $pdf embed $graftID1 999
}
Cleanup: { 
    $stampPdf quit 
    $pdf quit
    set pdf [mupdf::open [file join $samplesDir Book.pdf]]
}
Expected: error {Page not found: 1000}

Name: embed.neg.4
Description: {embed wrong options}
Setup: {
    set stampPdf [mupdf::open [file join $samplesDir Review-Stamps.pdf]]
}
Test: {
    set graftID1 [$pdf graft [$stampPdf getpage 12]]
    $pdf embed $graftID1 0 -scale 0.5 -wrongopt 123
}
Cleanup: { 
    $stampPdf quit 
    $pdf quit
    set pdf [mupdf::open [file join $samplesDir Book.pdf]]
}
Expected: error {unrecognized option "-wrongopt". Valid options are: -crop,-to,-scale,-rotate,-over}

Name: embed.neg.5
Description: {embed wrong options - malformed -crop}
Setup: {
    set stampPdf [mupdf::open [file join $samplesDir Review-Stamps.pdf]]
}
Test: {
    set graftID1 [$pdf graft [$stampPdf getpage 12]]
    $pdf embed $graftID1 0 -scale 0.5 -crop {100 100 xxx 1000}
}
Cleanup: { 
    $stampPdf quit 
    $pdf quit
    set pdf [mupdf::open [file join $samplesDir Book.pdf]]
}
Expected: error {malformed sequence of floating point numbers: expected floating-point number but got "xxx"}


Name: graft
Description: {graft (and discard)}
Test: {
    set stampPdf [mupdf::open [file join $samplesDir Review-Stamps.pdf]]
    set graftID1 [$pdf graft [$stampPdf getpage 23]] ;# it's a stamp 94x37
    set graftID2 [$pdf graft [$stampPdf getpage 20]] ;# it's a stamp 94x37
    $stampPdf quit

    return [llength [$pdf grafts]]
}
Cleanup: {
    $pdf quit
    set pdf [mupdf::open [file join $samplesDir Book.pdf]]
}
Expected: 2


Name: graft-and-embed
Description: {graft and embed .. check all options}
Test: {
    set stampPdf [mupdf::open [file join $samplesDir Review-Stamps.pdf]]
    set changes0 [$pdf haschanges]
    set graftID1 [$pdf graft [$stampPdf getpage 23]] ;# it's a stamp 94x37
    set changes1 [$pdf haschanges]
    set graftID2 [$pdf graft [$stampPdf getpage 20]] ;# it's a stamp 94x37
    $stampPdf quit
    
     # embed on the first (0-th) page.
    set pageNumber 0
    $pdf embed $graftID1 $pageNumber
    set changes2 [$pdf haschanges]	 
    $pdf embed $graftID2 $pageNumber  -scale 2.0 -to {100 300} -over true
    
     # embed under the text
    $pdf embed $graftID2 $pageNumber  -scale 2.0 -to {100 400} -over false

     # stamp just a portion 
    $pdf embed $graftID2 $pageNumber  -scale 2.0 -to {100 500} -crop {50 0 94 36}
    
     # rotated stamp on last page
    set pageNumber [expr {[$pdf npages]-1}]
    # note that the pivot for the rotation is the top-left corner
    $pdf embed $graftID2 $pageNumber  -scale 2.0 -to {100 500} -rotate -30
     
    $pdf export  [file join $TmpResultDir Stamped-Book.pdf]
    return [list $changes0 $changes1 $changes2]  ;;#  you should visually check
}
Cleanup: {
    $pdf quit
    set pdf [mupdf::open [file join $samplesDir Book.pdf]]
}
Expected: {0 1 1}


 # =================================================================
 # WARNING: You also need a visual testing !!!
 # =================================================================

#
# -- Common cleanup
#
    $pdf quit


# === cleanup ========================================
::tcltest::cleanupTests
