<?php

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @link       https://themeatelier.net
 * @since      1.0.0
 *
 * @package greet-bubble
 * @subpackage greet-bubble/Frontend
 * @author     ThemeAtelier<themeatelierbd@gmail.com>
 */

namespace ThemeAtelier\GreetBubble\Frontend;

use ThemeAtelier\GreetBubble\Frontend\Templates\Template;
use ThemeAtelier\GreetBubble\Includes\ThemeSupport;

/**
 * The Frontend class to manage all public facing stuffs.
 *
 * @since 1.0.0
 */
class Frontend
{
    /**
     * The slug of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $plugin_slug   The slug of this plugin.
     */
    private $plugin_slug;

    /**
     * The min of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $min   The slug of this plugin.
     */
    private $min;
    /**
     * Initialize the class and set its properties.
     *
     * @since    1.0.0
     * @param      string $plugin_name       The name of the plugin.
     * @param      string $version    The version of this plugin.
     */
    public function __construct()
    {
        $this->min   = defined('WP_DEBUG') && WP_DEBUG ? '' : '.min';
        add_action('wp_footer', [$this, 'greet_bubble_content']);
        new ThemeSupport();
    }

    /**
     * Register the stylesheets for the public-facing side of the site.
     *
     * @since    1.0.0
     */
    public static function enqueue_scripts()
    {
        global $post;
        $assign_bubble = get_option('assign_bubble');
        $assign_bubble_data = isset($assign_bubble['assign_bubble_data']) ? $assign_bubble['assign_bubble_data'] : array();

        $selected_bubble = '';
        $greet_meta = '';
        if (!empty($assign_bubble_data)) {
            foreach ($assign_bubble_data as $assign) {
                $selected_bubble = isset($assign['select_bubble']) ? $assign['select_bubble'] : '';
                $select_by       = isset($assign['select_by']) ? $assign['select_by'] : 'posts';

                if ('taxonomy' === $select_by) {
                    $saved_terms    = isset($assign['bubble_taxonomy_terms']) ? (array) $assign['bubble_taxonomy_terms'] : array();
                    $post_term_ids  = wp_get_object_terms(
                        $post->ID,
                        get_taxonomies(['public' => true]),
                        ['fields' => 'ids']
                    );
                    if (!is_wp_error($post_term_ids) && !empty(array_intersect($saved_terms, $post_term_ids)) && !empty($selected_bubble)) {
                        $greet_meta = get_post_meta($selected_bubble, 'greet_meta', true);
                    }
                } else {
                    $bubble_specific_posts = isset($assign['bubble_specific_posts']) ? $assign['bubble_specific_posts'] : array();
                    foreach ($bubble_specific_posts as $post_id) {
                        if (($post_id == $post->ID) && !empty($selected_bubble)) {
                            $greet_meta = get_post_meta($selected_bubble, 'greet_meta', true);
                        }
                    }
                }
            }
        }

        if (empty($greet_meta)) {
            $greet_meta = get_post_meta(get_the_ID(), '_greet_meta', true);
        }

        if (!empty($greet_meta['video']['url'])) {
            $options = $greet_meta;
            $custom_js              = isset($options['greet_additional_js']) ? $options['greet_additional_js'] : '';
            $additional_css         = isset($options['greet_additional_css']) ? $options['greet_additional_css'] : '';
            wp_enqueue_style('ico-font');
            wp_enqueue_style('greet-style');
            $custom_css = '';
            include 'dynamic-css/dynamic-css.php';
            wp_add_inline_style('greet-style', $custom_css);

            if (!empty($additional_css)) {
                wp_add_inline_style('greet-style', $additional_css);
            }
            wp_enqueue_script('greet-script');
            if (!empty($custom_js)) {
                wp_add_inline_script('greet-script', $custom_js);
            }
        } else {
            $options = get_option('_greet');
            $custom_js              = isset($options['greet_additional_js']) ? $options['greet_additional_js'] : '';
            $additional_css         = isset($options['greet_additional_css']) ? $options['greet_additional_css'] : '';
            wp_enqueue_style('ico-font');
            wp_enqueue_style('greet-style');
            $custom_css = '';
            include 'dynamic-css/dynamic-css.php';
            wp_add_inline_style('greet-style', $custom_css);

            if (!empty($additional_css)) {
                wp_add_inline_style('greet-style', $additional_css);
            }
            wp_enqueue_script('greet-script');
            if (!empty($custom_js)) {
                wp_add_inline_script('greet-script', $custom_js);
            }
        }
        $pause_video = isset($options['pause-video']) ? $options['pause-video'] : false;
        $session_hide = isset($options['session-hide']) ? $options['session-hide'] : false;
        $loop_video = isset($options['loop_video']) ? $options['loop_video'] : true;
        $time_based_load = isset($options['time_based_load']) ? $options['time_based_load'] : 0;
        wp_localize_script(
            'greet-script',
            'frontend_scripts',
            array(
                'pause_on_switch' => esc_attr($pause_video),
                'hide_for_session' => esc_attr($session_hide),
                'loop_video' => esc_attr($loop_video),
                'time_based_load' => esc_attr($time_based_load),
                'greet_ajaxurl' => admin_url('admin-ajax.php'),
                'nonce'   => wp_create_nonce('greet_nonce'),
            )
        );
    }

