#!/bin/bash

#
# This script runs visual regression tests using BackstopJS.
# Tests are run if composer.lock has changed
# but composer.json has not (e.g. dependency updates)
# OR if the last commit message contains [vr]
#

# Variables
BUILD_DIR=$(pwd)
GITHUB_API_URL="https://api.github.com/repos/$CI_PROJECT_USERNAME/$CI_PROJECT_REPONAME"

GIT_FILE_MODIFIED()
{
    # Stash list of changed files
    GIT_FILES_CHANGED="$(git diff origin/master --name-only)"

    while read -r changedFile; do
        if [[ "${changedFile}" == "$1" ]]
        then
            return 0;
        fi
    done <<< "$GIT_FILES_CHANGED"

    return 1;
}

LAST_GIT_COMMIT_MESSAGE=$(git log -1 --pretty=%B)

if [[ ${LAST_GIT_COMMIT_MESSAGE} == *"--skip-vr"* ]]
then
    echo -e "\nVisual regression tests skipped because the last commit contains --skip-vr"
    exit 0
fi

# Always run visual tests if "[vr]" is in the last commit message
if [[ ${LAST_GIT_COMMIT_MESSAGE} != *"[vr]"* ]]
then

    # Skip visual tests if there hasn't been a modification to composer.lock
    if ! GIT_FILE_MODIFIED 'composer.lock'
    then
        echo -e "\nSkipping visual regression tests since composer.lock has NOT changed"
        exit 0
    fi

    # Skip visual tests if has been a modification to composer.json
    if GIT_FILE_MODIFIED 'composer.json'
    then
        echo -e "\nSkipping visual regression tests since composer.json HAS changed"
        exit 0
    fi

else
    echo -e "\nRunning visual regression tests because the latest commit message demands it"
fi

# Ping the multidev environment to wake it from sleep
echo -e "\nPinging the ${TERMINUS_ENV} multidev environment to wake it from sleep..."
curl -I "$MULTIDEV_SITE_URL" >/dev/null

# Ping the live environment to wake it from sleep
echo -e "\nPinging the dev environment to wake it from sleep..."
curl -I "$DEV_SITE_URL" >/dev/null

# Backstop visual regression
echo -e "\nRunning backstop reference on ${DEV_SITE_URL}..."
backstop reference --config=.ci/test/visual-regression/backstopConfig.js

# Backstop test
echo -e "\nRunning backstop test on ${MULTIDEV_SITE_URL}..."
VISUAL_REGRESSION_RESULTS=$(backstop test --config=.ci/test/visual-regression/backstopConfig.js || echo 'true')

echo "${VISUAL_REGRESSION_RESULTS}"

# Create the artifacts directory if needed
if [ ! -d "$ARTIFACTS_FULL_DIR" ]; then
  mkdir -p $ARTIFACTS_FULL_DIR
fi


# Copy backstop_data files to ARTIFACTS_FULL_DIR
echo -e "\nCopying backstop_data files to $ARTIFACTS_FULL_DIR..."
rm -rf $ARTIFACTS_FULL_DIR/backstop_data
cp -r backstop_data $ARTIFACTS_FULL_DIR/

DIFF_IMAGE=$(find ./backstop_data -type f -name "*.png" | grep diff | grep desktop | head -n 1)

if [ ! -f $DIFF_IMAGE ]; then
	echo -e "\nDiff image file $DIFF_IMAGE not found!"
fi

DIFF_IMAGE_URL="$ARTIFACTS_DIR_URL/$DIFF_IMAGE"

DIFF_REPORT="$ARTIFACTS_FULL_DIR/backstop_data/html_report/index.html"

if [ ! -f $DIFF_REPORT ]; then
	echo -e "\nDiff report file $DIFF_REPORT not found!"
	exit 1
fi

DIFF_REPORT_URL="$ARTIFACTS_DIR_URL/backstop_data/html_report/index.html"

REPORT_LINK="[![Visual report]($DIFF_IMAGE_URL)]($DIFF_REPORT_URL)"

if [[ ${VISUAL_REGRESSION_RESULTS} == *"Mismatch errors found"* ]]
then
	# visual regression failed
	echo -e "\nVisual regression test failed!"
	PR_MESSAGE="Visual regression test failed! $REPORT_LINK"
else
	# visual regression passed
	REPORT_LINK="\n\nView the [visual regression test report]($DIFF_REPORT_URL)"
	echo -e "\n\nVisual regression test passed!"
	PR_MESSAGE="Visual regression test passed! $REPORT_LINK"
fi

# Post the image back to the pull request on GitHub
if [[ -n ${CI_REPOSITORY_URL} && ${CI_REPOSITORY_URL} == *"github"* ]]
then
    echo -e "\nPosting visual regression results back to PR #$PR_NUMBER "
    curl -s -i -u "$CI_PROJECT_USERNAME:$GITHUB_TOKEN" -d "{\"body\": \"$PR_MESSAGE\"}" $GITHUB_API_URL/issues/$PR_NUMBER/comments > /dev/null
fi

if [[ ${VISUAL_REGRESSION_RESULTS} == *"Mismatch errors found"* ]]
then
    exit 1
fi