<?php
    /**
     * Author info 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\Helpers as Helpers;
    use BBE\Query_Manager as Query_Manager;
    use BBE\Customizer\Section\Pagination as Pagination;
    
    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 get_the_title;
    use function wp_get_attachment_image;

    if( ! class_exists( __NAMESPACE__ . '\\Author_Info' ) ) {
        /**
         * Author_Info class
         */
        class Author_Info extends Base {
            /**
             * Constructor
             */
            public function __construct() {
                parent::__construct(
                    'blog_bee_author_info_widget',
                    esc_html__( 'Blog Bee: Author Info', 'blog-bee' ),
                    [ 'description' =>  __( 'Display details of author.', '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 ) {
                extract( $args );
                $author = isset( $instance[ 'author' ] ) ? $instance[ 'author' ] : 'custom';
                $title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : esc_html__( 'Author Info', 'blog-bee' );
                $radius = isset( $instance[ 'author_radius' ] ) ? $instance[ 'author_radius' ] : 'normal';
                if( $author === 'custom' ) {
                    $name = isset( $instance[ 'author_name' ] ) ? $instance[ 'author_name' ] : esc_html( 'John Doe', 'blog-bee' );
                    $image_id = isset( $instance[ 'author_image' ] ) ? $instance[ 'author_image' ] : 0;
                    $image = wp_get_attachment_image( $image_id, 'bbe_post_card', false, [ 'alt' => get_the_title( $image_id ), 'loading' => 'lazy' ] );
                    $tag = isset( $instance[ 'author_tag' ] ) ? $instance[ 'author_tag' ] : esc_html( 'Writer', 'blog-bee' );
                    $url = isset( $instance[ 'author_url' ] ) ? $instance[ 'author_url' ] : '#';
                    $description = isset( $instance[ 'author_description' ] ) ? $instance[ 'author_description' ] : esc_html__( 'Lorem ipsum is simply dummy text is simply dummy text Lorem ipsum is simply dummy text.', 'blog-bee' );
                } else {
                    $author_attrs = [ 
                        'fields'	=>	[ 'display_name', 'user_url', 'user_nicename' ],
                        'include' => [ $author ]
                    ];
                    $description = get_the_author_meta( 'description', $author );
                    $image = get_avatar( $author, 125 );
                    $author_info = get_users( $author_attrs );
                    if( $author_info ) {
                        $name = $author_info[ 0 ]->display_name;
                        $tag = $author_info[ 0 ]->user_nicename;
                        $url = $author_info[ 0 ]->user_url;
                    }
                }

                $widget_class[] = 'author-info-widget';
                $widget_class[] = 'bbe-sidebar-widget';
                $widget_class[] = 'radius--' . $radius;

                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="author-info">';

                            if( $image ) :

                                Query_Manager::get_opening_figure_tag();
    
                                    echo '<a href="' . esc_url( $url ) . '">' . $image . '</a>';
    
                                Query_Manager::get_closing_figure_tag();

                            endif;

                            if( $name || $tag ) :

                                echo '<div class="author-info-inner">';
    
                                    if( $name ) echo '<h2 class="author-name"><a href="' . esc_url( $url ) . '">' . esc_html( $name ) . '</a></h2>';
    
                                    if( $tag ) echo '<span class="author-tag">' . esc_html( $tag ) . '</span>';
    
                                echo '</div>';
                                
                            endif;

                            if( $description ) echo '<p class="author-desc">' . esc_html( $description ) . '</p>';

                            if( $author !== 'custom' ) Helpers::get_author_socials( $author );

                        echo '</div>';

                    echo '</div><!-- .author-info-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__( 'Author Info', 'blog-bee' )
                    ],
                    [
                        'name' => 'author',
                        'type' => 'select',
                        'title' => esc_html__( 'Author to Display', 'blog-bee' ),
                        'default'   => 'custom',
                        'choices' => self::get_users()
                    ],
                    [
                        'name' => 'custom_author_settings',
                        'type' => 'heading',
                        'title' => esc_html__( 'Custom Author Details', 'blog-bee' ),
                    ],
                    [
                        'name' => 'author_name',
                        'type' => 'text',
                        'title' => esc_html__( 'Author Name', 'blog-bee' ),
                        'default'   =>  esc_html__( 'Admin', 'blog-bee' )
                    ],
                    [
                        'name' => 'author_image',
                        'type' => 'upload',
                        'title' => esc_html__( 'Author Image', 'blog-bee' ),
                        'default'   =>  0
                    ],
                    [
                        'name' => 'author_tag',
                        'type' => 'text',
                        'title' => esc_html__( 'Author Tag', 'blog-bee' ),
                        'default'   =>  esc_html__( 'Writer', 'blog-bee' )
                    ],
                    [
                        'name' => 'author_url',
                        'type' => 'url',
                        'title' => esc_html__( 'Author URL', 'blog-bee' ),
                        'default'   =>  ''
                    ],
                    [
                        'name' => 'author_description',
                        'type' => 'textarea',
                        'title' => esc_html__( 'Author Description', 'blog-bee' ),
                        'default'   =>  esc_html__( 'Lorem ipsum is simply dummy text is simply dummy text Lorem ipsum is simply dummy text.', 'blog-bee' )
                    ],
                    [
                        'name' => 'author_radius',
                        'type' => 'select',
                        'title' => esc_html__( 'Image Radius', 'blog-bee' ),
                        'choices'   =>  [
                            'normal'    =>  esc_html__( 'Normal', 'blog-bee' ),
                            'circle'    =>  esc_html__( 'Circle', 'blog-bee' ),
                            'rounded'   =>  esc_html__( 'Rounded', 'blog-bee' ),
                        ]
                    ],
                ];
            }

            /**
             * Get users
             * 
             * @since 1.0.0
             */
            private static function get_users() {
                $author_attrs = [ 
                    'fields'	=>	[ 'ID', 'display_name' ]
                ];
                $users = [
                    'custom'    =>  esc_html__( 'Custom', 'blog-bee' ),
                ];
                $wp_users = get_users( $author_attrs );
                if( ! empty( $wp_users ) && is_array( $wp_users ) ) {
                    foreach( $wp_users as $user ) {
                        $users[ $user->ID ] = esc_html( $user->display_name );
                    }
                }
                return $users;
            }
        }
        Base::set_widget( Author_Info::class );
    }