<?php
    /**
     * Social Icons section
     * 
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Customizer\Section;

    use BBE\Customizer\Section\Section as Section;
    use BBE\Customizer\Theme_Defaults as Theme_Defaults;
    use BBE\Helpers as Helpers;

    use function esc_html__;
    use function esc_url;
    use function home_url;
    use function esc_attr;

    if( ! class_exists( __NAMESPACE__ . '\\Social_Icons' ) ) {
        /**
         * Social_Icons Class
         */
        class Social_Icons extends Section {
            /**
             * Section id
             * 
             * @since 1.0.0 
             */
            public static $id = 'bbe-social-icons';

            /**
             * Is section active
             * 
             * @since 1.0.0
             */
            private function is_active(): bool {
                $header_builder = Helpers::is_widget_in_builder( 'header_builder', 'social-icons' );
                $header_responsive_builder = Helpers::is_widget_in_builder( 'header_responsive_builder', 'social-icons' );
                $is_widget_active = is_active_widget( false, false, 'blog_bee_social_platforms_widget', true );
                $sticky_builder = Helpers::is_widget_in_builder( 'sticky_header_builder', 'social-icons' );
                return ( $header_builder || $header_responsive_builder || $is_widget_active || $sticky_builder );
            }

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'social_icons_section' );
                $this->add_control( 'social_icons_section_tab' );
                $this->add_control( 'social_icons_profiles' );
                $this->add_control( 'social_icons_enable_label' );
                $this->add_control( 'social_icons_style' );
                $this->tab = 'design';
                $this->add_control( 'social_icons_label_typography' );
                $this->add_control( 'social_icons_label_color' );
                $this->add_control( 'social_icons_color' );
                $this->add_control( 'social_icons_background' );
                $this->add_control( 'social_icons_border_color' );
                $this->add_control( 'social_icons_spacing' );
            }

            /**
             * Settings
             * 
             * @param string $id    The setting id
             * @since 1.0.0
             */
            public function get_settings( $id = '' ): array {
                $settings = [
                    'social_icons_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'social_icons_profiles' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_repeater' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'social_icons_enable_label' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'social_icons_style' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_multiselect' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'social_icons_label_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'social_icons_label_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'social_icons_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'social_icons_spacing' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_range' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'social_icons_background' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'social_icons_border_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @param string    $id     The control id
             * @since 1.0.0
             */
            public function get_controls( $id = '' ): array {
                $controls = [
                    'social_icons_section' =>  [
                        'title' =>  esc_html__( 'Social Icons', 'blog-bee' ),
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'social_icons_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'social_icons_profiles'   =>  [
                        'label' =>  esc_html__( 'Social Profiles', 'blog-bee' ),
                        'type'  =>  'bbe-repeater',
                        'heading_key'   =>  'label',    // Should match one of the field keys to show as heading in each repeater item
                        'fields'    =>  [
                            'url'   =>  [
                                'label' =>  esc_html__( 'URL', 'blog-bee' ),
                                'type' =>  'url'
                            ],
                            'label'   =>  [
                                'label' =>  esc_html__( 'Label', 'blog-bee' ),
                                'type' =>  'text',
                            ],
                            'icon'  =>  [
                                'label' =>  esc_html__( 'Icon', 'blog-bee' ),
                                'type' =>  'icon-picker',
                                'enable_search' =>  true,
                                'recommended_icons'   =>    [
                                    'fa-brands fa-facebook',
                                    'fa-brands fa-facebook-f',
                                    'fa-brands fa-facebook-messenger',
                                    'fa-brands fa-instagram',
                                    'fa-brands fa-whatsapp',
                                    'fa-brands fa-twitter',
                                    'fa-brands fa-x-twitter',
                                    'fa-brands fa-tiktok',
                                    'fa-brands fa-snapchat',
                                    'fa-brands fa-pinterest',
                                    'fa-brands fa-reddit',
                                    'fa-brands fa-tumblr',
                                    'fa-brands fa-threads',
                                    'fa-brands fa-mastodon',
                                    'fa-brands fa-bluesky',
                                    'fa-brands fa-linkedin',
                                    'fa-brands fa-linkedin-in',
                                    'fa-brands fa-youtube',
                                    'fa-brands fa-twitch',
                                    'fa-brands fa-vimeo',
                                    'fa-brands fa-dailymotion',
                                    'fa-brands fa-discord',
                                    'fa-brands fa-telegram',
                                    'fa-brands fa-skype',
                                    'fa-brands fa-viber',
                                    'fa-brands fa-weixin',
                                    'fa-brands fa-qq',
                                    'fa-brands fa-vk',
                                    'fa-brands fa-odnoklassniki',
                                    'fa-brands fa-weibo',
                                    'fa-brands fa-renren'
                                ]
                            ],
                        ]
                    ],
                    'social_icons_enable_label' =>  [
                        'label' =>  esc_html__( 'Enable Labels', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'social_icons_style'    =>  [
                        'label' =>  esc_html__( 'Icon Style', 'blog-bee' ),
                        'type'  =>  'multiselect',
                        'fields'  => Helpers::get_customizer_icon_style()
                    ],
                    'social_icons_label_typography' =>  [
                        'label' =>  esc_html__( 'Label Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'social_icons_label_color'    =>  [
                        'label' =>  esc_html__( 'Label Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'social_icons_color'    =>  [
                        'label' =>  esc_html__( 'Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                    ],
                    'social_icons_spacing'  =>  [
                        'label' =>  esc_html__( 'Icon Spacing', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'input_attrs'   =>  [
                           'px'    =>  [
                                'min'   =>  0,
                                'max'   =>  30,
                                'step'  =>  1
                            ],
                        ]
                    ],
                    'social_icons_background'   =>  [
                        'label' =>  esc_html__( 'Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                        'color_types'   =>  [ 'solid', 'gradient' ],
                    ],
                    'social_icons_border_color' =>  [
                        'label' =>  esc_html__( 'Border Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Set defaults
             * 
             * @since 1.0.0
             * 
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'social_icons_section_tab' =>  'general',
                    'social_icons_profiles'   =>  [
                        [
                            'url'   =>  esc_url( home_url() ),
                            'label' =>  'Facebook',
                            'icon'  =>  'fa-brands fa-facebook'
                        ],
                        [
                            'url'   =>  esc_url( home_url() ),
                            'label' =>  'Twitter',
                            'icon'  =>  'fa-brands fa-twitter'
                        ],
                        [
                            'url'   =>  esc_url( home_url() ),
                            'label' =>  'Instagram',
                            'icon'  =>  'fa-brands fa-instagram'
                        ],
                        [
                            'url'   =>  esc_url( home_url() ),
                            'label' =>  'LinkedIn',
                            'icon'  =>  'fa-brands fa-linkedin'
                        ],
                        [
                            'url'   =>  esc_url( home_url() ),
                            'label' =>  'Threads',
                            'icon'  =>  'fa-brands fa-threads'
                        ],
                    ],
                    'social_icons_target'   =>  '_blank',
                    'social_icons_enable_label' =>  false,
                    'social_icons_use_brand_colors' =>  false,
                    'social_icons_style'    =>  '',
                    'social_icons_icon_size'    =>  $this->get_responsive( '16px', '16px', '16px' ),
                    'social_icons_label_typography'   =>  $this->get_typography([
                        'font_weight'   =>  '500',
                        'font_size'    =>  $this->get_responsive( '14px', '14px', '14px' )
                    ]),
                    'social_icons_label_color'    =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                        'hover'   =>  $this->get_color(),
                    ],
                    'social_icons_color'    =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                        'hover'   =>  $this->get_color(),
                    ],
                    'social_icons_spacing'  =>  '8px',
                    'social_icons_background'   =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#efefef' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#efefef' ] ),
                    ],
                    'social_icons_border_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#000' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#000' ] ),
                    ],
                    'social_icons_width_height'  =>  '35px',
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! $this->is_active() ) return [];
                $dynamic_css_args = [
                    'social_icons_label_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'social_icons_label_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'social_icons_label_typography' ),
                        'selector'  =>  '.bbe-header .bbe-social-icons .label',
                    ],
                    'social_icons_label_color'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'social_icons_label_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'social_icons_label_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-social-icons .social-icon .label',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'social_icons_color'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'social_icons_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'social_icons_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-social-icons.official--off i',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'social_icons_background'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'social_icons_background' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'social_icons_background' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-social-icons.filled.official--off i',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                    'social_icons_border_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'social_icons_border_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'social_icons_border_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-social-icons.outline.official--off i',
                        'property'  =>  '--bbe-site-border-color'
                    ],
                    'social_icons_spacing'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'social_icons_spacing' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'social_icons_spacing' ),
                        'selector'  =>  '.bbe-header .bbe-social-icons',
                        'property'  =>  'gap'
                    ],
                ];
                return $dynamic_css_args;
            }

            /**
             * Render html
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $block_class[] = 'header-widget';
                $block_class[] = 'official--' . ( Theme_Defaults::get_theme_option( 'social_icons_use_brand_colors' ) ? 'on' : 'off' );
                $block_class[] = Theme_Defaults::get_theme_option( 'social_icons_style' );
                $block_class = apply_filters( 'bbe_header_social_icons_classes_filter', $block_class );
                $enable_label = Theme_Defaults::get_theme_option( 'social_icons_enable_label' );

                $social_icons = Theme_Defaults::get_theme_option( 'social_icons_profiles' );
                $target = Theme_Defaults::get_theme_option( 'social_icons_target' );
                
                if( empty( $social_icons ) || ! is_array( $social_icons ) ) return; // return if no social icons added

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

                    foreach( $social_icons as $icon ) :

                        $class = 'social-icon';
                        $icon_label = Helpers::get_social_icon_label( $icon[ 'icon' ] );
                        if( $icon_label ) $class .= ' ' . strtolower( $icon_label );

                        echo '<a href="' . esc_url( $icon[ 'url' ] ) . '" class="' . esc_attr( $class ) . '" target="' . esc_attr( $target ) . '" aria-label="' . esc_attr( $icon[ 'label' ] ) . '">';

                            echo '<i class="' . esc_attr( $icon[ 'icon' ] ) . '"></i>';

                            if( $enable_label ) echo '<span class="label">' . esc_html( $icon[ 'label' ] ) . '</span>';

                        echo '</a>';

                    endforeach;
                    
                echo '</div><!-- .bbe-social-icons -->';
            }
        }
        Social_Icons::get_instance();
    }