<?php

declare(strict_types=1);

namespace ProudNerds\Components;

use StoutLogic\AcfBuilder\FieldNameCollisionException;

class Header
{
    /**
     * @throws FieldNameCollisionException
     */
    public function __construct()
    {
        $this->addFilters();
        $this->addActions();
        $this->setFields();
    }

    /**
     * Register a menu.
     * @return void
     */
    public function registerMenu(): void
    {
        register_nav_menus([
            'header' => 'Header',
        ]);
    }

    /**
     * @param $context
     * @return array
     */
    public function addTimberContext($context): array
    {
        $context['header_menu'] = wp_get_nav_menu_items('Header');
        $context['header_variation'] = ComponentSettings::getComponentSetting('header', 'variation');
        $context['search_variation'] = ComponentSettings::getComponentSetting('header', 'search_variation');

        return $context;
    }


    /**
     * Add fields to the theme options
     * @throws FieldNameCollisionException
     */
    public function setFields(): void
    {
        $themeOptions = \ProudNerds\Helpers\FieldsBuilder::getInstance(\ProudNerds\Helpers\FieldsBuilder::THEME_OPTIONS);
        $componentSettings = ComponentSettings::getComponentSettings('header');

        $themeOptions->addTab('header', [
            'placement' => 'left',
            'label' => 'Header',
        ]);

        $themeOptions->addLink('header_button', [
            'label' => __('Header knop', 'proudnerds'),
            'description' => __('Knop staat rechts in de header', 'proudnerds'),
        ]);

        if ($componentSettings['search_variation'] ?? '' === 'Menu-v1') {
            if ($componentSettings['burgermenu_button']) {
                $themeOptions->addLink('menu_button', [
                    'label' => __('Menu knop', 'proudnerds'),
                    'description' => __('Knop staat in het burgermenu', 'proudnerds'),
                ]);
            }

            if ($componentSettings['burgermenu_contact']) {
                $themeOptions->addText('menu_contact', [
                    'label' => __('Contactgegevens', 'proudnerds'),
                    'description' => __('Contactgegevens tonen in het burgermenu', 'proudnerds'),
                ]);
            }

            if ($componentSettings['burgermenu_socials']) {
                $themeOptions->addTrueFalse('menu_socials', [
                    'label' => __('Social media iconen tonen', 'proudnerds'),
                    'description' => __('Social media iconen tonen in het burgermenu', 'proudnerds'),
                    'ui' => 1,
                    'ui_on_text' => __('Ja', 'proudnerds'),
                    'ui_off_text' => __('Nee', 'proudnerds'),
                ]);
            }
        }

        if ($componentSettings['search_variation'] ?? '' === 'Search-v3') {
            $themeOptions->addTab('search', [
                'placement' => 'left',
                'label' => 'Search',
            ]);

            $themeOptions->addText('search_title', [
                'label' => __('Zoek popup titel', 'proudnerds'),
                'description' => __('Titel toont in de zoek popup', 'proudnerds'),
                'value' => 'Waar ben je naar op zoek?',
            ]);

            $themeOptions->addText('search_placeholder', [
                'label' => __('Zoeken placeholder', 'proudnerds'),
                'description' => __('Placholder tekst van het zoekveld in de zoek popup', 'proudnerds'),
            ]);

            $themeOptions->addText('frequently_searched_title', [
                'label' => __('Zoek popup titel', 'proudnerds'),
                'description' => __('Titel toont in de zoek popup', 'proudnerds'),
                'value' => 'Veel gezocht',
            ]);

            $themeOptions->addRepeater('frequently_searched_posts', [
                'label' => __('Veel gezochte posts', 'proudnerds'),
                'description' => __('Voeg veel gezocht posts toe', 'proudnerds'),
                'max' => 6,
            ])->addLink('select_post', [
                'label' => __('Selecteer post', 'proudnerds'),
            ]);
        }
    }

    /**
     * Add actions.
     * @return void
     */
    private function addActions(): void
    {
        add_action('init', [$this, 'registerMenu']);
    }

    /**
     * Add filters.
     * @return void
     */
    private function addFilters(): void
    {
        add_filter('timber/context', [$this, 'addTimberContext'], 11, 1);
    }
}
