<?php
    /**
     * Footer 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__ . '\\Footer_Menu' ) ) {
        /**
         * Footer_Menu Class
         */
        class Footer_Menu extends Section {
            /**
             * Section id
             * 
             * @since 1.0.0 
             */
            public static $id = 'bbe-footer-menu';

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'footer_menu_section' );
                $this->add_control( 'footer_menu_section_tab' );
                $this->add_control( 'footer_menu_hover_animation' );
                $this->tab = 'design';
                $this->add_control( 'footer_menu_main_menu_typography' );
                $this->add_control( 'footer_menu_main_menu_color' );
            }

            /**
             * Settings
             * 
             * @param string $id    The setting id
             * @since 1.0.0
             */
            public function get_settings( $id = '' ): array {
                $settings = [
                    'footer_menu_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'footer_menu_hover_animation' =>  [
                        'sanitize_callback'  =>  [ $this, 'sanitize_select' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'footer_menu_main_menu_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'footer_menu_main_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 = [
                    'footer_menu_section' =>  [
                        'title' =>  esc_html__( 'Footer Menu', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return Helpers::is_widget_in_builder( 'footer_builder', 'footer-menu' );
                        },
                        'section_to_open' =>  'footer_builder_section'
                    ],
                    'footer_menu_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'footer_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' ),
                        ]
                    ],
                    'footer_menu_main_menu_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'footer_menu_main_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( [
                    'footer_menu_section_tab' =>  'general',
                    'footer_menu_hover_animation' =>  'none',
                    'footer_menu_main_menu_typography'   =>  $this->get_typography( [
                        'font_weight' =>  '500',
                        'font_size'   =>  $this->get_responsive( '15px', '15px', '15px' ),
                    ] ),
                    'footer_menu_main_menu_color'  =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#000' ] ),
                        'hover'   =>  $this->get_color(),
                    ],
                    'footer_menu_main_menu_background' =>  $this->get_color(  [ 'value' => '' ] ),
                    'footer_menu_main_menu_padding'    =>  $this->get_dimension( [ 'top' => 2, 'right' => 12, 'bottom' => 2, 'left' => 12, 'link' => false ] ),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! Helpers::is_widget_in_builder( 'footer_builder', 'footer-menu' ) ) return [];
                $dynamic_css_args = [
                    'footer_menu_hover_animation'  =>  [
                        'selector'  =>  '.bbe-footer-menu',
                        'prefix'    =>  'hover--'
                    ],
                    'footer_menu_main_menu_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'footer_menu_main_menu_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'footer_menu_main_menu_typography' ),
                        'selector'  =>  '.bbe-footer .bbe-footer-menu li a',
                    ],
                    'footer_menu_main_menu_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'footer_menu_main_menu_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'footer_menu_main_menu_color' ),
                        'selector'  =>  '.bbe-footer .bbe-footer-menu 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[] = 'footer-widget bbe-menu';
                $block_class[] = 'hover--' . Theme_Defaults::get_theme_option( 'footer_menu_hover_animation' );

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

                    wp_nav_menu( [
                        'theme_location'  =>  'menu-3',
                        'depth' =>  1
                    ] );
                    
                echo '</div><!-- .bbe-footer-menu -->';
            }
        }
        Footer_Menu::get_instance();
    }