<?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     eventful-for-elementor
 * @subpackage  eventful-for-elementor/src/Frontend
 * @author      ThemeAtelier<themeatelierbd@gmail.com>
 */

namespace ThemeAtelier\EventfulForElementor\Frontend;

use ThemeAtelier\EventfulForElementor\Frontend\Helpers\EventfulForElementorFunctions;
use ThemeAtelier\EventfulForElementor\Frontend\Helpers\EventfulForElementorLiveFilter;
use ThemeAtelier\EventfulForElementor\Frontend\Helpers\EventfulForElementorLoopHtml;
use ThemeAtelier\EventfulForElementor\Frontend\Helpers\EventfulForElementorQueryinside;

// don't call the file directly.
if (! defined('ABSPATH')) {
    exit;
}

/**
 * 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';
        $this->load_public_dependencies();
        $this->eventful_for_elemenotr_public_action();
    }

    private function load_public_dependencies()
    {
        new EventfulForElementorLiveFilter();
    }

    private function eventful_for_elemenotr_public_action()
    {
        add_action('wp_ajax_post_grid_ajax', array($this, 'post_grid_ajax'));
        add_action('wp_ajax_nopriv_post_grid_ajax', array($this, 'post_grid_ajax'));

        add_action('wp_ajax_post_pagination_bar', array($this, 'post_pagination_bar'));
        add_action('wp_ajax_nopriv_post_pagination_bar', array($this, 'post_pagination_bar'));

        add_action('wp_ajax_post_pagination_bar_mobile', array($this, 'post_pagination_bar_mobile'));
        add_action('wp_ajax_nopriv_post_pagination_bar_mobile', array($this, 'post_pagination_bar_mobile'));

        add_action('wp_ajax_eventful_for_elementor_post_order', array($this, 'eventful_for_elementor_post_order'));
        add_action('wp_ajax_nopriv_eventful_for_elementor_post_order', array($this, 'eventful_for_elementor_post_order'));
    }

    /**
     * Event Ajax Pagination.
     */
    public function post_grid_ajax()
    {
        if (isset($_POST['nonce']) && !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'ta_efpe_nonce')) {
            return false;
        }
        $views_id            = isset($_POST['id']) ? sanitize_text_field(wp_unslash($_POST['id'])) : '';
        // Initialize sanitized settings
        $efpe_settings = isset($_POST['efpe_settings']) ? $_POST['efpe_settings'] : '';

        // Sanitize efpe_settings if it exists and is an array
        // if (isset($_POST['efpe_settings']) && is_array($_POST['efpe_settings'])) {
        //     $unslashed_settings = wp_unslash($_POST['efpe_settings']);
        //     foreach ($unslashed_settings as $key => $value) {
        //         $sanitized_key = sanitize_text_field($key);
        //         if (is_array($value)) {
        //             // Recursively sanitize nested arrays
        //             $efpe_settings[$sanitized_key] = array_map('sanitize_text_field', $value);
        //         } else {
        //             $efpe_settings[$sanitized_key] = sanitize_text_field($value);
        //         }
        //     }
        // }
        $pagination_type     = isset($_POST['pagination_type']) ? sanitize_text_field(wp_unslash($_POST['pagination_type'])) : '';
        $event_per_page       = isset($_POST['post_per_page']) ? sanitize_text_field(wp_unslash($_POST['post_per_page'])) : '';
        $efpe_event_limit     = isset($_POST['efpe_post_limit']) ? sanitize_text_field(wp_unslash($_POST['efpe_post_limit'])) : '';
        $author_id           = isset($_POST['author_id']) ? sanitize_text_field(wp_unslash($_POST['author_id'])) : '';
        $keyword             = isset($_POST['keyword']) ? sanitize_text_field(wp_unslash($_POST['keyword'])) : '';
        $orderby             = isset($_POST['orderby']) ? sanitize_text_field(wp_unslash($_POST['orderby'])) : '';
        $order               = isset($_POST['order']) ? sanitize_text_field(wp_unslash($_POST['order'])) : '';
        $taxonomy            = isset($_POST['taxonomy']) ? sanitize_text_field(wp_unslash($_POST['taxonomy'])) : '';
        $term_id             = isset($_POST['term_id']) ? sanitize_text_field(wp_unslash($_POST['term_id'])) : '';
        $efpe_lang           = isset($_POST['lang']) ? sanitize_text_field(wp_unslash($_POST['lang'])) : '';
        $paged               = isset($_POST['page']) ? sanitize_text_field(wp_unslash($_POST['page'])) : '';
        $custom_fields_array = isset($_POST['custom_fields_array']) ? sanitize_text_field(wp_unslash($_POST['custom_fields_array'])) : '';
        $selected_term_list  = isset($_POST['term_list']) ? $_POST['term_list'] : '';
        $layout_preset         = isset($efpe_settings['layout_preset']) ? $efpe_settings['layout_preset'] : 'grid_layout';
        $view_options          = get_post_meta($views_id, 'efpe_view_options', true);
        // Event display settings.
        if ('ajax_number' === $pagination_type) {
            if (empty($paged)) {
                $paged = isset($_POST['page']) ? sanitize_text_field(wp_unslash($_POST['page'])) : '';
            }
        }

        $event_content_sorter              = isset($view_options['post_content_sorter']) ? $view_options['post_content_sorter'] : '';
        $query_args                       = EventfulForElementorQueryinside::get_filtered_content($view_options, $views_id, $layout_preset, $event_per_page, $efpe_event_limit, $efpe_settings);

        $event_limit                       = isset($efpe_event_limit) && !empty($efpe_event_limit) ? $efpe_event_limit : 10000;
        $event_offset                      = isset($view_options['efpe_post_offset']) ? $view_options['efpe_post_offset'] : 0;
        $new_query_args                   = $query_args;
        $new_query_args['fields']         = 'ids';
        $new_query_args['posts_per_page'] = $event_limit;
        $query_post_ids                   = get_posts($new_query_args);
        $relation                         = isset($efpe_settings['taxonomies_relation']) ? $efpe_settings['taxonomies_relation'] : 'AND';
        $query_args                       = EventfulForElementorFunctions::modify_query_params($query_args, $keyword, $author_id, $custom_fields_array, $orderby, $order, $selected_term_list, $event_offset, $relation, $query_post_ids, $efpe_lang);
        $new_query_args                   = $query_args;
        $new_query_args['fields']         = 'ids';
        $new_query_args['posts_per_page'] = $event_limit;
        $total_events                      = count(get_posts($new_query_args));
        $event_limit                       = $total_events;

        if ($event_limit > 0) {
            $event_per_page = ($event_per_page > $event_limit) ? $event_limit : $event_per_page;
            if ($event_limit < 1) {
                $total_page = 0;
            } else {
                $total_page = EventfulForElementorFunctions::efpe_max_pages($event_limit, $event_per_page);
            }
            $efpe_last_page_post   = EventfulForElementorFunctions::efpe_last_page_post($event_limit, $event_per_page, $total_page);
            $offset               = (int) $event_per_page * ($paged - 1);
            $query_args['offset'] = (int) $offset + (int) $event_offset;
            if ($total_page == $paged) {
                $query_args['posts_per_page'] = $efpe_last_page_post;
            }
            if ($paged > $total_page) {
                return false;
            }
        }
        $query_args['paged'] = $paged;
        $efpe_query           = new \WP_Query($query_args);
        EventfulForElementorLoopHtml::eventful_for_elementor_get_posts($view_options, $layout_preset, $event_content_sorter, $efpe_query, $views_id, $efpe_settings);
        die();
    }

    /**
     * Event Ajax Pagination.
     */
    public static function post_pagination_bar()
    {
        if (isset($_POST['nonce']) && !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'ta_efpe_nonce')) {
            return false;
        }
        $views_id            = isset($_POST['id']) ? absint($_POST['id']) : '';
        // Initialize sanitized settings
        $efpe_settings = isset($_POST['efpe_settings']) ? $_POST['efpe_settings'] : '';

        // Sanitize efpe_settings if it exists and is an array
        // if (isset($_POST['efpe_settings']) && is_array($_POST['efpe_settings'])) {
        //     $unslashed_settings = wp_unslash($_POST['efpe_settings']);
        //     foreach ($unslashed_settings as $key => $value) {
        //         $sanitized_key = sanitize_text_field($key);
        //         if (is_array($value)) {
        //             // Recursively sanitize nested arrays
        //             $efpe_settings[$sanitized_key] = array_map('sanitize_text_field', $value);
        //         } else {
        //             $efpe_settings[$sanitized_key] = sanitize_text_field($value);
        //         }
        //     }
        // }

        $event_per_page            = isset($_POST['post_per_page']) ? sanitize_text_field(wp_unslash($_POST['post_per_page'])) : '';
        $efpe_event_limit            = isset($_POST['efpe_post_limit']) ? sanitize_text_field(wp_unslash($_POST['efpe_post_limit'])) : '';
        $author_id           = isset($_POST['author_id']) ? sanitize_text_field(wp_unslash($_POST['author_id'])) : '';
        $keyword             = isset($_POST['keyword']) ? sanitize_text_field(wp_unslash($_POST['keyword'])) : '';
        $orderby             = isset($_POST['orderby']) ? sanitize_text_field(wp_unslash($_POST['orderby'])) : '';
        $order               = isset($_POST['order']) ? sanitize_text_field(wp_unslash($_POST['order'])) : '';
        $taxonomy            = isset($_POST['taxonomy']) ? sanitize_text_field(wp_unslash($_POST['taxonomy'])) : '';
        $term_id             = isset($_POST['term_id']) ? sanitize_text_field(wp_unslash($_POST['term_id'])) : '';
        $efpe_lang            = isset($_POST['lang']) ? sanitize_text_field(wp_unslash($_POST['lang'])) : '';
        $paged               = isset($_POST['page']) ? sanitize_text_field(wp_unslash($_POST['page'])) : '';
        $custom_fields_array = isset($_POST['custom_fields_array']) ? sanitize_text_field(wp_unslash($_POST['custom_fields_array'])) : '';
        $selected_term_list  = isset($_POST['term_list']) ? $_POST['term_list'] : '';
        $view_options        = get_post_meta($views_id, 'efpe_view_options', true);
        $layout_preset       = isset($efpe_settings['layout_preset']) ? $efpe_settings['layout_preset'] : 'grid_layout';
        $pagination_type     = isset($efpe_settings['post_pagination_type']) ? $efpe_settings['post_pagination_type'] : '';
        $pagination_type     = isset($view_options['post_pagination_type_mobile']) ? $view_options['post_pagination_type_mobile'] : '';
        $query_args          = EventfulForElementorQueryinside::get_filtered_content($view_options, $views_id, $layout_preset, $event_per_page, $efpe_event_limit, $efpe_settings);

        $event_offset                      = isset($efpe_settings['efpe_post_offset']) ? $efpe_settings['efpe_post_offset'] : 0;
        $new_query_args                   = $query_args;
        $new_query_args['fields']         = 'ids';
        $event_limit                       = isset($efpe_settings['efpe_post_limit']) && !empty($efpe_settings['efpe_post_limit']) ? $efpe_settings['efpe_post_limit'] : 10000;
        $new_query_args['posts_per_page'] = $event_limit;
        $query_post_ids                   = get_posts($new_query_args);

        $relation           = isset($efpe_settings['taxonomies_relation']) ? $efpe_settings['taxonomies_relation'] : 'AND';
        $query_args         = EventfulForElementorFunctions::modify_query_params($query_args, $keyword, $author_id, $custom_fields_array, $orderby, $order, $selected_term_list, $event_offset, $relation, $query_post_ids, $efpe_lang);
        $query_args['lang'] = '';
        $efpe_query          = new \WP_Query($query_args);

        EventfulForElementorLoopHtml::efpe_pagination_bar($efpe_query, $view_options, $layout_preset, $views_id, $efpe_settings, $event_per_page, $efpe_event_limit);
        die();
    }

    /**
     * Event Ajax mobile Pagination.
     */
    public static function post_pagination_bar_mobile()
    {
        if (isset($_POST['nonce']) && !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'ta_efpe_nonce')) {
            return false;
        }
        $views_id            = isset($_POST['id']) ? absint($_POST['id']) : '';
        // Initialize sanitized settings
        $efpe_settings = isset($_POST['efpe_settings']) ? $_POST['efpe_settings'] : '';

        // Sanitize efpe_settings if it exists and is an array
        // if (isset($_POST['efpe_settings']) && is_array($_POST['efpe_settings'])) {
        //     $unslashed_settings = wp_unslash($_POST['efpe_settings']);
        //     foreach ($unslashed_settings as $key => $value) {
        //         $sanitized_key = sanitize_text_field($key);
        //         if (is_array($value)) {
        //             // Recursively sanitize nested arrays
        //             $efpe_settings[$sanitized_key] = array_map('sanitize_text_field', $value);
        //         } else {
        //             $efpe_settings[$sanitized_key] = sanitize_text_field($value);
        //         }
        //     }
        // }
        $event_per_page            = isset($_POST['post_per_page']) ? sanitize_text_field(wp_unslash($_POST['post_per_page'])) : '';
        $efpe_event_limit            = isset($_POST['efpe_post_limit']) ? sanitize_text_field(wp_unslash($_POST['efpe_post_limit'])) : '';
        $author_id           = isset($_POST['author_id']) ? sanitize_text_field(wp_unslash($_POST['author_id'])) : '';
        $keyword             = isset($_POST['keyword']) ? sanitize_text_field(wp_unslash($_POST['keyword'])) : '';
        $orderby             = isset($_POST['orderby']) ? sanitize_text_field(wp_unslash($_POST['orderby'])) : '';
        $order               = isset($_POST['order']) ? sanitize_text_field(wp_unslash($_POST['order'])) : '';
        $taxonomy            = isset($_POST['taxonomy']) ? sanitize_text_field(wp_unslash($_POST['taxonomy'])) : '';
        $term_id             = isset($_POST['term_id']) ? sanitize_text_field(wp_unslash($_POST['term_id'])) : '';
        $efpe_lang            = isset($_POST['lang']) ? sanitize_text_field(wp_unslash($_POST['lang'])) : '';
        $paged               = isset($_POST['page']) ? sanitize_text_field(wp_unslash($_POST['page'])) : '';
        $selected_term_list  = isset($_POST['term_list']) ? $_POST['term_list'] : '';
        $custom_fields_array = isset($_POST['custom_fields_array']) ? sanitize_text_field(wp_unslash($_POST['custom_fields_array'])) : '';
        $view_options        = get_post_meta($views_id, 'efpe_view_options', true);
        $layout              = get_post_meta($views_id, 'efpe_layouts', true);
        $layout_preset       = $efpe_settings['layout_preset'];
        $pagination_type     = isset($view_options['post_pagination_type']) ? $view_options['post_pagination_type'] : '';
        $pagination_type     = isset($view_options['post_pagination_type_mobile']) ? $view_options['post_pagination_type_mobile'] : '';
        $query_args          = EventfulForElementorQueryinside::get_filtered_content($view_options, $views_id, $layout_preset, $event_per_page, $efpe_event_limit, $efpe_settings);
        $tax_settings        = array();
        $event_offset         = isset($view_options['efpe_post_offset']) ? $view_options['efpe_post_offset'] : 0;

        $new_query_args                   = $query_args;
        $new_query_args['fields']         = 'ids';
        $event_limit                       = isset($view_options['efpe_post_limit']) && !empty($view_options['efpe_post_limit']) ? $view_options['efpe_post_limit'] : 10000;
        $new_query_args['posts_per_page'] = $event_limit;
        $query_post_ids                   = get_posts($new_query_args);
        $query_post_ids                   = array('');

        $relation   = isset($efpe_settings['taxonomies_relation']) ? $efpe_settings['taxonomies_relation'] : 'AND';
        $query_args = EventfulForElementorFunctions::modify_query_params($query_args, $keyword, $author_id, $custom_fields_array, $orderby, $order, $selected_term_list, $event_offset, $relation, $query_post_ids, $efpe_lang);
        $efpe_query  = new \WP_Query($query_args);
        EventfulForElementorLoopHtml::efpe_pagination_bar($efpe_query, $view_options, $layout_preset, $views_id, $efpe_settings, $event_per_page, $efpe_event_limit);
        die();
    }

    /**
     * Event Ajax filter.
     */
    public static function eventful_for_elementor_post_order()
    {
        if (isset($_POST['nonce']) && !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'ta_efpe_nonce')) {
            return false;
        }
        $views_id               = isset($_POST['id']) ? absint($_POST['id']) : '';
        // Initialize sanitized settings
        $efpe_settings = isset($_POST['efpe_settings']) ? $_POST['efpe_settings'] : '';

        // Sanitize efpe_settings if it exists and is an array
        // if (isset($_POST['efpe_settings']) && is_array($_POST['efpe_settings'])) {
        //     $unslashed_settings = wp_unslash($_POST['efpe_settings']);
        //     foreach ($unslashed_settings as $key => $value) {
        //         $sanitized_key = sanitize_text_field($key);
        //         if (is_array($value)) {
        //             // Recursively sanitize nested arrays
        //             $efpe_settings[$sanitized_key] = array_map('sanitize_text_field', $value);
        //         } else {
        //             $efpe_settings[$sanitized_key] = sanitize_text_field($value);
        //         }
        //     }
        // }
        $event_per_page          = isset($_POST['post_per_page']) ? sanitize_text_field(wp_unslash($_POST['post_per_page'])) : '';
        $efpe_event_limit        = isset($_POST['efpe_post_limit']) ? sanitize_text_field(wp_unslash($_POST['efpe_post_limit'])) : '';
        $author_id              = isset($_POST['author_id']) ? sanitize_text_field(wp_unslash($_POST['author_id'])) : '';
        $keyword                = isset($_POST['keyword']) ? sanitize_text_field(wp_unslash($_POST['keyword'])) : '';
        $orderby                = isset($_POST['orderby']) ? sanitize_text_field(wp_unslash($_POST['orderby'])) : '';
        $order                  = isset($_POST['order']) ? sanitize_text_field(wp_unslash($_POST['order'])) : '';
        $taxonomy               = isset($_POST['taxonomy']) ? sanitize_text_field(wp_unslash($_POST['taxonomy'])) : '';
        $term_id                = isset($_POST['term_id']) ? sanitize_text_field(wp_unslash($_POST['term_id'])) : '';
        $efpe_lang              = isset($_POST['lang']) ? sanitize_text_field(wp_unslash($_POST['lang'])) : '';
        $selected_term_list     = isset($_POST['term_list']) ? $_POST['term_list'] : '';
        $custom_fields_array    = isset($_POST['custom_fields_array']) ? sanitize_text_field(wp_unslash($_POST['custom_fields_array'])) : '';
        $layout                 = get_post_meta($views_id, 'efpe_layouts', true);
        $layout_preset          = isset($efpe_settings['layout_preset']) ? $efpe_settings['layout_preset'] : 'grid_layout';
        $view_options           = get_post_meta($views_id, 'efpe_view_options', true);
        $pagination_type        = isset($efpe_settings['post_pagination_type']) ? $efpe_settings['post_pagination_type'] : '';
        $pagination_type_mobile = isset($view_options['post_pagination_type_mobile']) ? $view_options['post_pagination_type_mobile'] : '';
        $event_content_sorter    = isset($view_options['post_content_sorter']) ? $view_options['post_content_sorter'] : '';
        $query_args             = EventfulForElementorQueryinside::get_filtered_content($view_options, $views_id, $layout_preset, $event_per_page, $efpe_event_limit, $efpe_settings);
        $event_offset            = isset($efpe_settings['efpe_post_offset']) ? $efpe_settings['efpe_post_offset'] : 0;

        $new_query_args                   = $query_args;
        $new_query_args['fields']         = 'ids';
        $event_limit                       = isset($efpe_event_limit) && !empty($efpe_event_limit) ? $efpe_event_limit : 10000;
        $new_query_args['posts_per_page'] = $event_limit;
        $query_post_ids                   = get_posts($new_query_args);
        $relation                         = isset($efpe_settings['taxonomies_relation']) ? $efpe_settings['taxonomies_relation'] : 'AND';
        $query_args                       = EventfulForElementorFunctions::modify_query_params($query_args, $keyword, $author_id, $custom_fields_array, $orderby, $order, $selected_term_list, $event_offset, $relation, $query_post_ids, $efpe_lang);
        $efpe_query                        = new \WP_Query($query_args);

        EventfulForElementorLoopHtml::eventful_for_elementor_get_posts($view_options, $layout_preset, $event_content_sorter, $efpe_query, $views_id, $efpe_settings);
        die();
    }
}
