<?php

namespace Dispatch\Config;

use Timber\Timber as Timber;
use Timber\Site as TimberSite;
use Timber\Helper as TimberHelper;
use Twig_Filter_Function;

class Site extends TimberSite {

    public function __construct() {

        if( function_exists('acf_add_options_page') ) {
            acf_add_options_page();
        }

        // Timber::$cache = true;

        Timber::$dirname = [
            'views',
            'fewbricks/brick-layouts',
            'fewbricks/bricks',
        ];

        add_filter( 'get_twig', [$this, 'add_to_twig'] );
        add_filter( 'timber_context', [$this, 'add_to_context'] );

        parent::__construct();

    }

    /**
     * add functions to twig
     *
     * functions added here will be loaded globally
     * consider using the 'dispatch_add_to_twig' filter
     * to conditionally add methods when needed
     */
    public function add_to_twig( $twig ) {

        $twig->addFilter('css_background_image', new Twig_Filter_Function([$this, 'css_background_image']));

        return apply_filters( 'dispatch_add_to_twig', $twig );

    }

    public function css_background_image($text) {
        if ( empty( $text ) ) return '';
        return "background-image: url('$text');";
    }

    /**
     * add functions to timber context
     *
     * variables and methods added here will be loaded globally
     * consider using the 'dispatch_add_to_context' filter
     * to conditionally add methods when needed
     */
    public function add_to_context( $context ) {
        $context['assets'] = get_template_directory_uri() . '/assets';
        return apply_filters( 'dispatch_add_to_context', $context );
    }

}
