<?php

declare(strict_types=1);

use ProudNerds\Block\Block;
use ProudNerds\Block\BaseBlock;
use ProudNerds\Block\Context;

final class Example extends BaseBlock
{
    public const SLUG = 'example';

    public function getSlug(): string
    {
        return self::SLUG;
    }

    /**
     * The user-friendly title
     *
     * As shown in the Wordpress 'Add block' menu and when selecting or
     * identifying blocks in the editor.
     */
    public function getTitle(): string
    {
        return 'Voorbeeld blok';
    }

    /**
     * The user-friendly description
     *
     * This as shown in the sidemenu of the Wordpress admin when the block
     * is selected.
     */
    public function getDescription(): string
    {
        return 'Blok met voorbeelden';
    }

    /**
     * The category slug
     *
     * This is the category in which the block is shown in the 'Add block'
     * menu. You may choose from the Block::CATEGORY_* constants.
     * Use Block::CATEGORY_SITE to use the current site name as category.
     */
    public function getCategory(): string
    {
        return Block::CATEGORY_SITE;
    }

    /**
     * The icon
     *
     * This is the DashIcon classname without the 'dashicons-' prefix.
     * This icon is shown in the Wordpress 'Add block' menu.
     * See https://developer.wordpress.org/resource/dashicons/
     *
     * Alternatively, you can return an SVG string.
     */
    public function getIcon(): string
    {
        return 'palmtree';
    }

    /**
     * An array of keywords
     *
     * These keywords are used when searching for a block in the Wordpress
     * 'Add block' menu.
     *
     * This is optional, if no keywords are needed, return an empty array.
     */
    public function getKeywords(): array
    {
        return ['Example', 'Voorbeeld', 'key'];
    }

    /**
     * Return true if you want the user to have the possibility to edit
     * the variables of the block in-place. By default, you can return false.
     */
    public function hasModeSwitch(): bool
    {
        return true;
    }

    /**
     * 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 }}
     */
    public function extendContext(Context $context): void
    {
       // E.g. add posts
        if($context->get('add_latest_posts')){
            $postQuery = [
                'post_type' => 'post',
                'numberposts' => 3,
                'post_status' => 'publish',
            ];

            $context->set('posts', Timber::get_posts($postQuery));
        }

    }

    /**
     * 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 null to allow this block on all post types.
     */
    public function getAllowedPostTypes(): ?array
    {
        return null;
    }

    /**
     * Set the page templates on which this block is available
     *
     * Return an array of page template filenames to only make this block
     * available on those page templates, e.g.
     * return ['page-contact.php'];
     * Return null to allow this block on all post types.
     */
    public function getAllowedPageTemplates(): ?array
    {
        return null;
    }

    /**
     * Change the background color for the custom block-icons
     */
    public function getIconBackgroundColor(): ?string
    {
        return null;
    }
}
