<?php
    /**
     * Customizer scroll to top section
     * 
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Customizer\Section;

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

    use function esc_html__;
    use function get_template_directory_uri;
    use function esc_attr;
    use function esc_html;

    if( ! class_exists( __NAMESPACE__ . '\\Scroll_To_Top' ) ) :
        /**
         * Scroll to top
         */
        class Scroll_To_Top extends Section {
            /**
             * Id of section
             * 
             * @since 1.0.0
             * @var string
             */
            public static $id = 'bbe-scroll-to-top';

            /**
             * Register controls
             * Render
             * 
             * @since 1.0.0
             * @override
             */
            protected function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'scroll_to_top_section' );
                $this->add_control( 'scroll_to_top_section_tab' );
                $this->add_control( 'scroll_to_top_label' );
                $this->add_control( 'scroll_to_top_position' );
                $this->tab = 'design';
                $this->add_control( 'scroll_to_top_typography' );
                $this->add_control( 'scroll_to_top_color' );
                $this->add_control( 'scroll_to_top_background' );
            }

            /**
             * Get settings
             * 
             * @since 1.0.0
             * @override
             */
            protected function get_settings( $id = '' ) {
                $settings = [
                    'scroll_to_top_section_tab' =>  [
                        'transport'   =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'scroll_to_top_label' =>  [
                        'sanitize_callback' =>  'sanitize_text_field',
                        'transport'   =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'scroll_to_top_position' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'transport'   =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'scroll_to_top_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport'   =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'scroll_to_top_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport'   =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'scroll_to_top_background' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport'   =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return ( $id ) ? $settings[ $id ] : $settings;
            }

            /**
             * Get controls
             * Helpers
             * 
             * @since 1.0.0
             * @override
             */
            public function get_controls( $id = '' ) {
                $controls = [
                    'scroll_to_top_section' =>  [
                        'title' =>  esc_html__( 'Scroll to Top', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return Helpers::is_widget_in_builder( 'footer_builder', 'scroll-to-top' );
                        },
                        'section_to_open' =>  'footer_builder_section'
                    ],
                    'scroll_to_top_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'scroll_to_top_label' =>  [
                        'label' =>  esc_html__( 'Label', 'blog-bee' ),
                        'description'   =>  esc_html__( 'Leave the field empty to hide the label.', 'blog-bee' ),
                        'type'  =>  'bbe-text',
                    ],
                    'scroll_to_top_position' =>  [
                        'label' =>  esc_html__( 'Position', 'blog-bee' ),
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Left', 'blog-bee' ),
                                'value' =>  'left'
                            ],
                            [
                                'label' =>  esc_html__( 'Center', 'blog-bee' ),
                                'value' =>  'center'
                            ],
                            [
                                'label' =>  esc_html__( 'Right', 'blog-bee' ),
                                'value' =>  'right'
                            ],
                        ],
                        'type'  =>  'radio-tab',
                        'display_block' =>  true
                    ],
                    'scroll_to_top_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                        'tab'  =>  'design',
                    ],
                    'scroll_to_top_color' =>  [
                        'label' =>  esc_html__( 'Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'tab'  =>  'design',
                        'include_hover'   =>  true
                    ],
                    'scroll_to_top_background' =>  [
                        'label' =>  esc_html__( 'Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'tab'  =>  'design',
                        'include_hover'   =>  true,
                        'color_types'   =>  [ 'solid', 'gradient' ]
                    ],
                ];
                return ( $id ) ? $controls[ $id ] : $controls;
            }

            /**
             * Set defaults
             * Theme Starter
             * 
             * @since 1.0.0
             * @var array
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'scroll_to_top_section_tab' =>  'general',
                    'scroll_to_top_layouts' =>  'one',
                    'scroll_to_top_label' =>  '',
                    'scroll_to_top_icon_picker' =>  $this->get_icon_picker( [ 'value' => 'fa-solid fa-angle-up' ] ),
                    'scroll_to_top_is_fixed' =>  true,
                    'scroll_to_top_position' =>  'right',
                    'scroll_to_top_typography'   =>  $this->get_typography(),
                    'scroll_to_top_color' =>  [
                        'initial'   =>  [
                            'type'  =>  'solid',
                            'value' =>  '#fff'
                        ],
                        'hover'   =>  [
                            'type'  =>  'solid',
                            'value' =>  '#fff'
                        ]
                    ],
                    'scroll_to_top_background' =>  [
                        'initial'   =>  [
                            'type'  =>  'solid',
                            'value' =>  '#30344b'
                        ],
                        'hover'   =>  [
                            'type'  =>  'solid',
                            'value' =>  '--bbe-theme-color'
                        ]
                    ],
                    'scroll_to_top_box_shadow' =>  $this->get_box_shadow()
                ] );
            }

            /**
             * Dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! Helpers::is_widget_in_builder( 'footer_builder', 'scroll-to-top' ) ) return [];
                $dynamic_css_args = [
                    'scroll_to_top_position'  =>  [
                        'selector'  =>  '.bbe-scroll-to-top',
                        'prefix'  =>  'position--'
                    ],
                    'scroll_to_top_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'scroll_to_top_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'scroll_to_top_typography' ),
                        'selector'  =>  '.bbe-footer .bbe-scroll-to-top',
                    ],
                    'scroll_to_top_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'scroll_to_top_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'scroll_to_top_color' ),
                        'selector'  =>  '.bbe-footer .bbe-scroll-to-top',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'scroll_to_top_background'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'scroll_to_top_background' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'scroll_to_top_background' ),
                        'selector'  =>  '.bbe-footer .bbe-scroll-to-top.layout--one .label, .bbe-footer .bbe-scroll-to-top.layout--one .icon',
                        'hover_selector'  =>  '.bbe-footer .bbe-scroll-to-top.layout--one:hover .label, .bbe-footer .bbe-scroll-to-top.layout--one:hover .icon',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                ];
                return $dynamic_css_args;
            }

             /**
             * Add partial
             * 
             * @override
             * @since 1.0.0
             */
            protected function register_partials() {
                $this->manager->selective_refresh->add_partial( 'scroll_to_top_partial', [
                    'selector'  =>  '.bbe-scroll-to-top',
                    'render_callback'   =>  [ $this, 'render_partial' ],
                    'settings'  =>  [ 'scroll_to_top_icon_picker', 'scroll_to_top_label' ]
                ] );
            }

            /**
             * Partial Html
             * 
             * @since 1.0.0
             */
            public function render_partial() {
                $icon_attrs = Theme_Defaults::get_theme_option( 'scroll_to_top_icon_picker' );
                $label = Theme_Defaults::get_theme_option( 'scroll_to_top_label' );
                $icon_attrs[ 'aria_label' ] = __( 'Scroll to top', 'blog-bee' );

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

                echo Helpers::get_icon_html( $icon_attrs );
            }

            /**
             * Render html
             * 
             * @return html
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $block_class[] = 'layout--' . Theme_Defaults::get_theme_option( 'scroll_to_top_layouts' );

                $is_fixed = Theme_Defaults::get_theme_option( 'scroll_to_top_is_fixed' );
                if( $is_fixed ) {
                    $block_class[] = 'is-fixed';
                    $block_class[] = 'position--' . Theme_Defaults::get_theme_option( 'scroll_to_top_position' );
                }

                ?>
                    <div id="<?php echo esc_attr( self::$id ); ?>" class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>">

                        <?php $this->render_partial(); ?>

                    </div><!-- #bbe-scroll-to-top -->
                <?php
            }
        }
        Scroll_To_Top::get_instance();
    endif;