<?php
    /**
     * Post list and grid widget
     * 
     * @see WP_Widget
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Widgets;

    use WP_Widget;
    use BBE\Widgets\Base as Base;
    use BBE\Query_Manager as Query_Manager;
    use BBE\Customizer\Section\Pagination as Pagination;

    if( ! class_exists( __NAMESPACE__ . '\\Post_List_And_Grid' ) ) {
        /**
         * Post_List_And_Grid class
         */
        class Post_List_And_Grid extends Base {
            /**
             * Query vars
             * 
             * @since 1.0.0
             */
            private $query_vars = [];

            /**
             * Constructor
             */
            public function __construct() {
                parent::__construct(
                    'blog_bee_post_list_widget',
                    esc_html__( 'Blog Bee: Post List & Grid', 'blog-bee' ),
                    [ 'description' =>  __( 'Display posts in a list or grid.', 'blog-bee' ) ]
                );
            }

            /**
             * 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 ) {
                $query_attrs = $this->get_query_attrs( $instance );
                $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' ] : '';
                $layout = isset( $instance[ 'layout' ] ) ? $instance[ 'layout' ] : 'list';
                $variant = isset( $instance[ 'variant' ] ) ? $instance[ 'variant' ] : 'one';
                $widget_class[] = 'post-list-widget';
                $widget_class[] = 'bbe-sidebar-widget';
                $widget_class[] = 'layout--' . $layout;
                $widget_class[] = 'variant--' . $variant;

                $data_attrs_args = [
                    'data-widget="' . 'post-' . esc_attr( $layout ) . '"',
                    'data-query="' . esc_attr( json_encode( $query_attrs ) ) . '"'
                ];

                echo wp_kses_post( $before_widget );

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

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

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

                            while( $query_instance->have_posts() ) : $query_instance->the_post();

                                self::get_content();

                            endwhile;

                        echo '</div>';

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

                remove_filter( 'blog_bee_query_attrs_filter', [ $this, 'filter' ] );
            }

            /**
             * Render article tag
             * 
             * @since 1.0.0
             */
            public static function get_content() {

                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();
            }

            /**
             * 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__( 'Post List', 'blog-bee' )
                    ],
                    [
                        'name' => 'layout',
                        'type' => 'radio',
                        'title' => esc_html__( 'Layouts', 'blog-bee' ),
                        'default'   => 'list',
                        'choices' =>    [
                            'list'   => esc_html__( 'List', 'blog-bee' ),
                            'grid'   => esc_html__( 'Grid', 'blog-bee' ),
                            'grid-two-column'   => esc_html__( 'Grid - Two Column', 'blog-bee' )
                        ]
                    ],
                    [
                        'name' => 'query_settings',
                        'type' => 'heading',
                        'title' => esc_html__( 'Post Query Settings', 'blog-bee' ),
                    ],
                    [
                        'name' => 'number_of_posts',
                        'type' => 'number',
                        'title' => esc_html__( 'Number of posts to show', 'blog-bee' ),
                        'default'   => 4,
                        'input_attrs' => [
                            'min' => 1,
                            'max' => 10,
                            'step'  => 1
                        ]
                    ],
                    [
                        'name' => 'post_source',
                        'type' => 'radio',
                        'title' => esc_html__( 'Posts Source', 'blog-bee' ),
                        'description' => esc_html__( 'Posts will be displayed according to the selected option', 'blog-bee' ),
                        'default'   =>  'posts_to_include',
                        'choices'   =>  [
                            'posts_to_include'  =>  esc_html__( 'Posts to Include', 'blog-bee' ),
                            'posts_categories'  =>  esc_html__( 'Post Categories', 'blog-bee' ),
                        ]
                    ],
                    [
                        'name' => 'posts_to_include',
                        'type' => 'multiselect',
                        'title' => esc_html__( 'Posts to Include', 'blog-bee' ),
                        'default'   =>  [],
                        'fetch' =>  'post'
                    ],
                    [
                        'name' => 'posts_categories',
                        'type' => 'multiselect',
                        'title' => esc_html__( 'Post Categories', 'blog-bee' ),
                        'default'   =>  [],
                        'fetch' =>  'category'
                    ],
                ];
            }
            
            /**
             * Get wp_query attrs
             * 
             * @param array $instance The settings for the particular instance of the widget.
             * @since 1.0.0
             */
            private function get_query_attrs( $instance ) {
                $number_of_posts = isset( $instance[ 'number_of_posts' ] ) ? absint( $instance[ 'number_of_posts' ] ) : 4;
                $this->query_vars = [
                    'posts_per_page'    =>  $number_of_posts
                ];
                $post_source = isset( $instance[ 'post_source' ] ) ? $instance[ 'post_source' ] : 'posts_to_include';
                if( $post_source === 'posts_to_include' ) {
                    if( isset( $instance[ 'posts_to_include' ] ) && is_array( $instance[ 'posts_to_include' ] ) ) {
                        $this->query_vars[ 'post__in' ] = $instance[ 'posts_to_include' ];
                    }
                } else {
                    $posts_categories = isset( $instance[ 'posts_categories' ] ) ? $instance[ 'posts_categories' ] : [];
                    if( isset( $instance[ 'posts_categories' ] ) && is_array( $instance[ 'posts_categories' ] ) ) {
                        $this->query_vars[ 'category__in' ] = $instance[ 'posts_categories' ];
                    }
                }
                add_filter( 'blog_bee_query_attrs_filter', [ $this, 'filter' ] );
                return $this->query_vars;
            }

            /**
             * Filter
             * 
             * @since 1.0.0
             */
            public function filter( $query_vars ) {
                return array_merge( $query_vars, $this->query_vars );
            }
        }
        Base::set_widget( Post_List_And_Grid::class );
    }