<?php
    /**
     * Search Page Section
     * 
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Customizer\Section;

    use BBE\Helpers as Helpers;
    use BBE\Customizer\Section\Section as Section;
    use BBE\Customizer\Theme_Defaults as Theme_Defaults;
    use BBE\Customizer\Section\Pagination as Pagination;

    use function esc_html__;
    use function get_search_query;
    use function get_search_form;
    use function esc_html;
    use function is_search;
    use function get_categories;
    use function get_tags;
    use function esc_html_e;
    use function esc_attr;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->add_section( 'search_page_section' );
                $this->add_control( 'search_page_form_enable' );
                $this->add_control( 'search_page_title' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'search_page_form_enable' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'search_page_title' =>  [
                        'sanitize_callback' =>  'sanitize_text_field',
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $controls = [
                    'search_page_section' =>  [
                        'title' =>  esc_html__( 'Search Page', 'blog-bee' ),
                        'panel' =>  'blog_panel',
                        'priority'  =>  40,
                    ],
                    'search_page_form_enable' =>  [
                        'label' =>  esc_html__( 'Enable Search Form', 'blog-bee' ),
                        'type'  =>  'toggle-button',
                    ],
                    'search_page_title' =>  [
                        'label' =>  esc_html__( 'Search Page Title', 'blog-bee' ),
                        'type'  =>  'bbe-text',
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'search_page_form_enable' =>  true,
                    'search_page_filter_enable' =>  true,
                    'search_page_title' =>  'Search Results For: ',
                    'search_page_nothing_found_title' =>  'Nothing Found.',
                    'search_page_nothing_found_content' =>  'Sorry, but nothing matched your search terms. Please try again with some different keywords.',
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args() {
                $dynamic_css = [
                    'search_page_title' =>  [
                        'selector'  =>  'body.search .blog-bee-main-wrap .page-header .page-title .prefix',
                        'parent'    =>  'body.search .blog-bee-main-wrap .page-header .page-title '
                    ],
                ];
                return $dynamic_css;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {

            }

            /**
             * Content to display when no posts is found on search page
             * 
             * @since 1.0.0
             */
            public function get_nothing_found_content() {
                $content = Theme_Defaults::get_theme_option( 'search_page_nothing_found_content' );
                echo '<span class="label">' . esc_html( $content ) . '</span>';
            }

            /**
             * Content header to display when no posts is found on any page
             * 
             * @param bool $have_posts Whether or not posts exists
             * @since 1.0.0
             */
            public function get_page_header( $have_posts = false ) {
                $show_form = Theme_Defaults::get_theme_option( 'search_page_form_enable' );
                if( $have_posts ) {
                    $title_text = Theme_Defaults::get_theme_option( 'search_page_title' );
                    $search_query = '<span>' . get_search_query() . '</span>';
                    $title = sprintf( '%s %s', '<span class="prefix">' . $title_text . '</span>', $search_query );
                } else {
                    $title = Theme_Defaults::get_theme_option( 'search_page_nothing_found_title' );
                }
                ?>

                    <header class="page-header">

                        <h1 class="page-title"><?php echo wp_kses_post( $title ); ?></h1>

                        <?php if( $show_form && is_search() && $have_posts ) get_search_form(); ?>

                    </header><!-- .page-header -->

                <?php
            }

            /**
             * Render search filter block
             * 
             * @since 1.0.0
             */
            public function get_search_filter_block() {
                $filter_enable = Theme_Defaults::get_theme_option( 'search_page_filter_enable' );
                if( ! $filter_enable ) return;
                $term_args = [
                    'fields'	=>	'id=>name',
                    'hide_empty'	=>	true
                ];
                $categories = get_categories( $term_args );
                $tags = get_tags( $term_args );
                $user_query = get_users([
                    'fields'    =>  [ 'ID', 'display_name' ]
                ]);
                $post_types = [
                    'post'	=>	esc_html__( 'Posts', 'blog-bee' ),
                    'page'	=>	esc_html__( 'Pages', 'blog-bee' ),
                ];
                ?>
                    <div class="filter-wrapper">
                        <div class="filter has-dropdown type">
                            <div class="head">
                                <span class="label"><?php esc_html_e( 'Types', 'blog-bee' ); ?></span>
                                <span class="icon"><i class="fa-solid fa-angle-down"></i></span>
                            </div>
                            <div class="body">
                                <?php
                                    if( is_array( $post_types ) && ! empty( $post_types ) ) :
                                        echo '<div class="items">';
                                        foreach( $post_types as $key => $type ) :
                                            ?>
                                                <div class="item">
                                                    <input type="checkbox" name="type-<?php echo esc_attr( $key ); ?>" id="type-<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $key ); ?>">
                                                    <label for="type-<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $type ); ?></label>
                                                </div>
                                            <?php
                                        endforeach;
                                        echo '</div>';
                                    endif;
                                ?>
                                <button class="clear"><?php esc_html_e( 'Clear', 'blog-bee' ); ?></button>
                            </div>
                        </div>
                        <div class="filter has-dropdown authors">
                            <div class="head">
                                <span class="label"><?php esc_html_e( 'Authors', 'blog-bee' ); ?></span>
                                <span class="icon"><i class="fa-solid fa-angle-down"></i></span>
                            </div>
                            <div class="body">
                                <?php
                                    if( is_array( $user_query ) && ! empty( $user_query ) ) :
                                        echo '<div class="items">';
                                        foreach( $user_query as $user ) :
                                            $user_id = $user->ID;
                                            ?>
                                                <div class="item">
                                                    <input type="checkbox" name="user-<?php echo esc_attr( $user_id ); ?>" id="user-<?php echo esc_attr( $user_id ); ?>" value="<?php echo esc_attr( $user_id ); ?>">
                                                    <label for="user-<?php echo esc_attr( $user_id ); ?>"><?php echo esc_html( $user->display_name ); ?></label>
                                                </div>
                                            <?php
                                        endforeach;
                                        echo '</div>';
                                    endif;
                                ?>
                                <button class="clear"><?php esc_html_e( 'Clear', 'blog-bee' ); ?></button>
                            </div>
                        </div>
                        <div class="filter has-dropdown categories">
                            <div class="head">
                                <span class="label"><?php esc_html_e( 'Categories', 'blog-bee' ); ?></span>
                                <span class="icon"><i class="fa-solid fa-angle-down"></i></span>
                            </div>
                            <div class="body">
                                <?php
                                    if( is_array( $categories ) && ! empty( $categories ) ) :
                                        echo '<div class="items">';
                                        foreach( $categories as $id => $name ) :
                                            ?>
                                                <div class="item">
                                                    <input type="checkbox" name="cat-<?php echo esc_attr( $id ); ?>" id="cat-<?php echo esc_attr( $id ); ?>" value="<?php echo esc_attr( $id ); ?>">
                                                    <label for="cat-<?php echo esc_attr( $id ); ?>"><?php echo esc_html( $name ); ?></label>
                                                </div>
                                                <?php
                                        endforeach;
                                        echo '</div>';
                                    endif;
                                    ?>
                                <button class="clear"><?php esc_html_e( 'Clear', 'blog-bee' ); ?></button>
                            </div>
                        </div>
                        <div class="filter has-dropdown tags">
                            <div class="head">
                                <span class="label"><?php esc_html_e( 'Tags', 'blog-bee' ); ?></span>
                                <span class="icon"><i class="fa-solid fa-angle-down"></i></span>
                            </div>
                            <div class="body">
                                <?php
                                    if( is_array( $tags ) && ! empty( $tags ) ) :
                                        echo '<div class="items">';
                                        foreach( $tags as $id => $name ) :
                                            ?>
                                                <div class="item">
                                                    <input type="checkbox" name="tag-<?php echo esc_attr( $id ); ?>" id="tag-<?php echo esc_attr( $id ); ?>" value="<?php echo esc_attr( $id ); ?>">
                                                    <label for="tag-<?php echo esc_attr( $id ); ?>"><?php echo esc_html( $name ); ?></label>
                                                </div>
                                                <?php
                                        endforeach;
                                        echo '</div>';
                                    endif;
                                    ?>
                                <button class="clear"><?php esc_html_e( 'Clear', 'blog-bee' ); ?></button>
                            </div>
                        </div>
                        <div class="filter before">
                            <input type="text" name="before" id="before" placeholder="Before">
                        </div>
                        <div class="filter after">
                            <input type="text" name="after" id="after" placeholder="After">
                        </div>
                        <button class="action-btn filter-button disabled"><?php esc_html_e( 'Filter', 'blog-bee' ); ?></button>
                        <button class="action-btn clear-button disabled"><?php esc_html_e( 'Clear', 'blog-bee' ); ?></button>
                    </div>
                <?php
            }

            /**
             * Get search page pagination
             * 
             * @since 1.0.0
             */
            public function get_pagination() {
                $filter_enable = Theme_Defaults::get_theme_option( 'search_page_filter_enable' );
                if( $filter_enable ) {
                    Pagination::get_instance()->ajax_button();
                } else {
                    Pagination::get_instance()->render_html();
                }
            }
        }
        Search_Page::get_instance();
    }