<?php

namespace Dispatch\Config;

use Dispatch\Plugins\YoastSEO as YoastSEO;
use Timber\Timber as Timber;
use Timber\Helper as TimberHelper;
use Timber\Menu as TimberMenu;
use Timber\Post as TimberPost;
use Timber\User as TimberUser;
use Timber\Image as TimberImage;

class Context {

    public function __construct() {

        add_filter( 'dispatch_add_to_context', [$this, 'add_to_context'] );

    }

    public function add_to_context( $context ) {

        $user = new TimberUser();

        /**
         * pages
         */
        // $context['page'] = new TimberPost(get_field('page_id', 'options', false));

        /**
         * forms
         */

        $context['show_explore_modal'] = isset($_COOKIE['explore']) && $_COOKIE['explore'] ? false : true;
        $context['show_onboarding_modal'] =false;
        // $context['show_onboarding_modal'] = isset($_COOKIE['onboarding']) && $_COOKIE['onboarding'] ? false : true;

        $context['subscribe_form_id'] = get_field('subscribe_form', 'options')['id'];
        $context['subscribe_form_shortcode'] = '[gravityform id="' . get_field('subscribe_form', 'options')['id'] . '" title="false" description="false" ajax="true" tabindex="200" ]';
        $context['get_related_posts'] = TimberHelper::function_wrapper( [$this, 'get_related_posts'], array( null, null, 2 ) );
        $context['get_default_thumbnail'] = TimberHelper::function_wrapper( [$this, 'get_default_thumbnail'], array() );
        $context['get_primary_term'] = TimberHelper::function_wrapper( [$this, 'get_primary_term'], array( null, null) );

        $context['news_link'] = get_post_type_archive_link('news');
        $context['media_link'] = get_post_type_archive_link('media');
        $context['faq_link'] = get_post_type_archive_link('faq');

        $context['contact_page'] = new TimberPost(get_field('contact_page_id', 'options', false));
        $context['legal_page'] = new TimberPost(get_field('legal_page_id', 'options', false));
        $context['construction_page'] = new TimberPost(get_field('construction_page_id', 'options', false));
        $context['explore_page'] = new TimberPost(get_field('explore_page_id', 'options', false));

        $context['facebook_url'] = get_field('social_facebook', 'options');
        $context['twitter_url'] = get_field('social_twitter', 'options');
        $context['google_url'] = get_field('social_google', 'options');
        $context['instagram_url'] = get_field('social_instagram', 'options');
        $context['youtube_url'] = get_field('social_youtube', 'options');

        $context['footer_right'] = get_field('footer_right', 'options');

        $maintain_thumbnail_aspect_ratio = get_field('maintain_thumbnail_aspect_ratio');
        $context['maintain_thumbnail_aspect_ratio'] = isset( $maintain_thumbnail_aspect_ratio ) && $maintain_thumbnail_aspect_ratio ? true : false;

        return $context;

    }

    public function get_related_posts($post, $term, $count = 2) {
        if ( empty( $post ) ) return array();
        if ( empty( $term ) ) return array();

        $primary_category = YoastSEO::get_primary_term( $term, $post->ID );
        return \Timber::get_posts( [
                'post_type' => $post->post_type,
                'posts_per_page' => $count,
                'post__not_in' => [$post->ID],
                'tax_query' => array(
                    array(
                        'taxonomy' => $term,
                        'field' => 'slug',
                        'terms' => $primary_category->slug
                    )
                )
            ] );
    }

    public function get_primary_term($post, $term, $count = 2) {
        if ( empty( $post ) ) return array();
        if ( empty( $term ) ) return array();

        return YoastSEO::get_primary_term( $term, $post->ID );
    }

    public function get_default_thumbnail() {
        $default_images = get_field('default_images', 'options');
        return new TimberImage( $default_images[array_rand($default_images)]['default_images_image'] );
    }

}
