<?php
    /**
     * Tag 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 function esc_html__;
    use function wp_kses_post;
    use function esc_attr;
    use function esc_html;
    use function get_terms;
    use function get_tag_link;
    use function esc_url;

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

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

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

                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 '<div class="tag-wrap">';

                            foreach( $tags as $tag ) :
                                $li_class = 'tag-item';
                                $li_class .= ' tag-item-' . $tag->term_id;
                                $query_attrs[ 'tag' ] = $tag->term_id;

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

                                    echo '<a href="' . esc_url( get_tag_link( $tag->term_id ) ) . '" class="tag-item-link">';

                                        echo '<span class="label">' . esc_html( $tag->name ) . '</span>';

                                        echo '<span class="count">' . esc_html( $tag->count ) . '</span>';

                                    echo '</a>';

                                echo '</li>';

                            endforeach;
                            
                        echo '</div>';

                    echo '</div><!-- .tag-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__( 'Tag Collection', 'blog-bee' )
                    ],
                    [
                        'name' => 'tags',
                        'type' => 'multiselect',
                        'title' => esc_html__( 'Tags', 'blog-bee' ),
                        'description' => esc_html__( 'Choose the tags to display.', 'blog-bee' ),
                        'default'   => [],
                        'fetch' =>  'post_tag'
                    ],
                ];
            }
        }
        Base::set_widget( Tag_Collecton::class );
    }