<?php

use ProudNerds\WordPress\ACF\Composer as ACFComposer;
use ProudNerds\WordPress\ACF\BuildingBlocks\{
    BlockHighlighted,
    BlockVideoOrImage,
    BlockPostArchive
};

/**
 * Class: ProudNerds
 *
 * Starting point for the Proud Nerds base theme functions. Runs the bootstrap file that autoloads necessary classes.
 */
class ProudNerds
{
    /**
     * ProudNerds constructor.
     */
    public function __construct()
    {
        $this->init();
        $this->call();
        $this->buildingBlocks();
    }

    /**
     * Initializes the project. Calls required functions.
     */
    private function init()
    {
        // Require composer packages
        require_once(VENDOR_DIR . '/autoload.php');
        require_once(get_template_directory() . '/includes/Bootstrap.php');

        ProudNerds\Bootstrap::init();

        new ProudNerds\Core\Timber();
        new ProudNerds\Core\Cleaner();
        new ProudNerds\Plugins\ACF();
        new ProudNerds\Plugins\Yoast();
        new ProudNerds\Helpers\TwigExtension();
        if (USE_GUTENBERG) {
            $this->gutenbergSupport();
        }
    }

    private function gutenbergSupport()
    {
        new \ProudNerds\Block\Theme();
    }


    /**
     * Calls the required functions for the child theme.
     */
    private function call()
    {
        // Core functionality for the child theme
        new ProudNerds\Core\Assets();
        new ProudNerds\Core\Theme();

        // Custom post types
        ProudNerds\PostTypes\Example::factory();
    }

    /**
     * Add the building blocks needed for this project in the composer file like so:
     *
     *     "require": {
     *          "proudnerds/acf-building-block-highlighted": "^1.0.0",
     *          "proudnerds/acf-building-block-video-or-image": "^1.0.0"
     *      },
     *
     * And initialize them below if not using the default setting (all active for pages).
     */
    private function buildingBlocks()
    {
        // Uncomment the following line to disable the default building block setup
        add_filter('pn_setup_default_building_blocks', '__return_false');

        // Setup custom ACF Composer elements per post type, or group of post types by calling this multiple times
        // with different parameters.
        try {
            ACFComposer::factory(
                [''],
                [
                    BlockHighlighted::id(),
                    BlockVideoOrImage::id(),
                    BlockPostArchive::id()
                ]
            )->create();
        } catch (Exception $e) {
            error_log($e->getMessage());
        }
    }

}

new ProudNerds();