<?php
    /**
     * Pagination 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;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'pagination_section' );
                $this->add_control( 'pagination_section_tab' );
                $this->add_control( 'pagination_type' );
                $this->add_control( 'pagination_style' );
                $this->tab = 'design';
                $this->add_control( 'pagination_color' );
                $this->add_control( 'pagination_background' );
                $this->add_control( 'pagination_border_color' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'pagination_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'pagination_type' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'pagination_style' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_multiselect' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'pagination_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'pagination_background' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'pagination_border_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 = [
                    'pagination_section' =>  [
                        'title' =>  esc_html__( 'Archive Pagination', 'blog-bee' ),
                        'priority'  =>  50,
                        'panel' =>  'blog_panel'
                    ],
                    'pagination_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'pagination_type' =>  [
                        'label' =>  esc_html__( 'Type', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  [
                            'default'   =>  esc_html__( 'Default', 'blog-bee' ),
                            'number'   =>  esc_html__( 'Number', 'blog-bee' ),
                        ]
                    ],
                    'pagination_style' =>  [
                        'label' =>  esc_html__( 'Style', 'blog-bee' ),
                        'type'  =>  'multiselect',
                        'fields' =>  Helpers::get_customizer_icon_style(),
                    ],
                    'pagination_color' =>  [
                        'label' =>  esc_html__( 'Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                    ],
                    'pagination_background' =>  [
                        'label' =>  esc_html__( 'Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                        'color_types'   =>  [ 'solid', 'gradient' ]
                    ],
                    'pagination_border_color' =>  [
                        'label' =>  esc_html__( 'Border 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( [
                    'pagination_section_tab' =>  'general',
                    'pagination_type'   =>  'default',
                    'pagination_style'   =>  '',
                    'pagination_ajax_label'   =>  'Load More',
                    'pagination_ajax_no_results_label'   =>  'No more posts to load.',
                    'pagination_color'   =>  [
                        'initial'   =>  $this->get_color( [ 'value'  =>  '#30344b' ] ),
                        'hover'   =>  $this->get_color()
                    ],
                    'pagination_background'   =>  [
                        'initial'   =>  $this->get_color( [ 'value'  =>  '#30344b' ] ),
                        'hover'   =>  $this->get_color()
                    ],
                    'pagination_border_color'   =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#30344b' ] )
                    ],
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                $dynamic_args = [
                    'pagination_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'pagination_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'pagination_color' ),
                        'selector'  =>  '.blog-bee-main-wrap .bbe-pagination.default .nav-links a, .site .blog-bee-main-wrap .bbe-pagination.number .page-numbers a, .blog-bee-main-wrap .bbe-pagination.number .page-numbers a, .blog-bee-main-wrap .bbe-pagination.ajax .label',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'pagination_background' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'pagination_background' ),
                        'default' =>  Theme_Defaults::get_defaults( 'pagination_background' ),
                        'selector'  =>  '.blog-bee-main-wrap .bbe-pagination.default.filled .nav-links a, .blog-bee-main-wrap .bbe-pagination.number.filled .page-numbers .page-numbers.current, body .blog-bee-main-wrap .bbe-pagination.ajax.filled .label',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                    'pagination_border_color'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'pagination_border_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'pagination_border_color' ),
                        'selector'  =>  '.blog-bee-main-wrap .bbe-pagination.default.outline .nav-links a, .blog-bee-main-wrap .bbe-pagination.number.outline .page-numbers .page-numbers.current, body .blog-bee-main-wrap .bbe-pagination.ajax.outline .label',
                        'property'  =>  '--bbe-site-border-color'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html(){
                if( $GLOBALS[ 'wp_query' ]->max_num_pages < 2 ) return; // guard, returning if not page 2 exists

                $type = Theme_Defaults::get_theme_option( 'pagination_type' );
                $block_class[] = self::$id;
                $block_class[] = $type;
                $block_class[] = Theme_Defaults::get_theme_option( 'pagination_style' );
                echo '<div class="' . esc_attr( implode( ' ', $block_class ) ) . '">';
                    switch( $type ) {
                        case 'ajax' :
                                $this->ajax_button();
                            break;
                        case 'number' :
                                $this->number_pagination();
                            break;
                        default :
                                the_posts_navigation();
                            break;
                    }
                echo '</div>';
            }

            /**
             * Ajax load more button
             * 
             * @since 1.0.0
             */
            public function ajax_button() {
                $label = Theme_Defaults::get_theme_option( 'pagination_ajax_label' );
                $no_results_label = Theme_Defaults::get_theme_option( 'pagination_ajax_no_results_label' );
                ?>
                    <div class="ajax-wrapper">
                        <span class="no-results-label hidden"><?php echo esc_html( $no_results_label ); ?></span>
                        <button class="ajax-button" type="button">
                            <span class="label"><?php echo esc_html( $label ); ?></span>
                        </button>
                    </div>
                <?php
            }

            /**
             * Ajax load more button
             * 
             * @since 1.0.0
             */
            private function number_pagination() {
                $pagination_link_attrs = [
                    'prev_text' =>  '<i class="fa-solid fa-angles-left"></i>',
                    'next_text' =>  '<i class="fa-solid fa-angles-right"></i>',
                    'type' =>   'list'
                ];
                echo wp_kses_post( paginate_links( $pagination_link_attrs ) );
            }
        }
        Pagination::get_instance();
    }