<?php
if (!defined('ABSPATH')) {
    exit;
}


if (!function_exists('united_blog_customizer_read_more_icons')) :
    /**
     * Returns read more icons options as SVG strings.
     *
     * @return array Options array.
     * @since 1.0.0
     */
    function united_blog_customizer_read_more_icons()
    {
        $icons = array(
            'arrow-right' => united_blog_the_theme_svg('arrow-right',$return = true),
            'arrow-left' => united_blog_the_theme_svg('arrow-left',$return = true),
            'chevron-right' => united_blog_the_theme_svg('chevron-right',$return = true),
            'chevron-left' => united_blog_the_theme_svg('chevron-left',$return = true),
        );

        return $icons;
    }
endif;

class United_Blog_Posts_Slider extends United_Blog_Widget_Base
{
    /**
     * Constructor.
     */
    public function __construct()
    {
        $this->widget_cssclass = 'widget_united_blog_posts_slider';
        $this->widget_description = __('Displays posts in slider', 'united-blog');
        $this->widget_id = 'united_blog_posts_slider';
        $this->widget_name = __('United Blog: Posts Slider', 'united-blog');
        $this->settings = array(
            'title' => array(
                'type' => 'text',
                'label' => __('Title', 'united-blog'),
            ),
            'post_settings_heading' => array(
                'type' => 'heading',
                'label' => __('Post Settings', 'united-blog'),
            ),
            'category' => array(
                'type' => 'dropdown-taxonomies',
                'label' => __('Select Category', 'united-blog'),
                'desc' => __('Leave empty if you don\'t want the posts to be category specific', 'united-blog'),
                'args' => array(
                    'taxonomy' => 'category',
                    'class' => 'widefat',
                    'hierarchical' => true,
                    'show_count' => 1,
                    'show_option_all' => __('&mdash; Select &mdash;', 'united-blog'),
                ),
            ),
            'number' => array(
                'type' => 'number',
                'step' => 1,
                'min' => 1,
                'max' => '',
                'std' => 5,
                'label' => __('Number of posts to show', 'united-blog'),
            ),
            'offset' => array(
                'type' => 'number',
                'step' => 1,
                'min' => 0,
                'max' => '',
                'std' => '',
                'label' => __('Offset', 'united-blog'),
                'desc' => __('Can be useful if you want to skip certain number of posts. Leave as 0 if you do not want to use it.', 'united-blog'),
            ),
            'orderby' => array(
                'type' => 'select',
                'std' => 'date',
                'label' => __('Order by', 'united-blog'),
                'options' => array(
                    'date' => __('Date', 'united-blog'),
                    'ID' => __('ID', 'united-blog'),
                    'title' => __('Title', 'united-blog'),
                    'rand' => __('Random', 'united-blog'),
                ),
            ),
            'order' => array(
                'type' => 'select',
                'std' => 'desc',
                'label' => __('Order', 'united-blog'),
                'options' => array(
                    'asc' => __('ASC', 'united-blog'),
                    'desc' => __('DESC', 'united-blog'),
                ),
            ),
            'meta_settings_heading' => array(
                'type' => 'heading',
                'label' => __('Post Meta Settings', 'united-blog'),
            ),
            'post_meta' => array(
                'type' => 'multi-checkbox',
                'label' => __('Post Meta', 'united-blog'),
                'options' => array(
                    'author' => __('Author', 'united-blog'),
                    'date' => __('Date', 'united-blog'),
                    'comment' => __('Comment', 'united-blog'),
                ),
                'std' => array('date'),
            ),
            'date_format' => array(
                'type' => 'select',
                'label' => __('Date Format', 'united-blog'),
                'desc' => __('Make sure to select Date from above for this to work.', 'united-blog'),
                'options' => array(
                    'format_1' => __('Times Ago', 'united-blog'),
                    'format_2' => __('Default Format', 'united-blog'),
                ),
                'std' => 'format_1',
            ),
            'author_image' => array(
                'type' => 'checkbox',
                'label' => __('Show Author Image', 'united-blog'),
                'desc' => __('Make sure to select Author from above for this to work.', 'united-blog'),
                'std' => false,
            ),
            'excerpt_settings_heading' => array(
                'type' => 'heading',
                'label' => __('Excerpt Settings', 'united-blog'),
            ),
            'show_excerpt' => array(
                'type' => 'checkbox',
                'label' => __('Show Excerpt', 'united-blog'),
                'std' => false,
            ),
            'excerpt_length' => array(
                'type' => 'number',
                'step' => 1,
                'min' => 0,
                'max' => '',
                'std' => 20,
                'label' => __('Excerpt Length', 'united-blog'),
            ),
            'show_read_more' => array(
                'type' => 'checkbox',
                'label' => __('Show Read More', 'united-blog'),
                'std' => false,
            ),
            'read_more_text' => array(
                'type' => 'text',
                'label' => __('Read More Text', 'united-blog'),
                'desc' => __('Leave Empty if you want to use default text "Continue Reading" ', 'united-blog'),
            ),
            'read_more_icon' => array(
                'type' => 'select',
                'label' => __('Read More Icon', 'united-blog'),
                'options' => united_blog_widget_read_more_icons(),
                'std' => 'arrow-right',
            ),
            'category_settings_heading' => array(
                'type' => 'heading',
                'label' => __('Category Settings', 'united-blog'),
            ),
            'show_category' => array(
                'type' => 'checkbox',
                'label' => __('Show Category', 'united-blog'),
                'std' => true,
            ),
            'category_color' => array(
                'type' => 'select',
                'label' => __('Categories Display Style', 'united-blog'),
                'options' => united_blog_get_category_display_styles(),
                'std' => 'bg_color',
            ),
            'no_of_category' => array(
                'type' => 'number',
                'step' => 1,
                'min' => 0,
                'max' => '',
                'std' => 1,
                'label' => __('Number of Categories to Display', 'united-blog'),
            ),
            'widget_settings_heading' => array(
                'type' => 'heading',
                'label' => __('Widget Settings', 'united-blog'),
            ),
            'style' => array(
                'type' => 'select',
                'label' => __('Display Style', 'united-blog'),
                'options' => array(
                    'style_1' => __('Style 1', 'united-blog'),
                    'style_2' => __('Style 2', 'united-blog'),
                ),
                'std' => 'style_1',
            ),
            'autoplay' => array(
                'type' => 'checkbox',
                'label' => __('Autoplay', 'united-blog'),
                'std' => false,
            ),
            'arrows' => array(
                'type' => 'checkbox',
                'label' => __('Enable Arrows', 'united-blog'),
                'std' => true,
            ),
            'dots' => array(
                'type' => 'checkbox',
                'label' => __('Enable Dots', 'united-blog'),
                'std' => false,
            ),
            'enable_post_format_icon' => array(
                'type' => 'checkbox',
                'label' => __('Display Post Format Icon', 'united-blog'),
                'std' => true,
            ),
            'title_size' => array(
                'type' => 'select',
                'label' => __('Title Size', 'united-blog'),
                'options' => array(
                    'entry-title-small' => __('Small Size', 'united-blog'),
                    'entry-title-medium' => __('Medium Size', 'united-blog'),
                    'entry-title-big' => __('Big Size', 'united-blog'),
                    'entry-title-large' => __('Large Size', 'united-blog'),
                ),
                'std' => 'entry-title-medium',
            ),
            'image_size' => array(
                'type' => 'select',
                'label' => __('Image size', 'united-blog'),
                'options' => array(
                    'medium' => __('Medium', 'united-blog'),
                    'medium_large' => __('Medium Large', 'united-blog'),
                    'large' => __('Large', 'united-blog'),
                    'full' => __('Full', 'united-blog'),
                ),
                'std' => 'large',
            ),
            'height' => array(
                'type' => 'number',
                'step' => 1,
                'min' => 150,
                'max' => '',
                'std' => 500,
                'label' => __('Height (px)', 'united-blog'),
                'desc' => __('Change Height of Slides.', 'united-blog'),
            ),
            'bg_overlay_color' => array(
                'type' => 'color',
                'label' => __('Overlay Color', 'united-blog'),
                'std' => '#222',
            ),
            'overlay_opacity' => array(
                'type' => 'number',
                'step' => 10,
                'min' => 0,
                'max' => 100,
                'std' => 50,
                'label' => __('Overlay Opacity', 'united-blog'),
            ),
            'title_limit' => array(
                'type' => 'select',
                'label' => __('Post Title Limit', 'united-blog'),
                'options' => united_blog_get_title_line_clamp(),
                'std' => '',
            ),
        );
        parent::__construct();
    }
    /**
     * Query the posts and return them.
     *
     * @param array $args
     * @param array $instance
     * @return WP_Query
     */
    public function get_posts($args, $instance)
    {
        $number = !empty($instance['number']) ? absint($instance['number']) : $this->settings['number']['std'];
        $orderby = !empty($instance['orderby']) ? esc_attr($instance['orderby']) : $this->settings['orderby']['std'];
        $order = !empty($instance['order']) ? esc_attr($instance['order']) : $this->settings['order']['std'];
        $offset = !empty($instance['offset']) ? sanitize_text_field($instance['offset']) : $this->settings['offset']['std'];
        $query_args = array(
            'posts_per_page' => $number,
            'post_status' => 'publish',
            'no_found_rows' => 1,
            'orderby' => $orderby,
            'order' => $order,
            'ignore_sticky_posts' => 1,
        );
        if ($offset && 0 != $offset) {
            $query_args['offset'] = absint($offset);
        }
        if (!empty($instance['category']) && -1 != $instance['category'] && 0 != $instance['category']) {
            $query_args['tax_query'][] = array(
                'taxonomy' => 'category',
                'field' => 'term_id',
                'terms' => absint($instance['category']),
            );
        }
        return new WP_Query(apply_filters('united_blog_posts_slider_query_args', $query_args));
    }
    /**
     * Output widget.
     *
     * @param array $args
     * @param array $instance
     * @see WP_Widget
     *
     */
    public function widget($args, $instance)
    {
        ob_start();
        if (($posts = $this->get_posts($args, $instance)) && $posts->have_posts()) {
            $this->widget_start($args, $instance);
            do_action('united_blog_before_posts_slider');
            $slider_post_class = '';
            $slider_nav = '';

            $data_slider = array();
            $autoplay = isset($instance['autoplay']) ? $instance['autoplay'] : $this->settings['autoplay']['std'];
            if ($autoplay) :
                $data_slider['autoplay'] = array(
                    'delay' => 5000,
                );
            endif;
            $dots = isset($instance['dots']) ? $instance['dots'] : $this->settings['dots']['std'];
            if ($dots) {
                $slider_nav .= '<div class="swiper-pagination"></div>';
            }
            $arrows = isset($instance['arrows']) ? $instance['arrows'] : $this->settings['arrows']['std'];
            if ($arrows) {
                $slider_nav .= '<div class="swiper-button-next"></div><div class="swiper-button-prev"></div>';
            }
            $show_category = isset($instance['show_category']) ? $instance['show_category'] : $this->settings['show_category']['std'];
            if ($show_category) {
                $color = isset($instance['category_color']) ? $instance['category_color'] : $this->settings['category_color']['std'];
                $limit = isset($instance['no_of_category']) ? $instance['no_of_category'] : $this->settings['no_of_category']['std'];
            }
            $enabled_post_meta = isset($instance['post_meta']) ? $instance['post_meta'] : $this->settings['post_meta']['std'];
            $meta_settings['date_format'] = isset($instance['date_format']) ? $instance['date_format'] : $this->settings['date_format']['std'];
            $meta_settings['author_image'] = isset($instance['author_image']) ? $instance['author_image'] : $this->settings['author_image']['std'];
            $title_size = !empty($instance['title_size']) ? $instance['title_size'] : $this->settings['title_size']['std'];
            $image_size = !empty($instance['image_size']) ? $instance['image_size'] : $this->settings['image_size']['std'];
            $height = isset($instance['height']) ? $instance['height'] : $this->settings['height']['std'];
            $height = '--united-article-slides-height:' . esc_attr($height) . 'px;';
            $title_limit = isset($instance['title_limit']) ? $instance['title_limit'] : $this->settings['title_limit']['std'];
            $enable_post_format_icon = isset($instance['enable_post_format_icon']) ? $instance['enable_post_format_icon'] : $this->settings['enable_post_format_icon']['std'];
            $show_excerpt = isset($instance['show_excerpt']) ? $instance['show_excerpt'] : $this->settings['show_excerpt']['std'];
            if ($show_excerpt) {
                $excerpt_length = isset($instance['excerpt_length']) ? $instance['excerpt_length'] : $this->settings['excerpt_length']['std'];
            }
            $show_read_more = isset($instance['show_read_more']) ? $instance['show_read_more'] : $this->settings['show_read_more']['std'];
            if ($show_read_more) {
                $read_more_text = isset($instance['read_more_text']) ? $instance['read_more_text'] : '';
                $read_more_icon = isset($instance['read_more_icon']) ? $instance['read_more_icon'] : $this->settings['read_more_icon']['std'];
            }
            if ($enable_post_format_icon) {
                $slider_post_class .= ' has-format-icon';
            }

            $bg_overlay_color = isset($instance['bg_overlay_color']) ? $instance['bg_overlay_color'] : $this->settings['bg_overlay_color']['std'];
            $overlay_opacity = isset($instance['overlay_opacity']) ? $instance['overlay_opacity'] : $this->settings['overlay_opacity']['std'];


            $style = 'background-color:' . $bg_overlay_color . ';';
            $style .= 'opacity:' . ($overlay_opacity / 100) . ';';

            ?>
            <div class="united-swiper-js as_slider">
                <div class="swiper" data-slider='<?php echo esc_attr(json_encode($data_slider)); ?>'>
                    <div class="swiper-wrapper">
                        <?php
                        while ($posts->have_posts()) :
                            $posts->the_post();
                            if (has_post_thumbnail()) {
                                ?>
                                <div class="swiper-slide">
                                    <article id="slider-post-<?php the_ID(); ?>" <?php post_class('united-article united-article-slides' . esc_attr($slider_post_class)); ?> style="<?php echo esc_attr($height); ?>">
                                        <?php if (has_post_thumbnail()) : ?>
                                            <div class="entry-image entry-image-big">
                                                <a class="post-thumbnail" href="<?php the_permalink(); ?>"
                                                   aria-hidden="true" tabindex="-1">
                                                    <span aria-hidden="true" class="united-article-overlay" style="<?php echo esc_attr($style); ?>"></span>
                                                    <?php
                                                    the_post_thumbnail(
                                                        $image_size,
                                                        array(
                                                            'alt' => the_title_attribute(
                                                                array(
                                                                    'echo' => false,
                                                                )
                                                            ),
                                                        )
                                                    );
                                                    ?>
                                                </a>
                                            </div>
                                        <?php endif; ?>
                                        <div class="entry-details">
                                            <?php
                                            if ($show_category) {
                                                echo '<div class="article-cat-info">';
                                                united_blog_post_categories($color, $limit);
                                                echo '</div>';
                                            }
                                            ?>
                                            <header class="entry-header">
                                                <h3 class="entry-title <?php echo esc_attr($title_size); ?> has-line-clamp <?php echo esc_attr($title_limit); ?>">
                                                    <a href="<?php echo esc_url(get_permalink()); ?>" rel="bookmark">
                                                        <?php the_title(); ?>
                                                    </a>
                                                </h3>
                                            </header>
                                            <?php united_blog_post_meta_info($enabled_post_meta, $meta_settings); ?>
                                            <?php
                                            if ($show_excerpt && $excerpt_length > 0) {
                                                ?>
                                                <div class="article-excerpt hide-on-mobile">
                                                    <?php echo wp_trim_words(get_the_excerpt(), $excerpt_length, '&hellip;'); ?>
                                                </div>
                                            <?php } ?>
                                            <?php
                                            if ($show_read_more) {
                                                ?>
                                                <div class="united-read-more">
                                                    <a href="<?php the_permalink(); ?>" class="united-button united-button-transparent">
                                                        <?php
                                                        if ($read_more_text) {
                                                            echo esc_html($read_more_text);
                                                        } else {
                                                            esc_html_e('Continue Reading', 'united-blog');
                                                        }
                                                        if ($read_more_icon) {
                                                            ?>
                                                            <span><?php united_blog_the_theme_svg($read_more_icon); ?></span>
                                                            <?php
                                                        }
                                                        ?>
                                                    </a>
                                                </div>
                                                <?php
                                            }
                                            ?>
                                        </div>
                                    </article>
                                </div>
                                <?php
                            }
                        endwhile;
                        wp_reset_postdata();
                        ?>
                    </div>
                </div>
                <?php
                if ($slider_nav) :
                    echo $slider_nav;
                endif;
                ?>
            </div>
            <?php
            do_action('united_blog_after_posts_slider');
            $this->widget_end($args);
        }
        echo ob_get_clean();
    }

    public function enqueue_assets()
    {
        if (is_active_widget(false, false, $this->id_base)) {
            $file_prefix = is_rtl() ? '-rtl' : '';
            $css_file = get_template_directory() . '/inc/widgets/css/slider' . $file_prefix . '.css';
            if (file_exists($css_file)) {
                $styles = wp_strip_all_tags(file_get_contents($css_file));
                wp_add_inline_style('united-blog-style', $styles);
            }
        }
    }
}
