<?php

namespace QUI\WebsiteLocker\Controls;

use QUI;
use QUI\Exception;
use QUI\WebsiteLocker\Robots;

use function dirname;

/**
 * Class SiteLock
 *
 * @package QUI\WebsiteLocker\Controls
 */
class SiteLock extends QUI\Control
{
    /**
     * ContractList constructor.
     *
     * @param array<string, mixed> $attributes
     */
    public function __construct(array $attributes = [])
    {
        // defaults
        $this->setAttributes([
            'title' => false,
            'description' => false,
            'Site' => false,
            'backgroundImage' => false
        ]);

        parent::__construct($attributes);

        $this->addCSSFile(dirname(__FILE__) . 'SiteLock.css');
    }

    /**
     * @return string
     * @throws Exception
     */
    public function getBody(): string
    {
        $Engine = QUI::getTemplateManager()->getEngine();
        $Site = $this->getSite();
        $Project = $Site->getProject();
        $logo = '';
        $bgImage = $this->getAttribute('backgroundImage');
        $title = $this->getAttribute('title');
        $description = $this->getAttribute('description');
        $logoType = $this->getSite()->getAttribute('quiqqer.website.locker.logo');

        if ($title == '') {
            $title = QUI::getLocale()->get(
                'quiqqer/website-locker',
                'website-locker.control.title'
            );
        }

        if ($description == '') {
            $description = QUI::getLocale()->get(
                'quiqqer/website-locker',
                'website-locker.control.text'
            );
        }

        if ($logoType === "projectLogo") {
            $Logo = $Project->getMedia()->getLogoImage();

            if ($Logo) {
                $logo = '<img src="' . $Logo->getSizeCacheUrl(300, 100) . '" class="logo" />';
            }
        }

        if ($logoType === "ownImage") {
            $OwnImage = $this->getSite()->getAttribute('quiqqer.website.locker.ownImage');

            if ($OwnImage) {
                $logo = '<img src="' . $OwnImage . '" class="logo" />';
            }
        }

        if (!empty($bgImage)) {
            $Engine->assign('backgroundImage', $this->getAttribute('backgroundImage'));
        }

        $Engine->assign([
            'Site' => $Site,
            'title' => $title,
            'description' => $description,
            'logo' => $logo,
            'logoType' => $logoType,
            'robots' => Robots::getDirectives($Site)
        ]);

        return $Engine->fetch(dirname(__FILE__) . '/SiteLock.html');
    }

    /**
     * Return the current site
     *
     * @return QUI\Interfaces\Projects\Site
     * @throws Exception
     */
    public function getSite(): QUI\Interfaces\Projects\Site
    {
        $Site = $this->getAttribute('Site');

        if ($Site instanceof QUI\Interfaces\Projects\Site) {
            return $Site;
        }

        $Site = QUI::getRewrite()->getSite();

        if ($Site instanceof QUI\Interfaces\Projects\Site) {
            return $Site;
        }

        throw new Exception('No current site available.');
    }
}
