<?php
/**
 * Showcases shortcode for Revio
 *
 * @since 1.0.0
 * @package RVIOR
 */

// Namespace.
namespace RVIOR;

// Exit if accessed directly.
defined('ABSPATH') || exit(1);

if ( ! class_exists(__NAMESPACE__ . '\Reviews') ) {

    /**
     * Showcases shortcode for Revio
     *
     * @since 1.3.8
     */
    class Reviews extends \RVIOR\Base\Controller {


        // Use Utilities trait.
        use \RVIOR\Traits\Utilities;

        /**
         * Register the hooks
         *
         * @return void
         */
        public function register_hooks() {
            /**
             * 'reviews' shortcode
             *
             * @since 1.4.0
             */
            add_shortcode('reviews', [ $this, 'render_shortcode_template' ]);
        }

        /**
         * Render the reviews shortcode
         *
         * @param array $atts shortcode attributes.
         * @return string
         */
        public function render_shortcode_template( $atts = [] ) {
            if ( class_exists('WooCommerce') ) {
                $post_types = 'product';
                if ( is_singular($post_types) ) {
                    $woocommerce_gallery = $this->option()->get('woocommerce_gallery_settings');
                }
            } else {
                $woocommerce_gallery = new \stdClass();
            }

            $gallery_config = [];

            if ( isset($atts['id']) ) {
                // selected gallery id
                $gallery_id = $atts['id'];

                // get created gallery from database
                $galleries = $this->option()->get('gallaries');

                // get selected gallery by gallery id
                $get_selected_gallery = array_filter($galleries, function ( $gallery ) use ( $gallery_id ) {
                    return (string) $gallery['id'] === (string) $gallery_id;
                });

                if ( ! empty($get_selected_gallery) ) {
                    $gallery_config = array_values($get_selected_gallery)[0];
                } else {
                    return 'No gallery found';
                }
            }

            $default_args = [
                // internal.
                'view' => class_exists('WooCommerce') && isset($woocommerce_gallery['displaystyle']) ? sanitize_text_field(wp_unslash($woocommerce_gallery['displaystyle'])) : ( isset($gallery_config['displaystyle']) ? sanitize_text_field(wp_unslash($gallery_config['displaystyle'])) : 'grid' ),
                'reviewsids' => class_exists('WooCommerce') && isset($woocommerce_gallery['displaystyle']) ? '' : ( isset($gallery_config['reviews']) ? sanitize_text_field(wp_unslash(implode(',', $gallery_config['reviews']))) : '' ),
                'gallery_style' => class_exists('WooCommerce') && isset($woocommerce_gallery['style']) ? sanitize_text_field(wp_unslash($woocommerce_gallery['style'])) : ( isset($gallery_config['style']) ? sanitize_text_field(wp_unslash($gallery_config['style'])) : '1' ),
                'date' => class_exists('WooCommerce') && isset($woocommerce_gallery['showdate']) ? sanitize_text_field(wp_unslash($woocommerce_gallery['showdate'])) : ( isset($gallery_config['showdate']) ? sanitize_text_field(wp_unslash($gallery_config['showdate'])) : false ),
                'rounded' => false,
                'shares' => false,
                'grid_pagination' => class_exists('WooCommerce') && isset($woocommerce_gallery['grid']['pagination']) ? sanitize_text_field(wp_unslash($woocommerce_gallery['grid']['pagination'])) : ( isset($gallery_config['grid']) ? sanitize_text_field(wp_unslash($gallery_config['grid']['pagination'])) : '0' ),
                'additional_class_css' => class_exists('WooCommerce') && isset($woocommerce_gallery['additionalCssClass']) ? sanitize_text_field(wp_unslash($woocommerce_gallery['additionalCssClass'])) : ( isset($gallery_config['additionalCssClass']) ? sanitize_text_field(wp_unslash($gallery_config['additionalCssClass'])) : '' ),
                'player_control' => class_exists('WooCommerce') && isset($woocommerce_gallery['playercontrol']) ? sanitize_text_field(wp_unslash($woocommerce_gallery['playercontrol'])) : ( isset($gallery_config['playercontrol']) ? sanitize_text_field(wp_unslash($gallery_config['playercontrol'])) : '1' ),

                // slider.
                'autoplay' => false,
                'autoplay_speed' => 3000,
                'dots' => false,
                'arrows' => false,
                'infinite' => false,
                'slides_to_show' => 3,
                'slides_to_scroll' => 1,
                'auto_scroll_duration' => '2500',
                'draggable_scrolling' => '0',
                'auto_scrolling' => '0',

                // sorting.
                'limit' => class_exists('WooCommerce') && isset($woocommerce_gallery['grid']) ? sanitize_text_field(wp_unslash($woocommerce_gallery['grid']['videoperpage'])) : ( isset($gallery_config['grid']) ? sanitize_text_field(wp_unslash($gallery_config['grid']['videoperpage'])) : 10 ),
                'offset' => 0,
                'order' => 'DESC',
                'orderby' => 'id',
                'pagination' => class_exists('WooCommerce') && isset($woocommerce_galler['carousal']['pagination']) ? sanitize_text_field(wp_unslash($woocommerce_galler['carousal']['pagination'])) : ( isset($gallery_config['carousal']) ? sanitize_text_field(wp_unslash($gallery_config['carousal']['pagination'])) : '0' ),
                'navigation' => class_exists('WooCommerce') && isset($woocommerce_galler['carousal']['navigation']) ? sanitize_text_field(wp_unslash($woocommerce_galler['carousal']['navigation'])) : ( isset($gallery_config['carousal']) ? sanitize_text_field(wp_unslash($gallery_config['carousal']['navigation'])) : '0' ),
                'scrollbar' => class_exists('WooCommerce') && isset($woocommerce_galler['carousal']['scrollbar']) ? sanitize_text_field(wp_unslash($woocommerce_galler['carousal']['scrollbar'])) : ( isset($gallery_config['carousal']) ? sanitize_text_field(wp_unslash($gallery_config['carousal']['scrollbar'])) : '0' ),

                // gird.
                'columns' => class_exists('WooCommerce') && isset($woocommerce_gallery['columns']) ? sanitize_text_field(wp_unslash($woocommerce_gallery['columns'])) : ( isset($gallery_config['columns']) ? sanitize_text_field(wp_unslash($gallery_config['columns'])) : '3' ),
                'columns_tablet' => class_exists('WooCommerce') && isset($woocommerce_gallery['columns_tablet']) ? sanitize_text_field(wp_unslash($woocommerce_gallery['columns_tablet'])) : ( isset($gallery_config['columns_tablet']) ? sanitize_text_field(wp_unslash($gallery_config['columns_tablet'])) : '2' ),
                'columns_mobile' => class_exists('WooCommerce') && isset($woocommerce_gallery['columns_mobile']) ? sanitize_text_field(wp_unslash($woocommerce_gallery['columns_mobile'])) : ( isset($gallery_config['columns_mobile']) ? sanitize_text_field(wp_unslash($gallery_config['columns_mobile'])) : '1' ),
                'gap' => 10,
                'gap_tablet' => 10,
                'gap_mobile' => 10,

                // filters.
                'folder' => '',
                'folder_not' => '',
                'playlist' => '',
                'playlist_not' => '',
                'id' => '',
                'id_not' => '',
                'slug' => '',
                'slug_not' => '',
                'tags' => '',
                'tags_not' => '',
            ];

            $args = shortcode_atts($default_args, $atts);

            $configuration = wp_json_encode($args);

            $showcase = '';

            $showcase = wp_sprintf('<div class="rvior-reviews rvior-reviews-showcase relative md:gap-3 lg:gap-4" data-rvior-reviews="%s"><div id="rvior-showcase-grid" class="rvior-showcase-galleries"></div></div>', esc_attr($configuration));

            return wp_kses_post($showcase);
        }
    }

    // Instantiate the class.
    Reviews::init();

}
