<?php
    /**
     * Category collection 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\Category_Collection as Section_Category_Collection;
    
    use function esc_html__;
    use function wp_kses_post;
    use function esc_attr;
    use function esc_html;
    use function get_terms;
    use function wp_get_attachment_image;
    use function get_category_link;
    use function esc_url;

    if( ! class_exists( __NAMESPACE__ . '\\Category_Collection' ) ) {
        /**
         * Category_Collection class
         */
        class Category_Collection extends Base {
            /**
             * Constructor
             */
            public function __construct() {
                parent::__construct(
                    'blog_bee_category_collection_widget',
                    esc_html__( 'Blog Bee: Category Collection', 'blog-bee' ),
                    [ 'description' =>  __( 'Display posts of the selected categories.', '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 ) {
                $cat_ids = isset( $instance[ 'categories' ] ) ? $instance[ 'categories' ] : 'all';
                $cat_attrs = [
                    'taxonomy'  =>  'category',
                    'include'   =>  $cat_ids
                ];
                $categories = get_terms( $cat_attrs );
                if( empty( $categories ) || ! is_array( $categories ) ) return;

                extract( $args );
                $title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : esc_html__( 'Category Collection', 'blog-bee' );

                $widget_class[] = 'category-collection-widget';
                $widget_class[] = 'bbe-sidebar-widget';
                $attachment_ids = [];

                echo wp_kses_post( $before_widget );

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

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

                        echo '<ul class="category-wrap">';

                            foreach( $categories as $cat ) :
                                    
                                $li_class = 'cat-item';
                                $li_class .= ' cat-item-' . $cat->term_id;
                                $query_attrs[ 'cat' ] = $cat->term_id;
                                $attachment = Section_Category_Collection::get_category_attachment_id( $cat->term_id, $attachment_ids );
                                $attachment_ids[] = $attachment;
                                if( $attachment ) $li_class .= ' has-post-thumbnail';

                                echo '<li class="' . esc_attr( $li_class ) . '">';
                                
                                    Query_Manager::get_opening_figure_tag();

                                        $title = get_the_title( $attachment );
                                        $img_tag = wp_get_attachment_image( $attachment, 'bbe_post_card', false, [ 'alt' => $title, 'loading' => 'lazy' ] );
                                    
                                        echo '<a href="' . esc_url( get_category_link( $cat->term_id ) ) . '">' . $img_tag . '</a>';

                                    Query_Manager::get_closing_figure_tag();

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

                                        echo '<a href="' . esc_url( get_category_link( $cat->term_id ) ) . '" class="cat-item-link">' . esc_html( $cat->name ) . '</a>';

                                        echo '<a href="' . esc_url( get_category_link( $cat->term_id ) ) . '" class="cat-item-count">' . esc_html( $cat->count ) . '</a>';

                                    echo '</div>';

                                echo '</li>';

                            endforeach;
                            
                        echo '</ul>';

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

            /**
             * 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__( 'Category Collection', 'blog-bee' )
                    ],
                    [
                        'name' => 'categories',
                        'type' => 'multiselect',
                        'title' => esc_html__( 'Categories', 'blog-bee' ),
                        'description' => esc_html__( 'Choose the categories to display.', 'blog-bee' ),
                        'default'   => [],
                        'fetch' =>  'category'
                    ],
                ];
            }
        }
        Base::set_widget( Category_Collection::class );
    }