<?php

/**
 * @var QUI\Projects\Project $Project
 * @var QUI\Projects\Site $Site
 * @var QUI\Interfaces\Template\EngineInterface $Engine
 * @var QUI\Template $Template
 */

/**
 * Emotion
 */

QUI\Utils\Site::setRecursiveAttribute($Site, 'image_emotion');

/**
 * Logo
 */
$Logo = $Project->getMedia()->getLogoImage();

/**
 * Background
 */
$background            = true;
$ownImage              = false;
$backgroundColor       = '#503677';
$transparency          = 70;
$secondBackgroundColor = false;
$pageBackground        = $Project->getConfig('templateSimple.settings.pageBackground');

// page background?
switch ($pageBackground) {
    // own image?
    case 'ownImage':
        $ownImageUrl = (string)$Project->getConfig('templateSimple.settings.ownImage');

        if ($ownImageUrl) {
            try {
                $ownImage = QUI\Projects\Media\Utils::getImageByUrl($ownImageUrl)->getUrl(true);
            } catch (QUI\Exception) {
                $ownImage = false;
            }
        }
        break;

    case 'none':
        $background = false;
        break;

    default:
        break;
}

// set transparency
if ($Project->getConfig('templateSimple.settings.transparency')) {
    $number = (int)$Project->getConfig('templateSimple.settings.transparency');

    if ($number < 1 || $number > 100) {
        $number = $transparency;
    }

    $transparency = $number;
}

// second background?
switch ($Project->getConfig('templateSimple.settings.secondBackgroundColor')) {
    case 'dark':
        $transparency          = $transparency / 100;
        $secondBackgroundColor = "rgb(0 0 0 / $transparency)";
        break;

    case 'none':
        $secondBackgroundColor = false;
        break;

    default:
        $transparency          = $transparency / 100;
        $secondBackgroundColor = "rgb(255 255 255 / $transparency)";
}

$fontColor = '#5d5d5d';
$mainColor = '#787281';

$getColor = static function (mixed $value, string $fallback): string {
    $value = (string)$value;

    if (preg_match('/^#[0-9a-f]{3}(?:[0-9a-f]{3})?$/i', $value)) {
        return $value;
    }

    return $fallback;
};

$backgroundColor = $getColor(
    $Project->getConfig('templateSimple.settings.backgroundColor'),
    $backgroundColor
);
$fontColor = $getColor($Project->getConfig('templateSimple.settings.fontColor'), $fontColor);
$mainColor = $getColor($Project->getConfig('templateSimple.settings.mainColor'), $mainColor);

$Engine->assign([
    'Logo'                  => $Logo,
    'children'              => $Project->firstChild()->getNavigation(),
    'ownImage'              => $ownImage,
    'background'            => $background,
    'fontColor'             => $fontColor,
    'mainColor'             => $mainColor,
    'backgroundColor'       => $backgroundColor,
    'secondBackgroundColor' => $secondBackgroundColor,
]);
