<?php

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

namespace QUI\TemplateAvadaAgency;

use QUI;

/**
 * Help Class for Template Avada Agency
 *
 * @package QUI\TemplateAvadaAgency
 */
class Utils
{
    /**
     * @param array{
     *     Project: QUI\Projects\Project,
     *     Site: QUI\Projects\Site,
     *     Template: QUI\Template
     * } $params
     * @return array<string, mixed>
     */
    public static function getConfig(array $params): array
    {
        /* @var $Project QUI\Projects\Project */
        $Project   = $params['Project'];
        $Template  = $params['Template'];
        $Site      = $params['Site'];
        $cacheName = md5($Project->getName() . $Project->getLang() . $Site->getId());

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

        $config = [];

        $showFooterNav = false;
        if ($Project->getConfig('templateAvadaAgency.settings.footer.showFooterNav')) {
            $showFooterNav = $Project->getConfig('templateAvadaAgency.settings.footer.showFooterNav');
        }

        /**
         * no header?
         * Body Class
         *
         * own site type
         */

        $showHeader    = false;
        $showPageTitle = true;
        $showPageShort = true;

        switch ($Template->getLayoutType()) {
            case 'layout/startPage':
                $showHeader    = $Project->getConfig('templateAvadaAgency.settings.showHeaderStartPage');
                $showPageTitle = $Project->getConfig('templateAvadaAgency.settings.general.showTitleStartPage');
                $showPageShort = $Project->getConfig('templateAvadaAgency.settings.general.showShortStartPage');
                break;

            case 'layout/noSidebar':
                $showHeader    = $Project->getConfig('templateAvadaAgency.settings.showHeaderNoSidebar');
                $showPageTitle = $Project->getConfig('templateAvadaAgency.settings.general.showTitleNoSidebar');
                $showPageShort = $Project->getConfig('templateAvadaAgency.settings.general.showShortNoSidebar');
                break;

            case 'layout/noSidebarSmall':
                $showHeader    = $Project->getConfig('templateAvadaAgency.settings.showHeaderNoSidebarSmall');
                $showPageTitle = $Project->getConfig('templateAvadaAgency.settings.general.showTitleNoSidebarSmall');
                $showPageShort = $Project->getConfig('templateAvadaAgency.settings.general.showShortNoSidebarSmall');
                break;

            case 'layout/rightSidebar':
                $showHeader    = $Project->getConfig('templateAvadaAgency.settings.showHeaderRightSidebar');
                $showPageTitle = $Project->getConfig('templateAvadaAgency.settings.general.showTitleRightSidebar');
                $showPageShort = $Project->getConfig('templateAvadaAgency.settings.general.showShortRightSidebar');
                break;

            case 'layout/leftSidebar':
                $showHeader    = $Project->getConfig('templateAvadaAgency.settings.showHeaderLeftSidebar');
                $showPageTitle = $Project->getConfig('templateAvadaAgency.settings.general.showTitleLeftSidebar');
                $showPageShort = $Project->getConfig('templateAvadaAgency.settings.general.showShortLeftSidebar');
                break;
        }


        /* site own settings: show page title */
        switch ($Site->getAttribute('templateAvadaAgency.showTitle')) {
            case 'show':
                $showPageTitle = true;
                break;
            case 'hide':
                $showPageTitle = false;
        }

        /* site own settings: show page short description */
        switch ($Site->getAttribute('templateAvadaAgency.showShort')) {
            case 'show':
                $showPageShort = true;
                break;
            case 'hide':
                $showPageShort = false;
        }

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

        /**
         * Breadcrumb
         */
        $showBreadcrumb = 'displayInHeader';
        switch ($Project->getConfig('templateAvadaAgency.settings.general.breadcrumb')) {
            case 'displayUnderHeader':
                $showBreadcrumb = 'displayUnderHeader';
                break;
            case 'hide':
                $showBreadcrumb = false;
        }

        /* site own settings: breadcrumb */
        switch ($Site->getAttribute('templateAvadaAgency.breadcrumb')) {
            case 'displayInHeader':
                $showBreadcrumb = 'displayInHeader';
                break;
            case 'displayUnderHeader':
                $showBreadcrumb = 'displayUnderHeader';
                break;
            case 'hide':
                $showBreadcrumb = false;
        }

        /**
         * Template footer settings
         */
        $footerTemplate = false;
        if ($Project->getConfig('templateAvadaAgency.settings.footerTemplate.enable')) {
            $footerTemplate = $Project->getConfig('templateAvadaAgency.settings.footerTemplate.enable');
            $config         += self::getFooterTemplate($Project);
        }

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

        $config += [
            'showHeader'     => $showHeader,
            'showBreadcrumb' => $showBreadcrumb,
            'showFooterNav'  => $showFooterNav,
            'settingsCSS'    => '<style data-no-cache="1">' . $settingsCSS . '</style>',
            'typeClass'      => 'type-' . str_replace(['/', ':'], '-', $Site->getAttribute('type')),
            'showPageTitle'  => $showPageTitle,
            'showPageShort'  => $showPageShort,
            'footerTemplate' => $footerTemplate
        ];

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

        return $config;
    }