    public function greet_bubble_content()
    {
        global $post;

        $options = get_option('_greet');
        $assign_bubble = get_option('assign_bubble');
        $assign_bubble_data = isset($assign_bubble['assign_bubble_data']) ? $assign_bubble['assign_bubble_data'] : array();

        $selected_bubble = '';
        if (!empty($assign_bubble_data)) {
            foreach ($assign_bubble_data as $assign) {
                $selected_bubble = isset($assign['select_bubble']) ? $assign['select_bubble'] : '';
                $select_by       = isset($assign['select_by']) ? $assign['select_by'] : 'posts';

                if ($select_by === 'taxonomy') {
                    if (!is_singular()) {
                        continue;
                    }
                    $bubble_taxonomy_terms = isset($assign['bubble_taxonomy_terms']) ? (array) $assign['bubble_taxonomy_terms'] : array();
                    if (!empty($bubble_taxonomy_terms) && !empty($selected_bubble)) {
                        $post_term_ids = wp_get_object_terms(
                            $post->ID,
                            get_taxonomies(array('public' => true)),
                            array('fields' => 'ids')
                        );
                        if (!is_wp_error($post_term_ids) && !empty(array_intersect(array_map('intval', $bubble_taxonomy_terms), $post_term_ids))) {
                            $greet_meta = get_post_meta($selected_bubble, 'greet_meta', true);
                            if (!empty($greet_meta['video']['url'])) {
                                Template::content($greet_meta);
                            }
                            return;
                        }
                    }
                } else {
                    $queried_id = 0;
                    if (!empty($post) && isset($post->ID)) {
                        $queried_id = $post->ID;
                    }

                    if (function_exists('is_shop') && is_shop()) {
                        $queried_id = wc_get_page_id('shop');
                    }

                    if (function_exists('is_checkout') && is_checkout()) {
                        $queried_id = wc_get_page_id('checkout');
                    }
                    if (function_exists('is_cart') && is_cart()) {
                        $queried_id = wc_get_page_id('cart');
                    }
                    $bubble_specific_posts = isset($assign['bubble_specific_posts']) ? $assign['bubble_specific_posts'] : array();
                    foreach ($bubble_specific_posts as $post_id) {
                        if (($post_id == $queried_id) && !empty($selected_bubble)) {
                            $greet_meta = get_post_meta($selected_bubble, 'greet_meta', true);
                            if (!empty($greet_meta['video']['url'])) {
                                Template::content($greet_meta);
                            }
                            return;
                        }
                    }
                }
            }
        }

        $video = isset($options['video']) ? $options['video'] : '';
        $video_url = isset($video['url']) ? $video['url'] : '';

        $greet_meta = get_post_meta(get_the_ID(), '_greet_meta', true);
        if (!empty($greet_meta['video']['url'])) {
            Template::content($greet_meta);
        } elseif ($video_url) {
            Template::content($options);
        }
    }
}
