<?php
    /**
     * Mobile 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 esc_url;
    use function wp_get_attachment_image;

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

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'mobile_canvas_section' );
                $this->add_control( 'mobile_canvas_section_tab' );
                $this->add_control( 'mobile_canvas_horizontal_alignment' );
                $this->tab = 'design';
                $this->add_control( 'header_responsive_row_full_width' );
                $this->add_control( 'mobile_canvas_icon_color' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'mobile_canvas_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'mobile_canvas_horizontal_alignment' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'header_responsive_row_full_width' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'mobile_canvas_icon_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 = [
                    'mobile_canvas_section' =>  [
                        'title' =>  esc_html__( 'Mobile Canvas', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'mobile_canvas_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'mobile_canvas_horizontal_alignment' =>  [
                        'label' =>  esc_html__( 'Horizontal Alignment', 'blog-bee' ),
                        'type'  =>  'radio-tab',
                        'fields'    =>  Helpers::get_customizer_alignments()
                    ],
                    'header_responsive_row_full_width' =>  [
                        'label' =>  esc_html__( 'Width', 'blog-bee' ),
                        'type'  =>  'radio-tab',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Full Width', 'blog-bee' ),
                                'value' =>  'on'
                            ],
                            [
                                'label' =>  esc_html__( 'Boxed', 'blog-bee' ),
                                'value' =>  'off'
                            ],
                        ]
                    ],
                    'mobile_canvas_icon_color' =>  [
                        'label' =>  esc_html__( 'Icon Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'mobile_canvas_section_tab' =>  'general',
                    'mobile_canvas_popup' =>  'dropdown',
                    'mobile_canvas_horizontal_alignment' =>  'left',
                    'mobile_canvas_vertical_alignment' =>  'middle',
                    'header_responsive_row_full_width' =>  'off',
                    'mobile_canvas_icon_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                        'hover'   =>  $this->get_color()
                    ],
                    'mobile_canvas_padding' =>  $this->get_dimension( [ 'top' => 25, 'right' => 25, 'bottom' => 25, 'left' => 25 ] ),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! $this->is_active() ) return [];
                $dynamic_args = [
                    'mobile_canvas_horizontal_alignment'   =>  [
                        'selector'  =>  '.bbe--responsive > .bbe-mobile-canvas',
                        'prefix'  =>  'align--'
                    ],
                    'header_responsive_row_full_width' =>  [
                        'selector'  =>  '.bbe--responsive > .bbe-mobile-canvas',
                        'prefix'    =>  'full-width--'
                    ],
                    'mobile_canvas_icon_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'mobile_canvas_icon_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'mobile_canvas_icon_color' ),
                        'selector'  =>  '.bbe-header .bbe-mobile-canvas i',
                        'property'  =>  '--bbe-site-color'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $icon_attrs = [
                    'type'  =>  'icon',
                    'value' =>  'fa-solid fa-bars',
                    'aria_label'    =>  esc_html__( 'Close Sidebar', 'blog-bee' )
                ];
                ?>

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

                        <?php echo Helpers::get_icon_html( $icon_attrs ); ?>      

                    </div><!-- .bbe-header-image  -->

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