<?php

/**
 * This file contains QUI\TemplateWhoIAm\Utils
 */

namespace QUI\TemplateWhoIAm;

use QUI;

/**
 * Help Class for Template Who I Am
 *
 * @return array
 * @author www.pcsg.de (Michael Danielczok)
 *
 * @package QUI\TemplateWhoIAm
 */
class Utils
{
    /**
     * @param array $params
     * @return array
     */
    public static function getConfig($params)
    {
        /* @var $Project QUI\Projects\Project */
        $Project       = $params['Project'];
        $Template      = $params['Template'];
        $Site          = $params['Site'];
        $BricksManager = $params['BricksManager'];
        $cacheName     = md5($Project->getName() . $Project->getLang() . $Site->getId());

        try {
            return QUI\Cache\Manager::get(
                'quiqqer/templateWhoIAm/' . $cacheName
            );
        } catch (QUI\Exception $Exception) {
        }

        $config = [];

        $showHeader      = false;
        $showTitle       = $Project->getConfig('templateWhoIAm.settings.showTitle');
        $showShort       = $Project->getConfig('templateWhoIAm.settings.showShort');
        $bodyClass       = '';
        $layoutStartPage = false;

        switch ($Template->getLayoutType()) {
            case 'layout/startPage':
                $showHeader      = $Project->getConfig('templateWhoIAm.settings.showHeaderStartPage');
                $bodyClass       = 'start-page';
                $layoutStartPage = true;
                break;

            case 'layout/noSidebar':
                $showHeader = $Project->getConfig('templateWhoIAm.settings.showHeaderNoSidebar');
                $bodyClass  = 'no-sidebar';
                break;

            case 'layout/noSidebarSlim':
                $showHeader = $Project->getConfig('templateWhoIAm.settings.showHeaderNoSidebarSlim');
                $bodyClass  = 'no-sidebar-slim';
                break;
        }

        /* site own show header */
        switch ($params['Site']->getAttribute('templateWhoIAm.showHeader')) {
            case 'show':
                $showHeader = true;
                break;
            case 'hide':
                $showHeader = false;
        }

        /* site own show title */
        switch ($params['Site']->getAttribute('templateWhoIAm.showTitle')) {
            case 'show':
                $showTitle = true;
                break;
            case 'hide':
                $showTitle = false;
                break;
        }

        /* site own show short description */
        switch ($params['Site']->getAttribute('templateWhoIAm.showShort')) {
            case 'show':
                $showShort = true;
                break;
            case 'hide':
                $showShort = false;
                break;
        }

        /**
         * Menu entries
         */
        $subPage     = false;
        $StartPage   = $Site;
        $menuEntries = [];
        $menuHeight  = 80;

        if ($Project->getConfig('templateWhoIAm.settings.menuHeight') && $Project->getConfig('templateWhoIAm.settings.menuHeight') > 0) {
            $menuHeight = $Project->getConfig('templateWhoIAm.settings.menuHeight');
        }

        if ($Site->getId() !== 1) {
            $subPage   = true;
            $StartPage = $Project->get(1);
        }

        $bricks = \array_merge(
            $BricksManager->getBricksByArea('startpageHeader', $StartPage),
            $BricksManager->getBricksByArea('startpageContent', $StartPage),
            $BricksManager->getBricksByArea('headerSuffix', $StartPage),
            $BricksManager->getBricksByArea('footerPrefix', $StartPage)
        );

        foreach ($bricks as $Brick) {
            if (!$Brick->getSetting('navText')) {
                continue;
            }

            $navText = $Brick->getSetting('navText');

            if ($Brick->getSetting('navTarget')) {
                $target = \rawurlencode(str_replace(' ', '-', $Brick->getSetting('navTarget')));
            } else {
                $target = \rawurlencode(str_replace(' ', '-', $Brick->getSetting('navText')));
            }

            $menuEntries[] = ['navText' => $navText, 'target' => $target];
        }

        /**
         * Page scroll Offset
         * set extra CSS class after X pixels
         */
        $pageScrollOffset = intval($Project->getConfig('templateWhoIAm.settings.pageScrollOffset'));

        if ($pageScrollOffset < 0) {
            $pageScrollOffset = 0;
        }

        $settingsCSS = include 'settings.css.php';

        /**
         * Project logo
         */
        $Logo       = $Project->getMedia()->getLogoImage();
        $logoHeight = 60;
        $logoWidth  = false;

        if ($Project->getConfig('templateWhoIAm.settings.logoHeight') && $Project->getConfig('templateWhoIAm.settings.logoHeight') > 0) {
            $logoHeight = $Project->getConfig('templateWhoIAm.settings.logoHeight');
        }

        if ($Logo) {
            try {
                $logoWidth = $Logo->getResizeSize(false, $logoHeight)['width'];
            } catch (QUI\Exception $Exception) {
                QUI\System\Log::addNotice($Exception->getMessage());
            }
        }

        /**
         * Include demo css
         */
        $includeDemoCss = $Project->getConfig('templateWhoIAm.settings.includeDemoStyling');

        $config += [
            'settingsCSS'      => '<style data-no-cache="1">' . $settingsCSS . '</style>',
            'menuEntries'      => $menuEntries,
            'subPage'          => $subPage,
            'showHeader'       => $showHeader,
            'showTitle'        => $showTitle,
            'showShort'        => $showShort,
            'bodyClass'        => $bodyClass,
            'layoutStartPage'  => $layoutStartPage,
            'urlList'          => self::getUrlList($Project),
            'scrollOffset'     => $Project->getConfig('templateWhoIAm.settings.scrollOffset') ? $Project->getConfig('templateWhoIAm.settings.scrollOffset') : 100,
            'pageScrollOffset' => $pageScrollOffset,
            'typeClass'        => 'type-' . str_replace(['/', ':'], '-', $params['Site']->getAttribute('type')),
            'logoHeight'       => $logoHeight,
            'logoWidth'        => $logoWidth,
            'includeDemoCss'   => $includeDemoCss
        ];

        // set cache
        QUI\Cache\Manager::set(
            'quiqqer/templateWhoIAm/' . $cacheName,
            $config
        );

        return $config;
    }