    /**
     * @return array<string, mixed>
     */
    private static function getFooterTemplate(QUI\Projects\Project $Project): array
    {
        $lang                 = $Project->getLang();
        $footerTemplateConfig = [];
        $where['active']      = 1;

        // default
        $footerTemplateConfig['footerTemplate'] = [
            'linksBox'     => null,
            'linksBoxMore' => null,
            'recent'       => null,
            'shortText'    => null
        ];

        /**
         * Links box
         */
        if ($Project->getConfig('templateAvadaAgency.settings.footerTemplate.linksBox.show')) {
            $titleArray = json_decode($Project->getConfig(
                'templateAvadaAgency.settings.footerTemplate.linksBox.title'
            ), true);

            $title = false;
            if (isset($titleArray[$lang])) {
                $title = $titleArray[$lang];
            }

            // order the box in footer
            $linksBoxPriority = 1;
            if (
                $Project->getConfig(
                    'templateAvadaAgency.settings.footerTemplate.linksBox.priority'
                )
            ) {
                $linksBoxPriority = (int)$Project->getConfig(
                    'templateAvadaAgency.settings.footerTemplate.linksBox.priority'
                );
            }

            $parents = $Project->getConfig(
                'templateAvadaAgency.settings.footerTemplate.linksBox.sites'
            );

            $sites = QUI\Projects\Site\Utils::getSitesByInputList($Project, $parents, [
                'where' => $where,
                'limit' => 10,
                'order' => $Project->getConfig('templateAvadaAgency.settings.footerTemplate.linksBox.sites.order')
            ]);

            $footerTemplateConfig['footerTemplate']['linksBox'] = [
                'title'    => $title,
                'sites'    => $sites,
                'priority' => $linksBoxPriority
            ];
        }

        /**
         * Links box (second / more links)
         */
        if ($Project->getConfig('templateAvadaAgency.settings.footerTemplate.linksBoxMore.show')) {
            $titleArray = json_decode($Project->getConfig(
                'templateAvadaAgency.settings.footerTemplate.linksBoxMore.title'
            ), true);

            $title = false;
            if (isset($titleArray[$lang])) {
                $title = $titleArray[$lang];
            }

            // order the box in footer
            $linksBoxPriority = 1;
            if (
                $Project->getConfig(
                    'templateAvadaAgency.settings.footerTemplate.linksBoxMore.priority'
                )
            ) {
                $linksBoxPriority = (int)$Project->getConfig(
                    'templateAvadaAgency.settings.footerTemplate.linksBoxMore.priority'
                );
            }

            $parents = $Project->getConfig(
                'templateAvadaAgency.settings.footerTemplate.linksBoxMore.sites'
            );

            $sites = QUI\Projects\Site\Utils::getSitesByInputList($Project, $parents, [
                'where' => $where,
                'limit' => 10,
                'order' => $Project->getConfig('templateAvadaAgency.settings.footerTemplate.linksBoxMore.sites.order')
            ]);

            $footerTemplateConfig['footerTemplate']['linksBoxMore'] = [
                'title'    => $title,
                'sites'    => $sites,
                'priority' => $linksBoxPriority
            ];
        }

        /**
         * Recent (blog / news)
         */
        if ($Project->getConfig('templateAvadaAgency.settings.footerTemplate.recent.show')) {
            $titleArray = json_decode($Project->getConfig(
                'templateAvadaAgency.settings.footerTemplate.recent.title'
            ), true);

            $title = false;
            if (isset($titleArray[$lang])) {
                $title = $titleArray[$lang];
            }


            // order the box in footer
            $linksBoxPriority = 1;
            if (
                $Project->getConfig(
                    'templateAvadaAgency.settings.footerTemplate.recent.priority'
                )
            ) {
                $linksBoxPriority = (int)$Project->getConfig(
                    'templateAvadaAgency.settings.footerTemplate.recent.priority'
                );
            }

            $parents = $Project->getConfig(
                'templateAvadaAgency.settings.footerTemplate.recent.sites'
            );

            $sites = QUI\Projects\Site\Utils::getSitesByInputList($Project, $parents, [
                'where' => $where,
                'limit' => (int)$Project->getConfig('templateAvadaAgency.settings.footerTemplate.recent.limit'),
                'order' => $Project->getConfig('templateAvadaAgency.settings.footerTemplate.recent.sites.order')
            ]);

            $footerTemplateConfig['footerTemplate']['recent'] = [
                'title'    => $title,
                'sites'    => $sites,
                'priority' => $linksBoxPriority
            ];
        }

        /**
         * Short text
         */
        if ($Project->getConfig('templateAvadaAgency.settings.footerTemplate.shortText.show')) {
            $titleArray = json_decode($Project->getConfig(
                'templateAvadaAgency.settings.footerTemplate.shortText.title'
            ), true);

            $title = false;
            if (isset($titleArray[$lang])) {
                $title = $titleArray[$lang];
            }

            // order the box in footer
            $linksBoxPriority = 1;
            if (
                $Project->getConfig(
                    'templateAvadaAgency.settings.footerTemplate.shortText.priority'
                )
            ) {
                $linksBoxPriority = (int)$Project->getConfig(
                    'templateAvadaAgency.settings.footerTemplate.shortText.priority'
                );
            }

            $footerTemplateConfig['footerTemplate']['shortText'] = [
                'title'    => $title,
                'content'  => $Project->getConfig('templateAvadaAgency.settings.footerTemplate.shortText.content'),
                'priority' => $linksBoxPriority
            ];
        }

        return $footerTemplateConfig;
    }

