<?php
    /**
     * Header html 1 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_kses_post;

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

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'header_html_one_section' );
                $this->add_control( 'header_html_one_section_tab' );
                $this->add_control( 'header_html_one_editor' );
                $this->tab = 'design';
                $this->add_control( 'header_html_one_typography' );
                $this->add_control( 'header_html_one_text_color' );
                $this->add_control( 'header_html_one_link_color' );
                $this->add_control( 'header_html_one_margin' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'header_html_one_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'header_html_one_editor' =>  [
                        'sanitize_callback' =>  'wp_kses_post',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'header_html_one_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'header_html_one_text_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'header_html_one_link_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'header_html_one_margin' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_dimension' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $controls = [
                    'header_html_one_section' =>  [
                        'title' =>  esc_html__( 'Header HTML 1', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'header_html_one_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'header_html_one_editor' =>  [
                        'label' =>  esc_html__( 'Editor', 'blog-bee' ),
                        'type'  =>  'editor-control',
                    ],
                    'header_html_one_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'header_html_one_text_color' =>  [
                        'label' =>  esc_html__( 'Text Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                    ],
                    'header_html_one_link_color' =>  [
                        'label' =>  esc_html__( 'Link Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'header_html_one_margin' =>  [
                        'label' =>  esc_html__( 'Margin', 'blog-bee' ),
                        'type'  =>  'dimension',
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'header_html_one_section_tab' =>  'general',
                    'header_html_one_editor'    =>  wp_kses_post( '<p>Insert HTML here.</p>' ),
                    'header_html_one_typography'    =>  $this->get_typography( [
                        'font_weight' =>  '500',
                        'font_size'   =>  $this->get_responsive( '15px', '15px', '15px' )
                    ] ),
                    'header_html_one_text_color'    =>  $this->get_color( [ 'value' => '#30344b' ] ),
                    'header_html_one_link_color'    =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#0073aa' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#E78371' ] )
                    ],
                    'header_html_one_margin'    =>  $this->get_dimension(),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! $this->is_active() ) return [];
                $dynamic_args = [
                    'header_html_one_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'header_html_one_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'header_html_one_typography' ),
                        'selector'  =>  '.bbe-header .bbe-header-html-1 p',
                    ],
                    'header_html_one_text_color' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'header_html_one_text_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'header_html_one_text_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-header-html-1 p',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'header_html_one_link_color' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'header_html_one_link_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'header_html_one_link_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-header-html-1 a',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'header_html_one_margin' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'header_html_one_margin' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'header_html_one_margin' ),
                        'selector'  =>  '.bbe-header .bbe-header-html-1',
                        'property'  =>  'margin'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $block_class[] = 'bbe-builder-html';

                ?>
                    <div class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>">
                        <?php
                            echo wp_kses_post( Theme_Defaults::get_theme_option( 'header_html_one_editor' ) );
                        ?>
                    </div>
                <?php
            }
        }
        Header_Html_1::get_instance();
    }