<?php

declare(strict_types=1);

namespace ProudNerds\Block;

use ProudNerds\Components\ComponentSettings;
use StoutLogic\AcfBuilder\FieldNameCollisionException;
use StoutLogic\AcfBuilder\FieldsBuilder;

final class Example extends BaseBlock
{
    /**
     * @var string $title Block title.
     */
    public static string $title;

    /**
     * @var string $description Block description.
     */
    public static string $description;

    /**
     * @var string $icon Block icon.
     */
    public static string $icon = 'palmtree';

    /**
     * @var array|string[] $keywords Block search words.
     */
    public static array $keywords = ['Voorbeeld', 'Example', 'Voorbeeld blok', 'Example block'];

    public function __construct()
    {
        $this::$title = __('Voorbeeld blok', 'proudnerds');
        $this::$description = __('Blok als voorbeeld', 'proudnerds');
    }


    /**
     * Update the block context.
     *
     * Here, you can add, remove or change the context before it is
     * passed to Twig (within twig, access this context by using
     * {{ block }}
     *
     * @param Context $context
     *
     * @return void
     */
    public function extendContext(Context $context): void
    {

        //        $postType = $context->get('post_type');
        //        $context->set('postType', $postType);
    }

    /**
     * Set the post types on which this block is available.
     *
     * Return an array of post-type strings to only make this block
     * available on those post types.
     *
     * @return ?array Return null to allow this block on all post types.
     */
    public function getAllowedPostTypes(): ?array
    {
        return null;
    }

    /**
     * Create an example of the block for showing in the preview
     * Add all attributes that you want to preview to the data array
     * use the same keys as you use in the setFields method
     * @return array[]
     */
    public function getExample(): array
    {
        return [
            'attributes' => [
                'mode' => 'preview',
                'data' => [
                    'title' => 'Voorbeeld block',
                    'text' => 'Dit is een voorbeeld block met preview',
                ],
            ],
        ];
    }

    /**
     * No more need to go into the ACF settings in the backend. You still could, but why would you?
     * Set ACF fields according to: https://github.com/StoutLogic/acf-builder
     * See: https://github.com/StoutLogic/acf-builder/wiki
     * or: https://github.com/Log1x/acf-builder-cheatsheet
     *
     * @param FieldsBuilder $fields
     *
     * @return FieldsBuilder
     * @throws FieldNameCollisionException
     */
    public function setFields(FieldsBuilder $fields): FieldsBuilder
    {

        $slug = $this->getSlug();
        $fields->addText('title', [
            'label' => 'Titel',
            'default_value' => 'Voorbeeld block',
        ]);

        $fields->addWysiwyg('text', [
            'label' => 'Tekst',
        ]);

        if (ComponentSettings::isBlockSettingActive($slug, 'add_background')) {
            $fields->addTrueFalse('add_background', [
                'label' => 'Voeg achtergrond toe',
            ]);
        }

        return $fields;
    }
}
