<?php
    /**
     * Post Navigation Section
     * 
     * @package Blog Bee Pro
     * @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 BBE\Query_Manager as Query_Manager;

    use function esc_html__;
    use function esc_attr;
    use function get_the_post_thumbnail_url;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $controls = [
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'post_navigation_content_toggles' =>  [
                        'image' =>  true,
                        'title' =>  true,
                        'meta'  =>  true,
                    ]
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                $dynamic_args = [
                    
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $content_toggles = Theme_Defaults::get_theme_option( 'post_navigation_content_toggles' );
                $default_content_toggles = Theme_Defaults::get_defaults( 'post_navigation_content_toggles' );
                $image = isset( $content_toggles[ 'image' ] ) ? $content_toggles[ 'image' ] : $default_content_toggles[ 'image' ];
                $title = isset( $content_toggles[ 'title' ] ) ? $content_toggles[ 'title' ] : $default_content_toggles[ 'title' ];
                $meta = isset( $content_toggles[ 'meta' ] ) ? $content_toggles[ 'meta' ] : $default_content_toggles[ 'meta' ];

                ?>

                    <div class="<?php echo esc_attr( self::$id ); ?>" <?php Helpers::the_aos_data_attr(); ?>>

                        <?php

                            $previous = get_previous_post();
                            $next = get_next_post();
                            $prev_image_html = '';
                            $next_image_html = '';

                            // thumbnail
                            if( $image ) :
                                $prev_post_thumbnail = ( ! empty( $previous ) ) ? get_the_post_thumbnail( $previous->ID, 'post-thumbnail', [ 'alt' => '%title' ] ) : '';
                                $next_post_thumbnail = ( ! empty( $next ) ) ? get_the_post_thumbnail( $next->ID, 'post-thumbnail', [ 'alt' => '%title' ]  ) : '';

                                $prev_image_html = sprintf( '<figure class="bbe-article-figure">%s</figure>', $prev_post_thumbnail );
                                $next_image_html = sprintf( '<figure class="bbe-article-figure">%s</figure>', $next_post_thumbnail );
                            endif;

                            $prev_label = $this->get_label( [
                                'sub_title' =>  esc_html__( 'Previous: ', 'blog-bee' ),
                                'title_enabled' =>  $title,
                                'date_enabled' =>  $meta,
                            ] );
                            
                            $next_label = $this->get_label( [
                                'sub_title' =>  esc_html__( 'Next: ', 'blog-bee' ),
                                'title_enabled' =>  $title,
                                'date_enabled' =>  $meta,
                            ] );

                            the_post_navigation( [
                                'prev_text' => sprintf( '%s%s', $prev_image_html, $prev_label ),
                                'next_text' => sprintf( '%s%s', $next_label, $next_image_html )
                            ] );

                        ?>

                    </div>

                <?php

            }

            /**
             * Returns navigation label
             * 
             * @param string $label
             * @since 1.0.0
             */
            private function get_label( $args = [] ) {
                $sub_title = isset( $args[ 'sub_title' ] ) ? $args[ 'sub_title' ] : '';
                $title_enabled = isset( $args[ 'title_enabled' ] ) ? $args[ 'title_enabled' ] : true;
                $date_enabled = isset( $args[ 'date_enabled' ] ) ? $args[ 'date_enabled' ] : true;

                $title = $date = '';
                $sub_title = sprintf( '<span class="nav-subtitle">%s</span>', esc_html( $sub_title ) );
                if( $title_enabled ) $title = sprintf( '<span class="nav-title">%s</span>', '%title' );
                if( $date_enabled ) $date = sprintf( '<span class="nav-date">%s</span>', '%date' );

                $string = sprintf( '<div class="nav-content">%s%s%s</div>', $sub_title, $title, $date );
                return $string;
            }
        }
        Post_Navigation::get_instance();
    }