<?php

/**
 * This file contains \QUI\Animate\Events
 */

namespace QUI\Animate;

use QUI;
use QUI\Exception;

use function trim;

/**
 * Class Events
 *
 * @package quiqqer/animate
 */
class Events
{
    /**
     * Event : on template get header
     *
     * @param QUI\Template $Template
     * @throws Exception
     */
    public static function onTemplateGetHeader(QUI\Template $Template): void
    {
        $Project = QUI::getRewrite()->getProject();

        if ($Project->getConfig('animate.general.enableAnimationLibrary')) {
            $Template->extendHeaderWithCSSFile(URL_OPT_DIR . 'quiqqer/animate/bin/animate.min.css');
        }

        if ($Project->getConfig('animate.scroll.enable')) {
            $Template->extendHeaderWithCSSFile(URL_OPT_DIR . 'quiqqer/animate/bin/css/animate-scroll.css');
            $Template->extendFooterWithJavaScriptFile(URL_OPT_DIR . 'quiqqer/animate/bin/javascript/animate-scroll.js');

            // get elements from setting
            $fadeinFromBottom = '.fadein-from-bottom, .fadeIn-from-bottom';
            $fadeinFromLeft = '.fadein-from-left, .fadeIn-from-left';
            $fadeinFromRight = '.fadein-from-right, .fadeIn-from-right';
            $script = '<script type="text/javascript" data-no-cache="1">';

            if (trim($Project->getConfig('animate.scroll.fadeinFromDown')) !== '') {
                $fadeinFromBottom .= ', ' . trim($Project->getConfig('animate.scroll.fadeinFromDown'));
            }

            if (trim($Project->getConfig('animate.scroll.fadeinFromLeft')) !== '') {
                $fadeinFromLeft .= ', ' . trim($Project->getConfig('animate.scroll.fadeinFromLeft'));
            }

            if (trim($Project->getConfig('animate.scroll.fadeinFromRight')) !== '') {
                $fadeinFromRight .= ', ' . trim($Project->getConfig('animate.scroll.fadeinFromRight'));
            }

            $bottom = 'bottom: \'' . $fadeinFromBottom . '\'';
            $left = 'left: \'' . $fadeinFromLeft . '\'';
            $right = 'right: \'' . $fadeinFromRight . '\'';

            $script .= 'const QUI_ANIMATE_SCROLL_EFFECTS = {' . $bottom . ',' . $left . ',' . $right . '};';
            $script .= '</script>';

            $Template->extendHeader($script);

            $css = '<style>' . $fadeinFromBottom . '{opacity: 0}</style>';
            $css .= '<style>' . $fadeinFromLeft . '{opacity: 0}</style>';
            $css .= '<style>' . $fadeinFromRight . '{opacity: 0}</style>';

            $Template->extendHeader($css);
        }
    }
}
