<?php
    /**
     * Secondary Menu 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_attr;
    use function wp_nav_menu;

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

            /**
             * Is section active
             * 
             * @since 1.0.0
             */
            private function is_active(): bool {
                $header_builder = Helpers::is_widget_in_builder( 'header_builder', 'secondary-menu' );
                $header_responsive_builder = Helpers::is_widget_in_builder( 'header_responsive_builder', 'secondary-menu' );
                $sticky_builder = Helpers::is_widget_in_builder( 'sticky_header_builder', 'secondary-menu' );
                return ( $header_builder || $header_responsive_builder || $sticky_builder );
            }

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'secondary_menu_section' );
                $this->add_control( 'secondary_menu_section_tab' );
                $this->add_control( 'secondary_menu_hover_animation' );
                $this->tab = 'design';
                $this->add_control( 'secondary_menu_typography' );
                $this->add_control( 'secondary_menu_color' );
            }

            /**
             * Settings
             * 
             * @param string $id    The setting id
             * @since 1.0.0
             */
            public function get_settings( $id = '' ): array {
                $settings = [
                    'secondary_menu_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'secondary_menu_hover_animation' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'secondary_menu_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'secondary_menu_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 = [
                    'secondary_menu_section' =>  [
                        'title' =>  esc_html__( 'Secondary Menu', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'secondary_menu_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'secondary_menu_hover_animation' =>  [
                        'label' =>  esc_html__( 'Hover Animation', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'   =>  [
                            'none'  =>  esc_html__( 'None', 'blog-bee' ),
                            'one'   =>  esc_html__( 'Underline from left', 'blog-bee' ),
                        ]
                    ],
                    'secondary_menu_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'secondary_menu_color' =>  [
                        'label' =>  esc_html__( '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( [
                    'secondary_menu_section_tab' =>  'general',
                    'secondary_menu_hover_animation' =>  'none',
                    'secondary_menu_typography'   =>  $this->get_typography( [
                        'font_weight' =>  '500',
                        'font_size'   =>  $this->get_responsive( '15px', '15px', '15px' ),
                    ] ),
                    'secondary_menu_color'  =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                        'hover'   =>  $this->get_color(),
                    ],
                    'secondary_menu_background' =>  $this->get_color( [ 'value' => '' ] ),
                    'secondary_menu_padding'    =>  $this->get_dimension(),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! $this->is_active() ) return [];
                $dynamic_css_args = [
                    'secondary_menu_hover_animation'  =>  [
                        'selector'  =>  '.bbe-secondary-menu',
                        'prefix'    =>  'hover--'
                    ],
                    'secondary_menu_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'secondary_menu_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'secondary_menu_typography' ),
                        'selector'  =>  '.bbe-header .bbe-secondary-menu div > .menu > li > a, .bbe-header .bbe-secondary-menu .menu > ul > li > a',
                    ],
                    'secondary_menu_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'secondary_menu_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'secondary_menu_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-secondary-menu div > .menu > li a, .bbe-light .bbe-header .bbe-secondary-menu .menu > ul > li a',
                        'property'  =>  '--bbe-site-color'
                    ],
                ];
                return $dynamic_css_args;
            }

            /**
             * Render html
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $block_class[] = 'bbe-menu header-widget';
                $block_class[] = 'hover--' . Theme_Defaults::get_theme_option( 'secondary_menu_hover_animation' );

                ?>

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

                        <nav class="secondary-navigation" aria-label="<?php esc_attr_e( 'Secondary navigation', 'blog-bee' )?>">

                            <?php

                                Helpers::get_menu_button( 'bbe-secondary-menu-burger' );

                                wp_nav_menu( [
                                    'theme_location'  =>  'menu-2',
                                ] );

                            ?>

                        </nav>
                        
                    </div><!-- .bbe-secondary-menu -->

                <?php
            }
        }
        Secondary_Menu::get_instance();
    }