<?php
    /**
     * Primary 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\Customizer\Section\Time as Time;
    use BBE\Helpers as Helpers;
    use BBE\Menu_Cutoff_Walker as Menu_Cutoff_Walker;
    use BBE\Page_Cutoff_Walker as Page_Cutoff_Walker;

    use function esc_html__;
    use function absint;
    use function esc_attr;
    use function esc_html_e;
    use function wp_nav_menu;
    use function wp_page_menu;


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

            /**
             * Is section active
             * 
             * @since 1.0.0
             */
            private function is_active(): bool {
                $header_builder = Helpers::is_widget_in_builder( 'header_builder', 'primary-menu' );
                $header_responsive_builder = Helpers::is_widget_in_builder( 'header_responsive_builder', 'primary-menu' );
                $sticky_builder = Helpers::is_widget_in_builder( 'sticky_header_builder', 'primary-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( 'primary_menu_section' );
                $this->add_control( 'primary_menu_section_tab' );
                $this->add_control( 'primary_menu_hover_animation' );
                $this->tab = 'design';
                $this->add_control( 'primary_menu_main_menu_settings' );
                $this->add_control( 'primary_menu_main_menu_typography' );
                $this->add_control( 'primary_menu_main_menu_color' );
                $this->add_control( 'primary_menu_sub_menu_settings' );
                $this->add_control( 'primary_menu_sub_menu_typography' );
                $this->add_control( 'primary_menu_sub_menu_color' );
            }

            /**
             * Settings
             * 
             * @param string $id    The setting id
             * @since 1.0.0
             */
            public function get_settings( $id = '' ): array {
                $settings = [
                    'primary_menu_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'primary_menu_hover_animation' =>  [
                        'sanitize_callback'  =>  [ $this, 'sanitize_select' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'primary_menu_main_menu_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'primary_menu_main_menu_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'primary_menu_main_menu_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'primary_menu_sub_menu_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'primary_menu_sub_menu_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'primary_menu_sub_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 = [
                    'primary_menu_section' =>  [
                        'title' =>  esc_html__( 'Primary Menu', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'primary_menu_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'primary_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' )
                        ]
                    ],
                    'primary_menu_main_menu_settings' =>  [
                        'label' =>  esc_html__( 'Main Menu Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'primary_menu_main_menu_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'primary_menu_main_menu_color' =>  [
                        'label' =>  esc_html__( 'Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'primary_menu_sub_menu_settings' =>  [
                        'label' =>  esc_html__( 'Sub Menu Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'primary_menu_sub_menu_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'primary_menu_sub_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( [
                    'primary_menu_section_tab' =>  'general',
                    'primary_menu_hover_animation' =>  'none',
                    'primary_menu_enable_menu_cutoff' =>  false,
                    'primary_menu_menu_cutoff' =>  5,
                    'primary_menu_main_menu_settings'   =>  true,
                    'primary_menu_main_menu_typography'   =>  $this->get_typography( [
                        'font_weight' =>  '500',
                        'font_size'   =>  $this->get_responsive( '15px', '15px', '15px' ),
                    ] ),
                    'primary_menu_main_menu_color'  =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#000' ] ),
                        'hover'   =>  $this->get_color(),
                    ],
                    'primary_menu_main_menu_background' =>  $this->get_color( [ 'value' => '' ] ),
                    'primary_menu_main_menu_padding'    =>  $this->get_dimension( [ 'top' => 2, 'right' => 12, 'bottom' => 2, 'left' => 12, 'link' => false ] ),
                    'primary_menu_sub_menu_settings'    =>  false,
                    'primary_menu_sub_menu_typography'    =>  $this->get_typography( [
                        'font_weight' =>  '500',
                        'font_size'   =>  $this->get_responsive( '15px', '15px', '15px' ),
                    ] ),
                    'primary_menu_sub_menu_color'   =>  [
                        'initial'   =>  $this->get_color( [ 'value' =>  '#000' ] ),
                        'hover'   =>  $this->get_color(),
                    ],
                    'primary_menu_sub_menu_background'  =>  $this->get_color( [ 'value' => '#fff' ] ),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! $this->is_active() ) return [];
                $dynamic_css_args = [
                    'primary_menu_hover_animation'  =>  [
                        'selector'  =>  '.bbe-primary-menu',
                        'prefix'    =>  'hover--'
                    ],
                    'primary_menu_main_menu_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'primary_menu_main_menu_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'primary_menu_main_menu_typography' ),
                        'selector'  =>  '.bbe-header.site-header .bbe-primary-menu a',
                    ],
                    'primary_menu_main_menu_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'primary_menu_main_menu_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'primary_menu_main_menu_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-primary-menu a',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'primary_menu_sub_menu_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'primary_menu_sub_menu_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'primary_menu_sub_menu_typography' ),
                        'selector'  =>  '.bbe-header .bbe-primary-menu li .sub-menu a',
                    ],
                    'primary_menu_sub_menu_color'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'primary_menu_sub_menu_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'primary_menu_sub_menu_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-primary-menu li .sub-menu 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( 'primary_menu_hover_animation' );

                ?>

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

                        <nav id="site-navigation" class="main-navigation" aria-label="<?php esc_attr_e( 'Primary navigation', 'blog-bee' )?>">

                            <?php
                                Helpers::get_menu_button( 'bbe-primary-menu-burger' );

                                echo wp_nav_menu( [
                                    'theme_location'  =>  'menu-1',
                                    'container_class'    =>  'menu-container',
                                    'walker'    =>  new Menu_Cutoff_Walker(),
                                    'fallback_cb'   =>  [ $this, 'fallback_walker' ]
                                ] );

                            ?>

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

                <?php
            }

            /**
             * Fallback walker
             * 
             * @since 1.0.0
             */
            public function fallback_walker(){
                wp_page_menu( [
                    'theme_location'  =>  'menu-1',
                    'container_class'    =>  'menu-container',
                    'menu_class'    =>  'menu-container menu',
                    'walker'    =>  new Page_Cutoff_Walker()
                ] );
            }
        }
        Primary_Menu::get_instance();
    }