<?php
    /**
     * Site Logo 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 is_home;
    use function is_front_page;
    use function esc_attr;
    use function the_custom_logo;
    use function esc_url;
    use function home_url;
    use function wp_get_attachment_image;
    use function esc_html;
    use function get_bloginfo;

    if( ! class_exists( __NAMESPACE__ . '\\Site_Logo' ) ) {
        /**
         * Site Logo class
         */
        class Site_Logo extends Section {
            /**
             * Section Id
             * 
             * @since 1.0.0
             * @var string
             */
            public static $id = 'bbe-site-logo';

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $section = $this->manager->get_section( 'title_tagline' );
                $this->manager->remove_section( 'title_tagline' );
                $this->tab = 'general';
                $this->add_section( 'title_tagline' );
                $this->manager->get_section( 'title_tagline' )->title = $section->title;
                $this->manager->get_section( 'title_tagline' )->priority = $section->priority;
                $this->add_control( 'site_logo_section_tab' );
                $this->add_control( 'site_logo_image_settings' );
                $this->manager->remove_control( 'header_textcolor' );
                $this->manager->get_control( 'custom_logo' )->priority = 6;
                $this->manager->get_control( 'site_icon' )->priority = 7;
                $this->add_control( 'site_logo_label_settings' );
                $this->manager->get_control( 'display_header_text' )->label = esc_html__( 'Display Site Title', 'blog-bee' );
                $this->manager->get_control( 'display_header_text' )->priority = 10;
                $this->add_control( 'site_tagline_option' );
                $this->tab = 'design';
                $this->add_control( 'site_title_typography' );
                $this->add_control( 'site_description_typography' );
                $this->add_control( 'site_title_color' );
                $this->add_control( 'site_description_color' );
                $this->add_control( 'site_logo_width' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'site_logo_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'site_logo_image_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'site_logo_label_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'site_tagline_option' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'transport'   =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'site_title_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'site_description_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'site_title_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'site_description_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'site_logo_width' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_range' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $controls = [
                    'title_tagline' =>  [
                        'section_to_open' =>  'header_builder_section',
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        }
                    ],
                    'site_logo_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'priority'  =>  5,
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'site_logo_image_settings' =>  [
                        'label' =>  esc_html__( 'Image Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle',
                        'priority'  =>  5
                    ],
                    'site_logo_label_settings' =>  [
                        'label' =>  esc_html__( 'Label Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle',
                        'priority'  =>  8
                    ],
                    'site_tagline_option' =>  [
                        'label' =>  esc_html__( 'Display Tagline', 'blog-bee' ),
                        'type'  =>  'checkbox',
                    ],
                    'site_title_typography' =>  [
                        'label' =>  esc_html__( 'Site Title Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'site_description_typography' =>  [
                        'label' =>  esc_html__( 'Site Description Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'site_title_color' =>  [
                        'label' =>  esc_html__( 'Site Title', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'site_description_color' =>  [
                        'label' =>  esc_html__( 'Site Description', 'blog-bee' ),
                        'type'  =>  'bbe-color'
                    ],
                    'site_logo_width' =>  [
                        'label' =>  esc_html__( 'Logo Width', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'responsive'    =>  true,
                        'input_attrs'   =>  [
                            'px'    =>  [
                                'min'   =>  100,
                                'max'   =>  500,
                                'step'  =>  1
                            ],
                            '%'    =>  [
                                'min'   =>  0,
                                'max'   =>  100,
                                'step'  =>  1
                            ],
                            'em'    =>  [
                                'min'   =>  0,
                                'max'   =>  2,
                                'step'  =>  0.1
                            ],
                            'rem'    =>  [
                                'min'   =>  0,
                                'max'   =>  2,
                                'step'  =>  0.1
                            ]
                        ]
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'site_logo_section_tab' =>  'general',
                    'site_logo_image_settings' =>  true,
                    'site_logo_label_settings' =>  false,
                    'site_logo_dark' =>  0,
                    'site_tagline_option' =>  true,
                    'site_title_enable_highlight' =>  false,
                    'site_title_highlight_text' =>  '',
                    'site_title_typography' =>  $this->get_typography( [
                        'font_weight'  =>  '600',
                        'font_size'  =>  $this->get_responsive( '26px', '26px', '26px' ),
                        'line_height'    =>  $this->get_responsive( 1.4, 1.4, 1.4 ),
                    ] ),
                    'site_description_typography' =>  $this->get_typography(),
                    'site_title_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#141414' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '--bbe-theme-color' ] ),
                    ],
                    'site_description_color' =>  $this->get_color( [ 'value' => '#424242' ] ),
                    'site_logo_width' =>  $this->get_responsive( '210px', '210px', '210px' ),
                    'site_title_highlight_color' =>  [
                        'initial'   =>  $this->get_color(),
                        'hover'   =>  $this->get_color( [ 'value' => '#EEEEEE' ] ),
                    ],
                    'site_title_highlight_settings' =>  false,
                    'site_title_highlight_typography' =>  $this->get_typography( [
                        'font_size'  =>  $this->get_responsive( '26px', '26px', '26px' ),
                        'line_height'    =>  $this->get_responsive( 1.4, 1.4, 1.4 ),
                    ] ),
                    'site_title_highlight_color' =>  [
                        'initial'   =>  $this->get_color(),
                        'hover'   =>  $this->get_color(),
                    ],
                    'site_title_highlight_background' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#ffffff00' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#ffffff00' ] ),
                    ],
                    'site_title_highlight_border_radius' =>  '0px',
                    'site_title_highlight_padding' =>  $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 = [
                    'site_title_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'site_title_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'site_title_typography' ),
                        'selector'  =>  '.bbe-header .bbe-site-logo .site-title, .bbe-header .bbe-site-logo .site-title-link',
                    ],
                    'site_description_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'site_description_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'site_description_typography' ),
                        'selector'  =>  '.bbe-header .bbe-site-logo .site-description',
                    ],
                    'site_title_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'site_title_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'site_title_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-site-logo .site-title-link',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'site_description_color'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'site_description_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'site_description_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-site-logo .site-description',
                        'property'  =>  '--bbe-site-description-color'
                    ],
                    'site_logo_width'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'site_logo_width' ),
                        'default' =>  Theme_Defaults::get_defaults( 'site_logo_width' ),
                        'selector'  =>  '.bbe-header .bbe-site-logo .custom-logo',
                        'property'  =>  'width'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $block_class[] = 'header-widget';
                $title_tag = ( is_home() || is_front_page() ) ? 'h1' : 'p';

                $dark_logo = Theme_Defaults::get_theme_option( 'site_logo_dark' );
                $tagline_option = Theme_Defaults::get_theme_option( 'site_tagline_option' );

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

                    the_custom_logo();

                    if( $dark_logo ) echo '<a href="'. esc_url( home_url() ) .'" class="dark-custom-logo-link">'. wp_get_attachment_image( $dark_logo, 'large', false, [ 'class' => 'custom-logo' ] ) .'</a>';

                    if( display_header_text() ) :

                        echo '<'. esc_html( $title_tag ) .' class="site-title"><a href="'. esc_url( home_url() ) .'" rel="home" class="site-title-link">'. $this->get_site_title() .'</a></'. esc_html( $title_tag ) .'>';
    
                    endif;
                    
                    if( $tagline_option ) echo '<p class="site-description">'. esc_html( get_bloginfo( 'description' ) ) .'</p>';

                echo '</div><!-- .bbe-site-logo -->';
            }

            /**
             * Get site title
             * 
             * @since 1.0.0
             */
            private function get_site_title() {
                if( Theme_Defaults::get_theme_option( 'site_title_enable_highlight' ) ) {
                    $text_to_highlight = Theme_Defaults::get_theme_option( 'site_title_highlight_text' );
                    $full_title = get_bloginfo( 'title' );
                    $regex = preg_split( '/('. $text_to_highlight .')/', $full_title, 2, PREG_SPLIT_DELIM_CAPTURE );
                    $title = [];
                    foreach( $regex as $index => $item ) {
                        $title_class = 'site-title-partial';
                        if( $index === 1 ) $title_class .= ' highlight';
                        if( $item ) $title[] = '<span class="' . esc_attr( $title_class ) . '">' .  esc_html( $item ) . '</span>';
                    }
                    return implode( '', $title );
                } else {
                    return esc_html( get_bloginfo( 'title' ) );
                }
            }
        }
        Site_Logo::get_instance();
    }