<?php
    /**
     * Search Icon 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\Customizer\Section\Live_Search as Live_Search;
    use BBE\Helpers as Helpers;

    use function esc_html__;
    use function get_template_directory_uri;
    use function esc_attr;
    use function get_search_form;

    if( ! class_exists( __NAMESPACE__ . '\\Search_Icon' ) ) {
        /**
         * Search_Icon Class
         */
        class Search_Icon extends Section {
            /**
             * Section id
             * 
             * @since 1.0.0 
             */
            public static $id = 'bbe-search-icon';

            /**
             * Layout
             * 
             * @since 1.0.0
             */
            private $layout = null;

            /**
             * Layout getter
             * 
             * @since 1.0.0
             */
            public function get_layout() {
                if( $this->layout === null ) {
                    $this->layout = Theme_Defaults::get_theme_option( 'search_icon_layouts' );
                }
                return $this->layout;
            }

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'search_icon_section' );
                $this->add_control( 'search_icon_section_tab' );
                $this->add_control( 'search_icon_style' );
                $this->add_control( 'search_icon_enable_live_search' );
                $this->add_control( 'search_icon_live_search_redirect' );
                $this->tab = 'design';
                $this->add_control( 'search_icon_color' );
                $this->add_control( 'search_icon_background' );
                $this->add_control( 'search_icon_border_color' );
            }

            /**
             * Settings
             * 
             * @param string $id    The setting id
             * @since 1.0.0
             */
            public function get_settings( $id = '' ): array {
                $settings = [
                    'search_icon_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'search_icon_style' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_multiselect' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'search_icon_enable_live_search' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'search_icon_live_search_redirect' =>  [
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'search_width' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'search_icon_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'search_icon_background' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'search_icon_border_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 = [
                    'search_icon_section' =>  [
                        'title' =>  esc_html__( 'Search Icon', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'search_icon_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'search_icon_style' =>  [
                        'label' =>  esc_html__( 'Icon Style', 'blog-bee' ),
                        'type'  =>  'multiselect',
                        'fields'  => Helpers::get_customizer_icon_style()
                    ],
                    'search_icon_enable_live_search' =>  [
                        'label' =>  esc_html__( 'Enable Live Search', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'search_icon_live_search_redirect' =>  [
                        'label' =>  esc_html__( 'Head to Live Search', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'live_search_section',
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'search_icon_enable_live_search',
                                    'operator'  =>  '==',
                                    'value' =>  true
                                ]
                            ]
                        ]
                    ],
                    'search_icon_color' =>  [
                        'label' =>  esc_html__( 'Icon Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'search_icon_background' =>  [
                        'label' =>  esc_html__( 'Icon Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                        'color_types'   =>  [ 'solid', 'gradient' ]
                    ],
                    'search_icon_border_color' =>  [
                        'label' =>  esc_html__( 'Border 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( [
                    'search_icon_section_tab' =>  'general',
                    'search_icon_enable_live_search' =>  true,
                    'search_icon_live_search_redirect' =>  '',
                    'search_icon_style' =>  '',
                    'search_icon_layouts' =>  'one',
                    'search_icon_icon_picker' =>  $this->get_icon_picker( [ 'value'  =>  'fa-solid fa-magnifying-glass' ] ),
                    'search_icon_placeholder_text' =>  'Search...',
                    'search_width' =>  '250px',
                    'search_enable_live_search' =>  false,
                    'search_result_count' =>  4,
                    'search_show_featured_image' =>  true,
                    'search_show_all_results_button' =>  true,
                    'search_icon_color'  =>  $this->get_color( [
                        'initial'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                        'hover'   =>  $this->get_color(),
                    ] ),
                    'search_icon_background' =>  $this->get_color( [
                        'initial'   =>  $this->get_color( [ 'value' => '#efefef' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#efefef' ] ),
                    ] ),
                    'search_icon_border_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#efefef' ] ),
                        'hover'   =>  $this->get_color(),
                    ],
                    'search_icon_width_height'  =>  '35px',
                    'search_icon_size'    =>  $this->get_responsive( '16px', '16px', '16px' ),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! $this->is_active() ) return [];
                $dynamic_css_args = [
                    'search_icon_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'search_icon_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'search_icon_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-search-icon i, .bbe-light .bbe-header .bbe-search-icon .icon svg',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'search_icon_background' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'search_icon_background' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'search_icon_background' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-search-icon.filled i',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                    'search_icon_border_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'search_icon_border_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'search_icon_border_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-search-icon.outline i',
                        'property'  =>  '--bbe-site-border-color'
                    ],
                ];
                return $dynamic_css_args;
            }

            /**
             * Render html
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $live_search = Theme_Defaults::get_theme_option( 'search_icon_enable_live_search' );
                $block_class[] = self::$id;
                $block_class[] = 'header-widget';
                $block_class[] = Theme_Defaults::get_theme_option( 'search_icon_style' );
                $is_layout_one = ( $this->get_layout() === 'one' );
                if( $is_layout_one ) $block_class[] = 'layout--one';
                $icon = Theme_Defaults::get_theme_option( 'search_icon_icon_picker' );
                $icon[ 'aria_label' ] = esc_html__( 'Open Search', 'blog-bee' );

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

                    echo Helpers::get_icon_html( $icon );

                    if( $is_layout_one ) $this->get_form_wrapper();

                echo '</div><!-- .bbe-search-icon -->';
            }

            /**
             * Get form wrapper
             * 
             * @since 1.0.0
             */
            public function get_form_wrapper( $args = [] ) {
                $live_search = Theme_Defaults::get_theme_option( 'search_icon_enable_live_search' );
                $form_wrapper_class[] = 'form-wrapper';
                $form_wrapper_class[] = 'layout--' . $this->get_layout();
                if( isset( $args[ 'class' ] ) ) {
                    $class = $args[ 'class' ];
                    if( $class ) $form_wrapper_class[] = $class;
                }

                $placeholder = Theme_Defaults::get_theme_option( 'search_icon_placeholder_text' );

                $is_layout_one = in_array( $this->get_layout(), [ 'one' ] );

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

                    if( ! $is_layout_one ) echo '<div class="bbe-container">';

                        echo get_search_form( [ 'placeholder' => $placeholder ] );

                        if( $live_search ) Live_Search::get_instance()->render_html();

                    if( ! $is_layout_one ) echo '</div>';

                echo '</div>';
            }
        }
        Search_Icon::get_instance();
    }