<?php
    /**
     * Customizer Sticky Header builder section
     * 
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Customizer\Section;

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

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

    if( ! class_exists( __NAMESPACE__ . '\\Sticky_Header_Builder' ) ) {
        /**
         * Header builder
         */
        class Sticky_Header_Builder extends Section {
            /**
             * Id of section
             * 
             * @since 1.0.0
             * @var string
             */
            public static $id = 'bbe-sticky-header';

            /**
             * Widgets to hide in tablet
             * 
             * @since 1.0.0
             */
            private static $tablet_hide = null;

            /**
             * Widgets to hide in mobile
             * 
             * @since 1.0.0
             */
            private static $mobile_hide = null;

            /**
             * Widgets to hide in tablet
             * 
             * @since 1.0.0
             */
            private static function get_tablet_hidden_widgets() {
                if( self::$tablet_hide === null ) {
                    $hidden = Theme_Defaults::get_theme_option( 'sticky_header_tablet_widgets_hide' );
                    if( $hidden ) {
                        self::$tablet_hide = explode( ' ', $hidden );
                    }
                }
                return is_null( self::$tablet_hide ) ? [] : self::$tablet_hide;
            }

            /**
             * Widgets to hide in mobile
             * 
             * @since 1.0.0
             */
            private static function get_mobile_hidden_widgets() {
                if( self::$mobile_hide === null ) {
                    $hidden = Theme_Defaults::get_theme_option( 'sticky_header_mobile_widgets_hide' );
                    if( $hidden ) {
                        self::$mobile_hide = explode( ' ', $hidden );
                    }
                }
                return is_null( self::$mobile_hide ) ? [] : self::$mobile_hide;
            }



            /**
             * Register controls
             * Render
             * 
             * @since 1.0.0
             * @override
             */
            protected function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'sticky_header_section' );
                $this->add_control( 'sticky_header_builder_section_tab' );
                $this->add_control( 'sticky_header_builder_layouts' );
                $this->add_control( 'sticky_header_builder' );
                $this->add_control( 'sticky_header_tablet_widgets_hide' );
                $this->add_control( 'sticky_header_mobile_widgets_hide' );
                $this->tab = 'design';
                $this->add_control( 'sticky_header_builder_margin' );
            }

            /**
             * Get settings
             * 
             * @since 1.0.0
             * @override
             */
            protected function get_settings( $id = '' ) {
                $settings = [
                    'sticky_header_builder_section_tab' =>  [
                        'transport'   =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sticky_header_builder_layouts' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_image' ],
                        'default' =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sticky_header_builder' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_builder' ],
                        'default' =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sticky_header_tablet_widgets_hide' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_multiselect' ],
                        'default' =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sticky_header_mobile_widgets_hide' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_multiselect' ],
                        'default' =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sticky_header_builder_margin' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_dimension' ],
                        '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 = [
                    'sticky_header_section' =>  [
                        'title' =>  esc_html__( 'Sticky Header', 'blog-bee' ),
                        'active_callback'   =>  function( $setting ){
                            return $setting->manager->get_setting( 'header_builder_enable_sticky' )->value();
                        },
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'sticky_header_builder_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'sticky_header_builder_layouts' =>  [
                        'label' =>  esc_html__( 'Layouts', 'blog-bee' ),
                        'type'  =>  'builder-layout',
                        'builder_id'    =>  'sticky_header_builder',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Custom', 'blog-bee' ),
                                'url' =>  get_template_directory_uri() . '/inc/customizer/assets/images/header-builder-custom.webp',
                                'value' =>  'custom'
                            ],
                            [
                                'label' =>  esc_html__( 'Layout 1', 'blog-bee' ),
                                'url' =>  get_template_directory_uri() . '/inc/customizer/assets/images/header-builder-one.webp',
                                'value' =>  'one'
                            ],
                            [
                                'label' =>  esc_html__( 'Layout 2', 'blog-bee' ),
                                'url' =>  get_template_directory_uri() . '/inc/customizer/assets/images/header-builder-two.webp',
                                'value' =>  'two'
                            ]
                        ],
                        'field_values'  =>  [
                            'one'   =>  [
                                'middle'    =>  [
                                    'first' =>  [ 'site-logo' ],
                                    'second' =>  [],
                                    'third' =>  [ 'primary-menu', 'custom-button' ]
                                ],
                                'bottom'    =>  [
                                    'first' =>  [],
                                    'second' =>  [],
                                    'third' =>  []
                                ],
                            ],
                            'two'   =>  [
                                'middle'    =>  [
                                    'first' =>  [],
                                    'second' =>  [ 'site-logo' ],
                                    'third' =>  []
                                ],
                                'bottom'    =>  [
                                    'first' =>  [ 'off-canvas' ],
                                    'second' =>  [ 'primary-menu' ],
                                    'third' =>  [ 'search-icon' ]
                                ],
                            ],
                        ]
                    ],
                    'sticky_header_tablet_widgets_hide' =>  [
                        'label' =>  esc_html__( 'Widgets to Hide in Tablet', 'blog-bee' ),
                        'type'  =>  'multiselect',
                        'fields'    =>  $this->get_visibility_widgets()
                    ],
                    'sticky_header_mobile_widgets_hide' =>  [
                        'label' =>  esc_html__( 'Widgets to Hide in Mobile', 'blog-bee' ),
                        'type'  =>  'multiselect',
                        'fields'    =>  $this->get_visibility_widgets()
                    ],
                    'sticky_header_builder' =>  [
                        'type'  =>  'bbe-builder',
                        'responsive_control_id' =>  'header_responsive_builder',
                        'layout' =>  'sticky_header_builder_layouts',
                        'row_section_ids'   =>  [
                            'middle' =>  'sticky_header_middle_row_section',
                            'bottom' =>  'sticky_header_bottom_row_section',
                        ],
                        'widgets'   =>  [
                            'custom-button'  =>  [
                                'label' =>  esc_html__( 'Button', 'blog-bee' ),
                                'icon'  =>  'button',
                                'section_id' =>  'sticky_custom_button_section'
                            ],
                            'dark-mode'  =>  [
                                'label' =>  esc_html__( 'Dark Mode', 'blog-bee' ),
                                'icon'  =>  'lightbulb',
                                'section_id' =>  'sticky_dark_mode_section'
                            ],
                            'date-time'  =>  [
                                'label' =>  esc_html__( 'Date Time', 'blog-bee' ),
                                'icon'  =>  'calendar',
                                'section_id' =>  'sticky_date_time_section'
                            ],
                            'header-divider-1'  =>  [
                                'label' =>  esc_html__( 'Divider 1', 'blog-bee' ),
                                'icon'  =>  'minus',
                                'section_id' =>  'sticky_divider_one_section'
                            ],
                            'header-divider-2'  =>  [
                                'label' =>  esc_html__( 'Divider 2', 'blog-bee' ),
                                'icon'  =>  'minus',
                                'section_id' =>  'sticky_divider_two_section',
                                'disable'   =>  true
                            ],
                            'header-divider-3'  =>  [
                                'label' =>  esc_html__( 'Divider 3', 'blog-bee' ),
                                'icon'  =>  'minus',
                                'section_id' =>  'sticky_divider_three_section',
                                'disable'   =>  true
                            ],
                            'off-canvas'  =>  [
                                'label' =>  esc_html__( 'Off Canvas', 'blog-bee' ),
                                'icon'  =>  'text-page',
                                'section_id'    =>  'sticky_off_canvas_section'
                            ],
                            'primary-menu'  =>  [
                                'label' =>  esc_html__( 'Menu 1', 'blog-bee' ),
                                'icon'  =>  'menu',
                                'section_id' =>  'sticky_primary_menu_section'
                            ],
                            'secondary-menu'  =>  [
                                'label' =>  esc_html__( 'Menu 2', 'blog-bee' ),
                                'icon'  =>  'menu',
                                'section_id'    =>  'sticky_secondary_menu_section'
                            ],
                            'random-blog'  =>  [
                                'label' =>  esc_html__( 'Random Blog', 'blog-bee' ),
                                'icon'  =>  'randomize',
                                'section_id'    =>  'sticky_random_blog_section',
                                'disable'   =>  true
                            ],
                            'search-field'  =>  [
                                'label' =>  esc_html__( 'Search Field', 'blog-bee' ),
                                'icon'  =>  'search',
                                'section_id'    =>  'sticky_search_field_section',
                                'disable'   =>  true
                            ],
                            'search-icon'  =>  [
                                'label' =>  esc_html__( 'Search Icon', 'blog-bee' ),
                                'icon'  =>  'search',
                                'section_id'    =>  'sticky_search_icon_section'
                            ],
                            'site-logo'  =>  [
                                'label' =>  esc_html__( 'Site Identity', 'blog-bee' ),
                                'icon'  =>  'admin-site',
                                'section_id' =>  'sticky_title_tagline'
                            ],
                            'social-icons'  =>  [
                                'label' =>  esc_html__( 'Social Icons', 'blog-bee' ),
                                'icon'  =>  'share',
                                'section_id'    =>  'sticky_social_icons_section'
                            ],
                            'time'  =>  [
                                'label' =>  esc_html__( 'Time', 'blog-bee' ),
                                'icon'  =>  'clock',
                                'section_id'    =>  'sticky_time_section'
                            ],
                        ]
                    ],
                    'sticky_header_builder_margin' =>  [
                        'label' =>  esc_html__( 'Margin', 'blog-bee' ),
                        'type'  =>  'dimension',
                        'exclude'   =>  [ 'right', 'bottom', 'left' ]
                    ],
                ];
                return ( $id ) ? $controls[ $id ] : $controls;
            }

            /**
             * Set defaults
             * Theme Starter
             * 
             * @since 1.0.0
             * @var array
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'sticky_header_builder_section_tab' =>  'general',
                    'sticky_header_builder_layouts' =>  'one',
                    'sticky_header_scroll_toggles'  =>  [
                        'down'  =>  true,
                        'up'    =>  true,
                    ],
                    'sticky_header_tablet_widgets_hide'  =>  '',
                    'sticky_header_mobile_widgets_hide'  =>  '',
                    'sticky_header_builder' =>  [
                        'middle'   =>  [
                            'first' =>  [ 'site-logo' ],
                            'second'    =>  [],
                            'third' =>  [ 'primary-menu', 'custom-button' ]
                        ],
                        'bottom'    =>  [
                            'first' =>  [],
                            'second'    =>  [],
                            'third' =>  []
                        ],
                    ],
                    'sticky_header_builder_background' =>  $this->get_color( [ 'value' => '' ] ),
                    'sticky_header_builder_border' =>  $this->get_border(),
                    'sticky_header_builder_margin' =>  $this->get_dimension(),
                ] );
            }

            /**
             * Add partials
             * 
             * @override
             * @since 1.0.0
             */
            protected function register_partials() {
                $this->manager->selective_refresh->add_partial( 'sticky_header_builder_section_tab', [
                    'selector'        => 'header.site-header .bbe-sticky-header'
                ]);
            }

            /**
             * Dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                $dynamic_css_args = [
                    'sticky_header_builder_margin' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'sticky_header_builder_margin' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'sticky_header_builder_margin' ),
                        'selector'  =>  'body .bbe-header .bbe-sticky-header',
                        'property'  =>  'margin'
                    ],
                ];
                return $dynamic_css_args;
            }

            /**
             * Render html
             * 
             * @return html
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $search_icon_instance = Search_Icon::get_instance();
                ?>
                    <div class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>">
                        <?php
                            self::get_instance()->render_row_html( 'bbe-header-middle-row', 'middle', 'sticky_header_builder' );

                            self::get_instance()->render_row_html( 'bbe-header-bottom-row', 'bottom', 'sticky_header_builder' );

                            if( Helpers::is_widget_in_builder( 'sticky_header_builder', 'search-icon' ) && $search_icon_instance->get_layout() !== 'one' ) $search_icon_instance->get_form_wrapper( [ 'class' => 'sticky' ] );
                        ?>    
                    </div>
                <?php
            }

            /**
             * Render row html
             * 
             * @return html
             * @since 1.0.0
             * @param   string  $row_id The row id
             * @param   string  $row    The row
             */
            public static function render_row_html( $row_id, $row, $builder_id ) {
                $block_class[] = $row_id;
                $block_class[] = 'bbe-header-row';
                $block_class[] = 'bbe-row';
                $full_width = Theme_Defaults::get_theme_option( 'sticky_header_'. $row .'_row_full_width' );
                $block_class[] = 'full-width--' . $full_width;
                
                $header_builder = Theme_Defaults::get_theme_option( $builder_id );
                $row = $header_builder[ $row ];
                if( ! array_filter( $row ) ) return;

                if( empty( $row ) || ! is_array( $row ) ) {
                    // return $row is empty or not an array, no need to render row
                    return;
                } else {
                    // add class if row has widgets
                    $widgets = $row && isset( $row[ 'second' ] ) ? $row[ 'second' ] : [];
                    if( count( $widgets ) > 0 ) {
                        $block_class[] = 'has-middle-column';
                    }
                }

                $all_registered_sections = self::get_all_registered_sections();
                ?>  
                    <div class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>">
                        <div class="bbe-container">
                            <div class="row">
                                <?php
                                    foreach( $row as $column => $widgets ) :
                                        $column_class = 'column ' . $column;
                                        if( in_array( 'primary-menu', $widgets ) ) $column_class .= ' has-primary-menu';
                                        if( in_array( 'secondary-menu', $widgets ) ) $column_class .= ' has-secondary-menu';
                                        if( ( $column === 'second' ) && empty( $widgets ) ) continue;  // skip if no widgets in column, apply this logic for second columns only
                                        echo '<div class="'. esc_attr( $column_class ) .'">';
                                            foreach( $widgets as $widget ) :
                                                $data_attrs = [
                                                    'data-tablet=' . esc_attr( ( in_array( $widget, self::get_tablet_hidden_widgets() ) ? 'hide' : 'show' ) ) . '',
                                                    'data-mobile=' . esc_attr( ( in_array( $widget, self::get_mobile_hidden_widgets() ) ? 'hide' : 'show' ) ) . '',
                                                ];
                                                echo '<div class="' . esc_attr( $widget ) . '" ' . esc_attr( implode( ' ', $data_attrs ) ) . '>';

                                                    $all_registered_sections[ 'bbe-sticky-' . $widget ]::get_instance()->render_html();

                                                echo '</div>';
                                            endforeach;
                                        echo '</div>';
                                    endforeach;
                                ?>
                            </div>
                        </div>
                    </div>
                <?php
            }

            /**
             * Get widgets
             * 
             * @since 1.0.0
             */
            private function get_visibility_widgets() {
                return [
                    [
                        'label' =>  esc_html__( 'Button', 'blog-bee'),
                        'value' =>  'custom-button'
                    ],
                    [
                        'label' =>  esc_html__( 'Dark Mode', 'blog-bee'),
                        'value' =>  'dark-mode'
                    ],
                    [
                        'label' =>  esc_html__( 'Date Time', 'blog-bee'),
                        'value' =>  'date-time'
                    ],
                    [
                        'label' =>  esc_html__( 'Divider 1', 'blog-bee'),
                        'value' =>  'header-divider-1'
                    ],
                    [
                        'label' =>  esc_html__( 'Divider 2', 'blog-bee'),
                        'value' =>  'header-divider-2'
                    ],
                    [
                        'label' =>  esc_html__( 'Divider 3', 'blog-bee'),
                        'value' =>  'header-divider-3'
                    ],
                    [
                        'label' =>  esc_html__( 'Off Canvas', 'blog-bee'),
                        'value' =>  'off-canvas'
                    ],
                    [
                        'label' =>  esc_html__( 'Primary Menu', 'blog-bee'),
                        'value' =>  'primary-menu'
                    ],
                    [
                        'label' =>  esc_html__( 'Secondary Menu', 'blog-bee'),
                        'value' =>  'secondary-menu'
                    ],
                    [
                        'label' =>  esc_html__( 'Random Blog', 'blog-bee'),
                        'value' =>  'random-blog'
                    ],
                    [
                        'label' =>  esc_html__( 'Search Field', 'blog-bee'),
                        'value' =>  'search-field'
                    ],
                    [
                        'label' =>  esc_html__( 'Search Icon', 'blog-bee'),
                        'value' =>  'search-icon'
                    ],
                    [
                        'label' =>  esc_html__( 'Site Logo', 'blog-bee'),
                        'value' =>  'site-logo'
                    ],
                    [
                        'label' =>  esc_html__( 'Social Icons', 'blog-bee'),
                        'value' =>  'social-icons'
                    ],
                    [
                        'label' =>  esc_html__( 'Time', 'blog-bee'),
                        'value' =>  'time'
                    ],
                ];
            }
        }
        Sticky_Header_Builder::get_instance();
    }