<?php
    /**
     * Carousel posts widget
     * 
     * @see WP_Widget
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Widgets;

    use WP_Widget;
    use BBE\Helpers as Helpers;
    use BBE\Widgets\Base as Base;
    use BBE\Query_Manager as Query_Manager;
    use BBE\Customizer\Section\Carousel as Carousel;
    
    use function esc_html__;
    use function wp_kses_post;
    use function esc_attr;
    use function esc_html;
    use function get_users;
    use function get_user;
    use function is_customize_preview;

    if( ! class_exists( __NAMESPACE__ . '\\Carousel_Posts' ) ) {
        /**
         * Carousel_Posts class
         */
        class Carousel_Posts extends Base {
            /**
             * Widget categories
             * 
             * @since 1.0.0
             */
            private $widget_categories = [];

            /**
             * Constructor
             */
            public function __construct() {
                parent::__construct(
                    'blog_bee_carousel_posts_widget',
                    esc_html__( 'Blog Bee: Carousel Posts', 'blog-bee' ),
                    [ 'description' =>  __( 'Display posts in a carousel.', 'blog-bee' ) ]
                );
            }

            /**
             * Filter
             * 
             * @since 1.0.0
             */
            public function filter( $classes ) {
                $classes[] = 'is-widget';
                return $classes;
            }

            /**
             * Filter
             * 
             * @since 1.0.0
             */
            public function category_filter() {
                return $this->widget_categories;
            }

            /**
             * Filter
             * 
             * @since 1.0.0
             */
            public function post_source_filter() {
                return 'categories';
            }

            /**
             * Echoes the widget content.
             *
             * @param array $args     Display arguments including 'before_title', 'after_title',
             *                        'before_widget', and 'after_widget'.
             * @param array $instance The settings for the particular instance of the widget.
             */
            public function widget( $args, $instance ) {
                $this->widget_categories = isset( $instance[ 'categories' ] ) ? $instance[ 'categories' ] : [];
                add_filter( 'blog_bee_carousel_classes_filter', [ $this, 'filter' ] );
                add_filter( 'blog_bee_carousel_cat_array_filter', [ $this, 'category_filter' ] );
                add_filter( 'blog_bee_carousel_post_source_filter', [ $this, 'post_source_filter' ] );
                $query_attrs = [
                    'category__in'   =>  $this->widget_categories
                ];
                $query_instance = Query_Manager::fine_tune_query_attrs( $query_attrs, true );
                if( ! $query_instance->have_posts() ) return;

                extract( $args );
                $title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : esc_html__( 'Carousel', 'blog-bee' );
                $autoplay = isset( $instance[ 'autoplay' ] ) ? $instance[ 'autoplay' ] : true;
                $arrows = isset( $instance[ 'arrows' ] ) ? $instance[ 'arrows' ] : true;
                $infinite = isset( $instance[ 'infinite' ] ) ? $instance[ 'infinite' ] : true;
                $direction = isset( $instance[ 'direction' ] ) ? $instance[ 'direction' ] : 'horizontal';

                $widget_class[] = 'carousel-widget';
                $widget_class[] = 'bbe-sidebar-widget';
                $data_attrs = [
                    'data-arrows="' . esc_attr( $arrows ) . '"',
                    'data-autoplay="' . esc_attr( $autoplay ) . '"',
                    'data-infinite="' . esc_attr( $infinite ) . '"',
                    'data-direction="' . esc_attr( $direction ) . '"',
                ];

                $frontpage_widget_areas = [ 'bbe-frontpage-widget-area-one', 'bbe-frontpage-widget-area-two' ];
                if( isset( $args[ 'id' ] ) ) {
                    $sidebar_id = $id;
                    $in_frontpage = in_array( $sidebar_id, $frontpage_widget_areas );
                } else {
                    $in_frontpage = false;
                }

                if( ! $in_frontpage ) $widget_class[] = 'not-frontpage';

                echo wp_kses_post( $before_widget );

                    echo '<div class="' . esc_attr( implode( ' ', $widget_class ) ) . '" ' . implode( ' ', $data_attrs ) . '>';

                        if( $title ) echo wp_kses_post( $before_title ) . esc_html( $title ) . wp_kses_post( $after_title );

                        if( $in_frontpage ) :

                            Carousel::get_instance()->render_html();

                        else:

                            echo '<div class="article-wrap">';

                                while( $query_instance->have_posts() ) :

                                    $query_instance->the_post();

                                    Query_Manager::get_opening_article_tag();

                                        Query_Manager::get_opening_figure_tag();

                                            Query_Manager::get_featured_image( 'bbe_post_card' );

                                        Query_Manager::get_closing_figure_tag();

                                        echo '<div class="entry-content">';

                                            Query_Manager::get_title();

                                            Query_Manager::get_date();

                                        echo '</div>';

                                    Query_Manager::get_closing_article_tag();

                                endwhile;
                                
                            echo '</div>';

                            Helpers::get_slick_arrows( true, true );

                        endif;

                    echo '</div><!-- .carousel-widget -->';
                    
                echo wp_kses_post( $after_widget );

                remove_filter( 'blog_bee_carousel_classes_filter', [ $this, 'filter' ] );
                remove_filter( 'blog_bee_carousel_cat_array_filter', [ $this, 'category_filter' ] );
                remove_filter( 'blog_bee_carousel_post_source_filter', [ $this, 'post_source_filter' ] );
            }

            /**
             * Define widget fields
             * 
             * @since 1.0.0
             */
            public static function widget_fields(): array {
                return [
                    [
                        'name' => 'title',
                        'type' => 'text',
                        'title' => esc_html__( 'Widget Title', 'blog-bee' ),
                        'default'   =>  esc_html__( 'Carousel', 'blog-bee' )
                    ],
                    [
                        'name' => 'categories',
                        'type' => 'multiselect',
                        'title' => esc_html__( 'Categories', 'blog-bee' ),
                        'description' => esc_html__( 'Choose the categories to display in carousel.', 'blog-bee' ),
                        'default'   => [],
                        'fetch' =>  'category'
                    ],
                ];
            }
        }
        Base::set_widget( Carousel_Posts::class );
    }