### ----------------------------------------------------
### -- Collection
### -- Licenses: Artistic-2.0
### -- Authors: Richard Hainsworth, aka finanalyst
### -- File: xt/45-asset-movement.rakutest
### ----------------------------------------------------
use Test;
use File::Directory::Tree;

use Collection;
constant TMP = 'xt/test-dir';
constant MODE = 'test-mode';
constant PLUGS = "{MODE}/plugins";
constant CONFS = "{MODE}/configs";
constant TEMPLATES = "{MODE}/templates";
constant SRC = "{MODE}/m-sources";
constant OUTPUT = "{MODE}/html";
constant ASSETS = "assets";

plan 10;
my $rv;

bail-out "{TMP} and {TMP}/{ OUTPUT } must exist. Have completion tests been run?"
unless TMP.IO.d and "{TMP}/{ OUTPUT }".IO.d;

mktree ASSETS ~ '/videos';
mktree ASSETS ~ '/images';

chdir TMP;

(MODE ~ '/configs/04-asset-output-options.raku').IO.spurt(qq:to/CONF/);
    %(
        asset-out-path => "assets", # relative to destination directory
    );
    CONF
"{ASSETS}/videos/fake-video.mp4".IO.spurt(:bin, "This is not a video");
"{ASSETS}/images/fake-image.png".IO.spurt(:bin, "This is not an image");
(PLUGS ~ '/rend2/config.raku' ).IO.spurt(q:to/CONF/);
    %(
        :render<image-manager.raku>,
        :template-raku<rend2_templates.raku>,
        :custom-raku<blocks.raku>,
        :css<mystyling.css>,
    )
    CONF
(PLUGS ~ '/rend2/image-manager.raku' ).IO.spurt(q:to/CONF/);
    sub ( $pp, %options -->Array ) {
        my $config = $pp.get-data('image-manager');
        $pp.add-data('image', $config);
        $pp.add-data('video-clip', $config);
        [] # return empty list
    }
    CONF
(PLUGS ~ '/rend2/rend2_templates.raku' ).IO.spurt(q:to/CONF/);
    %(
        image => sub (%prm, %tml) {
            my $config = %prm<image>;
            exit note ('Unexpected. Got ' ~ %prm<src>)
                unless $config<manager>.asset-is-used(%prm<src>,'image');
            '<img src="' ~ $config<dest-dir> ~ '/' ~ %prm<contents> ~ '">' ~ %prm<contents> ~ '</img>'
        },
        video-clip => sub (%prm, %tml) {
            my $config = %prm<video-clip>;
            exit note ('Unexpected. Got ' ~ %prm<src>)
                unless $config<manager>.asset-is-used(%prm<src>,'video-clip');
            '<img src="' ~ $config<dest-dir> ~ '/' ~ %prm<contents> ~ '">' ~ %prm<contents> ~ '</img>'
        },
    )
    CONF
(PLUGS ~ '/rend2/blocks.raku' ).IO.spurt(q:to/CONF/);
    <Image Video-clip>
    CONF
mktree "{PLUGS}/asset-mover/extra/";
"{PLUGS}/asset-mover/extra/css-fake-image.png".IO.spurt(:bin, "This is not an image for css");
(PLUGS ~ '/asset-mover/config.raku').IO.spurt(q:to/CONF/);
    %(
        :render<image-mover.raku>,
        :custom-raku(),
        :template-raku(),
    )
    CONF
(PLUGS ~ '/asset-mover/image-mover.raku' ).IO.spurt(q:to/CONF/);
        sub ($processedpod, %options ) {
        [ ('assets/images/css-fake-image.png', 'myself', 'extra/css-fake-image.png'), ]
    }
    CONF

"sources/image-test.rakudoc".IO.spurt(q:to/PODFILE/);
    =begin pod :no-toc :no-glossary :no-meta :no-footnotes

    =for Image :src<images/fake-image.png>
    Alt text

    =for Video-clip :src<videos/fake-video.mp4>
    Alt text

    =end pod
    PODFILE

#write a structure file with Latex-Static blocks
(SRC ~ '/some-images.rakudoc').IO.spurt( q:to/CONF/);
    =begin pod

    =Make-image
    First image

    =Make-image
    Second image

    =end pod
    CONF

#rewrite the config for image-mover plugin, that has a transfer milestone plugin
(PLUGS ~ '/image-mover/config.raku').IO.spurt(q:to/CONF/);
    %(
        :render,
        :custom-raku<custom-blocks.raku>,
        :template-raku<templates.raku>,
        :transfer<move-images.raku>
    )
    CONF
(PLUGS ~ '/image-mover/move-images.raku').IO.spurt(q:to/CONF/);
    sub ($pr, %processed, %options --> Array) {
        # find correctly named files in the directory
        my @files = dir(test => / ^ 'image-mover' .+ $ /)>>.Str;
        @files.map( { [ "assets/images/$_", 'myself', $_ ]  } ).Array;
    }
    CONF
(PLUGS ~ '/image-mover/custom-blocks.raku').IO.spurt(q:to/CONF/);
    <Make-image >
    CONF
(PLUGS ~ '/image-mover/templates.raku').IO.spurt(q:to/CONF/);
    %(
        make-image => sub (%prm, %tml) {
            state $count = 0;
            (PLUGS ~ '/image-mover/image-mover-' ~ ++$count ).IO.spurt( "This is not an image file, No. $count");
            "<img src=\"assets/images/image-mover-$count\" />";
        },
    )
    CONF
ok (PLUGS ~ '/gather-css/local.css').IO ~~ :e & :f, 'the css file generated by gather-css still exists';
$rv = collect( :no-status, :no-preserve-state, :after<transfer>, :full-render );
ok "{OUTPUT}/assets/videos/fake-video.mp4".IO ~~ :e & :f, 'video asset in correct place';
ok "{OUTPUT}/assets/images/fake-image.png".IO ~~ :e & :f, 'image asset in correct place';
ok "{OUTPUT}/assets/images/css-fake-image.png".IO ~~ :e & :f, 'css url image asset in correct place';
like "{OUTPUT}/assets/videos/fake-video.mp4".IO.slurp(:bin).decode,
        /
        'This is not a video'
        /, 'file contents as expected';
ok "{OUTPUT}/assets/images/image-mover-1".IO ~~ :e & :f, 'first image in correct place';
ok "{OUTPUT}/assets/images/image-mover-2".IO ~~ :e & :f, '2nd image in correct place';

ok (OUTPUT ~ '/some-images.html').IO ~~ :e & :f, 'structure file exists';
like (OUTPUT ~ '/some-images.html').IO.slurp,
    /
    '<img src="assets/images/image-mover-1" />'
    .*
    '<img src="assets/images/image-mover-2" />'
    /, 'file contents as expected';

nok (PLUGS ~ '/gather-css/local.css').IO ~~ :e & :f, 'the css file generated by gather-css no longer exists';

done-testing;


