<?php
    /**
     * Social Platforms 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\Customizer\Section\Social_Icons as Social_Icons;
    
    use function esc_html__;
    use function wp_kses_post;
    use function esc_attr;
    use function esc_html;

    if( ! class_exists( __NAMESPACE__ . '\\Social_Platforms' ) ) {
        /**
         * Social_Platforms class
         */
        class Social_Platforms extends Base {
            /**
             * Enable official color
             * 
             * @since 1.0.0
             */
            private $official_color = true;

            /**
             * Constructor
             */
            public function __construct() {
                parent::__construct(
                    'blog_bee_social_platforms_widget',
                    esc_html__( 'Blog Bee: Social Platforms', 'blog-bee' ),
                    [ 'description' =>  __( 'Display social platforms.', '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 );
                $title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : esc_html__( 'Social Platforms', 'blog-bee' );
                $this->official_color = isset( $instance[ 'official_color' ] ) ? $instance[ 'official_color' ] : true;

                $widget_class[] = 'social-platforms-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 );

                        add_filter( 'bbe_header_social_icons_classes_filter', [ $this, 'filter_classes' ] );

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

                        remove_filter( 'bbe_header_social_icons_classes_filter', [ $this, 'filter_classes' ] );

                    echo '</div><!-- .social-platforms-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__( 'Social Platforms', 'blog-bee' )
                    ],
                    [
                        'name' => 'official_color',
                        'type' => 'checkbox',
                        'title' => esc_html__( 'Inherit global default social icon colors', 'blog-bee' ),
                        'default'   => true
                    ],
                ];
            }

            /**
             * Filter classes
             * 
             * @param array $classes An array of classes to filter
             * @since 1.0.0
             */
            public function filter_classes( $classes ) {
                $classes_to_remove = [ 'header-widget', 'bbe-social-icons', 'official--on', 'official--off' ];
                $classes = array_diff( $classes, $classes_to_remove );
                $classes[] = 'official--' . ( $this->official_color ? 'on' : 'off' );
                return $classes;
            }

            /**
             * Adding custom widget field
             * 
             * @since 1.0.0
             * @override
             */
            protected function custom_widget_fields() {
                ?>
                    <div class="refer-note">
                        <p>
                            <?php echo esc_html__( 'Manage social icons from customizer ', 'blog-bee' ); ?>
                            <a href="<?php echo admin_url( 'customize.php?autofocus[control]=social_icons_profiles' ); ?>" target="_blank"><?php echo esc_html__( 'go to manage social icons', 'blog-bee' ); ?></a>
                        </p>
                    </div>
                <?php
            }
        }
        Base::set_widget( Social_Platforms::class );
    }