<?php
    /**
     * Off canvas 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 is_sidebar_active;

    if( ! class_exists( __NAMESPACE__ . '\\Off_Canvas' ) ) {
        /**
         * Off Canvas class
         */
        class Off_Canvas extends Section {
            /**
             * Section Id
             * 
             * @since 1.0.0
             * @var string
             */
            public static $id = 'bbe-off-canvas';

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'off_canvas_section' );
                $this->add_control( 'off_canvas_section_tab' );
                $this->add_control( 'off_canvas_redirect' );
                $this->add_control( 'off_canvas_icon_style' );
                $this->tab = 'design';
                $this->add_control( 'off_canvas_icon_settings' );
                $this->add_control( 'off_canvas_icon_color' );
                $this->add_control( 'off_canvas_icon_border_color' );
                $this->add_control( 'off_canvas_background' );
                $this->add_control( 'off_canvas_sidebar_settings' );
                $this->add_control( 'off_canvas_sidebar_heading_typography' );
                $this->add_control( 'off_canvas_sidebar_content_typography' );
                $this->add_control( 'off_canvas_sidebar_heading_color' );
                $this->add_control( 'off_canvas_sidebar_content_color' );
                $this->add_control( 'off_canvas_sidebar_link_color' );
                $this->add_control( 'off_canvas_sidebar_background_color' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'off_canvas_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'off_canvas_icon_style' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_multiselect' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'off_canvas_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'off_canvas_icon_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'off_canvas_icon_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'off_canvas_icon_border_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'off_canvas_background' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'off_canvas_sidebar_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'off_canvas_sidebar_heading_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'off_canvas_sidebar_content_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'off_canvas_sidebar_heading_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'off_canvas_sidebar_content_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'off_canvas_sidebar_link_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'off_canvas_sidebar_background_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $controls = [
                    'off_canvas_section' =>  [
                        'title' =>  esc_html__( 'Off Canvas', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'off_canvas_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'off_canvas_icon_style' =>  [
                        'label' =>  esc_html__( 'Icon Style', 'blog-bee' ),
                        'type'  =>  'multiselect',
                        'fields'  => Helpers::get_customizer_icon_style()
                    ],
                    'off_canvas_redirect' =>  [
                        'label' =>  esc_html__( 'Edit Off Canvas', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'sidebar-widgets-bbe-off-canvas',
                    ],
                    'off_canvas_icon_settings' =>  [
                        'label' =>  esc_html__( 'Icon Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'off_canvas_icon_color' =>  [
                        'label' =>  esc_html__( 'Icon Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'off_canvas_icon_border_color' =>  [
                        'label' =>  esc_html__( 'Border Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'off_canvas_background' =>  [
                        'label' =>  esc_html__( 'Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                        'color_types'   =>  [ 'solid', 'gradient' ]
                    ],
                    'off_canvas_sidebar_settings' =>  [
                        'label' =>  esc_html__( 'Sidebar Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'off_canvas_sidebar_heading_typography' =>  [
                        'label' =>  esc_html__( 'Heading Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'off_canvas_sidebar_content_typography' =>  [
                        'label' =>  esc_html__( 'Content Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'off_canvas_sidebar_heading_color' =>  [
                        'label' =>  esc_html__( 'Heading Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                    ],
                    'off_canvas_sidebar_content_color' =>  [
                        'label' =>  esc_html__( 'Content Color', 'blog-bee' ),
                        'type'  =>  'bbe-color'
                    ],
                    'off_canvas_sidebar_link_color' =>  [
                        'label' =>  esc_html__( 'Link Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'off_canvas_sidebar_background_color' =>  [
                        'label' =>  esc_html__( 'Sidebar Color', 'blog-bee' ),
                        'type'  =>  'bbe-color'
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'off_canvas_section_tab' =>  'general',
                    'off_canvas_icon_picker' =>  $this->get_icon_picker( [ 'value'  =>  'fa-solid fa-bars' ] ),
                    'off_canvas_width' =>  $this->get_responsive( '350px', '300px', '250px' ),
                    'off_canvas_position' =>  'left',
                    'off_canvas_icon_style' =>  null,
                    'off_canvas_redirect' =>  '',
                    'off_canvas_icon_settings'   =>  true,
                    'off_canvas_icon_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#E78371' ] )
                    ],
                    'off_canvas_icon_border_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#000' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#000' ] )
                    ],
                    'off_canvas_background' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#efefef' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#efefef' ] )
                    ],
                    'off_canvas_icon_width_height'  =>  '35px',
                    'off_canvas_icon_size'    =>  $this->get_responsive( '16px', '16px', '16px' ),
                    'off_canvas_sidebar_settings'   =>  false,
                    'off_canvas_sidebar_heading_typography' =>  $this->get_typography( [
                        'font_size'   =>  $this->get_responsive( '26px', '26px', '26px' ),
                        'line_height' =>  $this->get_responsive( 1.4, 1.4, 1.4 )
                    ] ),
                    'off_canvas_sidebar_content_typography' =>  $this->get_typography( [
                        'font_weight' => 'regular',
                        'font_size'   =>  $this->get_responsive( '15px', '15px', '15px' ),
                    ] ),
                    'off_canvas_sidebar_heading_color'  =>  $this->get_color( [ 'value' => '#30344b' ] ),
                    'off_canvas_sidebar_content_color'  =>  $this->get_color( [ 'value' => '#30344b' ] ),
                    'off_canvas_sidebar_link_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#0073aa' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#E78371' ] ),
                    ],
                    'off_canvas_sidebar_background_color'  =>  $this->get_color( [ 'value' => '#fff' ] ),
                    'off_canvas_sidebar_padding' =>  $this->get_dimension( [ 'top' => 20, 'right' => 20, 'bottom' => 20, 'left' => 25, 'link' => false ] ),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! $this->is_active() ) return [];
                $dynamic_args = [
                    'off_canvas_icon_color' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'off_canvas_icon_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'off_canvas_icon_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-off-canvas .icon',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'off_canvas_icon_border_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'off_canvas_icon_border_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'off_canvas_icon_border_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-off-canvas .outline i',
                        'property'  =>  '--bbe-site-border-color'
                    ],
                    'off_canvas_background' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'off_canvas_background' ),
                        'default' =>  Theme_Defaults::get_defaults( 'off_canvas_background' ),
                        'selector'  =>  '.bbe-header .bbe-off-canvas .filled i',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                    'off_canvas_sidebar_heading_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'off_canvas_sidebar_heading_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'off_canvas_sidebar_heading_typography' ),
                        'selector'  =>  '.bbe-header .bbe-off-canvas .widget h1, .bbe-header .bbe-off-canvas .widget h2, .bbe-header .bbe-off-canvas .widget h3, .bbe-header .bbe-off-canvas .widget h4, .bbe-header .bbe-off-canvas .widget h5, .bbe-header .bbe-off-canvas .widget h6',
                    ],
                    'off_canvas_sidebar_content_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'off_canvas_sidebar_content_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'off_canvas_sidebar_content_typography' ),
                        'selector'  =>  '.bbe-header .bbe-off-canvas .widget',
                    ],
                    'off_canvas_sidebar_heading_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'off_canvas_sidebar_heading_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'off_canvas_sidebar_heading_color' ),
                        'selector'  =>  '.bbe-header .bbe-off-canvas .widget h1, .bbe-header .bbe-off-canvas .widget h2, .bbe-header .bbe-off-canvas .widget h3, .bbe-header .bbe-off-canvas .widget h4, .bbe-header .bbe-off-canvas .widget h5, .bbe-header .bbe-off-canvas .widget h6',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'off_canvas_sidebar_content_color'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'off_canvas_sidebar_content_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'off_canvas_sidebar_content_color' ),
                        'selector'  =>  '.bbe-header .bbe-off-canvas .widget',
                        'property'  =>  '--bbe-site-excerpt-color'
                    ],
                    'off_canvas_sidebar_link_color'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'off_canvas_sidebar_link_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'off_canvas_sidebar_link_color' ),
                        'selector'  =>  '.bbe-header .bbe-off-canvas .widget a',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'off_canvas_sidebar_background_color'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'off_canvas_sidebar_background_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'off_canvas_sidebar_background_color' ),
                        'selector'  =>  '.bbe-header .bbe-off-canvas .off-canvas-sidebar',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Selective refresh partial
             * 
             * @since 1.0.0
             */
            public function render_icon() {
                $icon = Theme_Defaults::get_theme_option( 'off_canvas_icon_picker' );
                $icon[ 'class' ] = 'canvas-icon ' . Theme_Defaults::get_theme_option( 'off_canvas_icon_style' );
                $icon[ 'aria_label' ] = esc_html__( 'Open Sidebar', 'blog-bee' );
                echo Helpers::get_icon_html( $icon );
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $block_class[] = 'position--' . Theme_Defaults::get_theme_option( 'off_canvas_position' );

                $close_button_attrs = [
                    'type'  =>  'icon',
                    'value' =>  'fa-solid fa-xmark',
                    'class' =>  'close-icon',
                    'aria_label'    =>  esc_html__( 'Close Sidebar', 'blog-bee' )
                ];

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

                        <?php

                            $this->render_icon();

                            echo '<div class="overlay"></div>';

                            echo '<div class="off-canvas-sidebar">';
                            
                                echo Helpers::get_icon_html( $close_button_attrs );

                                if( is_active_sidebar( self::$id ) ) :

                                    echo '<div class="widgets-wrap">';

                                        dynamic_sidebar( self::$id );

                                    echo '</div>';

                                endif;

                            echo '</div>';
                            
                        ?>

                    </div><!-- .bbe-off-canvas  -->
                <?php
            }
        }
        Off_Canvas::get_instance();
    }