    /**
     * @param $Project QUI\Projects\Project
     */
    private static function getUrlList($Project)
    {
        $urlList = [];

        if ($Project->getConfig('templateWhoIAm.settings.showFooterUrlList')) {

            /** start page */
            $StartPage = $Project->get(1);

            $urlList['startPage'] = [
                'title' => $StartPage->getAttribute('title'),
                'url'   => $StartPage->getUrlRewritten()
            ];

            /** legal notes */
            $legalNotes = $Project->getSites([
                'where' => [
                    'type' => [
                        'type'  => 'IN',
                        'value' => 'quiqqer/sitetypes:types/legalnotes'
                    ]
                ],
                'limit' => 1
            ]);

            if (count($legalNotes)) {
                $urlList['legalNotes'] = [
                    'title' => $legalNotes[0]->getAttribute('title'),
                    'url'   => $legalNotes[0]->getUrlRewritten()
                ];
            }

            /** privacy policy */
            $privacyPolicy = $Project->getSites([
                'where' => [
                    'type' => [
                        'type'  => 'IN',
                        'value' => 'quiqqer/sitetypes:types/privacypolicy'
                    ]
                ],
                'limit' => 1
            ]);

            if (count($privacyPolicy)) {
                $urlList['privacyPolicy'] = [
                    'title' => $privacyPolicy[0]->getAttribute('title'),
                    'url'   => $privacyPolicy[0]->getUrlRewritten()
                ];
            }

            /** general terms and conditions */
            $generalTermsAndConditions = $Project->getSites([
                'where' => [
                    'type' => [
                        'type'  => 'IN',
                        'value' => 'quiqqer/sitetypes:types/generalTermsAndConditions'
                    ]
                ],
                'limit' => 1
            ]);

            if (count($generalTermsAndConditions)) {
                $urlList['generalTermsAndConditions'] = [
                    'title' => $generalTermsAndConditions[0]->getAttribute('title'),
                    'url'   => $generalTermsAndConditions[0]->getUrlRewritten()
                ];
            }
        }

        return $urlList;
    }
}