    /**
     * Convert #hex value to rgb(a)
     * inspired by https://stackoverflow.com/a/31934345
     *
     * returns array...
     * Array (
     *     [r] => 25
     *     [g] => 182
     *     [b] => 152
     *     [a] => 1
     * )
     *
     * or string in rgba format...
     * "255,255,255,1"
     *
     * @param string $hex
     * @param float|int|bool $alpha
     * @param bool $rgbaFormat
     * @return ($rgbaFormat is true
     *     ? string
     *     : array{r: int, g: int, b: int, a?: float|int|bool})
     */
    public static function hexToRgb(
        string $hex,
        float|int|bool $alpha = false,
        bool $rgbaFormat = false
    ): array|string {
        $hex      = str_replace('#', '', $hex);
        $length   = strlen($hex);
        $rgb['r'] = intval($length === 6 ? substr($hex, 0, 2) : ($length === 3 ? str_repeat($hex[0], 2) : '0'), 16);
        $rgb['g'] = intval($length === 6 ? substr($hex, 2, 2) : ($length === 3 ? str_repeat($hex[1], 2) : '0'), 16);
        $rgb['b'] = intval($length === 6 ? substr($hex, 4, 2) : ($length === 3 ? str_repeat($hex[2], 2) : '0'), 16);

        if ($alpha) {
            $rgb['a'] = $alpha;
        }

        //If you'd like to return the rgb(a) in CSS format...
        if ($rgbaFormat) {
            return implode(array_keys($rgb)) . '(' . implode(', ', $rgb) . ')';
        }

        return $rgb;
    }
}
