<?php

namespace Theme\Bridge;

use Drosalys\Wordpress\Kernel\Bridge\BridgeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Theme\Module\SocialNetwork;
use Twig\Environment;

class ACFBridge implements BridgeInterface
{
    public function bridge(ContainerInterface $container)
    {
        //Load Scripts
        add_action('init', function () use ($container) {
            $this->loadACF($container);
        });
        add_action('init', [$this, 'initOptionPage']);

        add_filter('timber/twig', function (Environment $twig) use ($container) {
            $twig->addGlobal('options', get_fields('option'));
            return $twig;
        });
    }

    public function initOptionPage()
    {
        if (function_exists('acf_add_options_page')) {

            // add parent
            $parent = acf_add_options_page([
                'page_title' => 'Options du site',
                'menu_title' => 'Options',
                'redirect' => false,
                'menu_slug' => 'options'
            ]);

            // add sub page
            acf_add_options_sub_page([
                'page_title' => 'Gestion des reséaux sociaux',
                'menu_title' => 'Reséaux sociaux',
                'parent_slug' => 'options',
            ]);
        }
    }

    private function loadACF(ContainerInterface $container)
    {
        if (function_exists('register_field_group')) {
            //Social Networks Module ACF fields
            SocialNetwork::registerACFFields();

            //Site options
            include_once $container->getParameter('kernel.current_theme_dir') . '/src/ACF/Fields/gallery.php';
            include_once $container->getParameter('kernel.current_theme_dir') . '/src/ACF/Fields/members.php';
            include_once $container->getParameter('kernel.current_theme_dir') . '/src/ACF/Fields/realisations.php';
            include_once $container->getParameter('kernel.current_theme_dir') . '/src/ACF/Fields/site-options.php';
            include_once $container->getParameter('kernel.current_theme_dir') . '/src/ACF/Fields/home.php';
            include_once $container->getParameter('kernel.current_theme_dir') . '/src/ACF/Fields/trainings.php';
            include_once $container->getParameter('kernel.current_theme_dir') . '/src/ACF/Fields/colored-taxonomy.php';
            include_once $container->getParameter('kernel.current_theme_dir') . '/src/ACF/Fields/page-creation-site.php';
            include_once $container->getParameter('kernel.current_theme_dir') . '/src/ACF/Fields/page-formations.php';
        }
    }
}