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

    use BBE\Customizer\Section\Section as Section;
    use BBE\Customizer\Section\Page_Table_Of_Content as Page_Table_Of_Content;
    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;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'page_section' );
                $this->add_control( 'page_section_tab' );
                $this->add_control( 'page_show_title' );
                $this->add_control( 'page_show_featured_image' );
                $this->add_control( 'page_show_content' );
                $this->add_control( 'page_container' );
                $this->add_control( 'page_image_settings' );
                $this->add_control( 'page_image_size' );
                $this->tab = 'design';
                $this->add_control( 'page_title_typography' );
                $this->add_control( 'page_content_typography' );
                $this->add_control( 'page_content_background' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'page_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'page_show_title' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'page_show_featured_image' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'page_show_content' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'page_container' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'page_image_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'page_image_size' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'page_title_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'page_content_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'page_content_background' =>  [
                        '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 = [
                    'page_section' =>  [
                        'title' =>  esc_html__( 'Single Page', 'blog-bee' ),
                        'priority'  =>  30,
                        'panel' =>  'blog_panel'
                    ],
                    'page_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'page_show_title' =>  [
                        'label' =>  esc_html__( 'Show Title', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'page_show_featured_image' =>  [
                        'label' =>  esc_html__( 'Show Featured Image', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'page_show_content' =>  [
                        'label' =>  esc_html__( 'Show Content', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'page_container' =>  [
                        'label' =>  esc_html__( 'Container', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  Helpers::get_card_options()
                    ],
                    'page_image_settings' =>  [
                        'label' =>  esc_html__( 'Image Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle',
                    ],
                    'page_image_size' =>  [
                        'label' =>  esc_html__( 'Image Size', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  Helpers::get_customizer_image_sizes()
                    ],
                    'page_title_typography' =>  [
                        'label' =>  esc_html__( 'Title Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'page_content_typography' =>  [
                        'label' =>  esc_html__( 'Content Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'page_content_background' =>  [
                        'label' =>  esc_html__( 'Background', 'blog-bee' ),
                        'description' =>  esc_html__( 'Will only show if container is boxed.', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'color_types'   =>  [ 'solid', 'gradient' ]
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'page_section_tab' =>  'general',
                    'page_show_title' =>  true,
                    'page_show_featured_image' =>  true,
                    'page_show_content' =>  true,
                    'page_show_table_of_content' =>  false,
                    'page_table_of_content_redirect' =>  true,
                    'page_container' =>  'default',
                    'page_image_settings' =>  false,
                    'page_image_size' =>  'bbe_featured_large',
                    'page_image_ratio' =>  'wide',
                    'page_title_typography' =>  $this->get_typography([
                        'font_weight' =>  '700',
                        'font_size'   =>  $this->get_responsive( '28px', '24px', '28px' ),
                        'line_height' =>  $this->get_responsive( 1.42, 1.4, 1.42 )
                    ]),
                    'page_content_typography' =>  $this->get_typography([
                        'font_weight' =>  'regular',
                        'font_size'   =>  $this->get_responsive( '14px', '14px', '14px' ),
                        'line_height' =>  $this->get_responsive( 1.64, 1.64, 1.64 )
                    ]),
                    'page_content_background' =>  $this->get_color( [ 'value' => '' ] ),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                $dynamic_args = [
                    'page_container'  =>   [
                        'selector'  =>  '.blog-bee-main-wrap',
                        'prefix'    =>  'card--'
                    ],
                    'page_title_typography' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'page_title_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'page_title_typography' ),
                        'selector'  =>  '.page .site-main .bbe-article .entry-title',
                    ],
                    'page_content_typography'   =>    [
                        'value' =>  Theme_Defaults::get_theme_option( 'page_content_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'page_content_typography' ),
                        'selector'  =>  '.page .site-main .bbe-article .entry-excerpt p',
                    ],
                    'page_content_background'   =>    [
                        'value' =>  Theme_Defaults::get_theme_option( 'page_content_background' ),
                        'default' =>  Theme_Defaults::get_defaults( 'page_content_background' ),
                        'selector'  =>  '.page .blog-bee-main-wrap',
                        'property'  =>  '--bbe-site-bg-color',
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $article_class[] = 'bbe-article';
                $image_size = Theme_Defaults::get_theme_option( 'page_image_size' );
                ?>
                    <article id="post-<?php the_ID(); ?>" <?php post_class( implode( ' ', $article_class ) ); ?>>

                        <header class="entry-header">

                            <?php if( Theme_Defaults::get_theme_option( 'page_show_title' ) ) Query_Manager::get_title( false ); ?>

                        </header><!-- .entry-header -->

                        <?php

                            if( Theme_Defaults::get_theme_option( 'page_show_featured_image' ) ) :

                                Query_Manager::get_opening_figure_tag();

                                    blog_bee_post_thumbnail( $image_size );

                                Query_Manager::get_closing_figure_tag();

                            endif;

                        ?>

                        <div class="entry-content">

                            <?php
                                if( Theme_Defaults::get_theme_option( 'page_show_table_of_content' ) ) Page_Table_Of_Content::get_instance()->render_html();

                                if( Theme_Defaults::get_theme_option( 'page_show_content' ) ) :

                                    echo '<div class="entry-excerpt">';
                                
                                        the_content();

                                    echo '</div>';

                                endif;

                                wp_link_pages( [
                                    'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'blog-bee' ),
                                    'after'  => '</div>',
                                ] );
                            ?>

                        </div><!-- .entry-content -->

                        <?php if ( get_edit_post_link() ) : ?>

                            <footer class="entry-footer">

                                <?php
                                    edit_post_link(
                                        sprintf(
                                            wp_kses(
                                                /* translators: %s: Name of current post. Only visible to screen readers */
                                                __( 'Edit <span class="screen-reader-text">%s</span>', 'blog-bee' ),
                                                array(
                                                    'span' => array(
                                                        'class' => array(),
                                                    ),
                                                )
                                            ),
                                            wp_kses_post( get_the_title() )
                                        ),
                                        '<span class="edit-link">',
                                        '</span>'
                                    );
                                ?>

                            </footer><!-- .entry-footer -->

                        <?php endif; ?>

                    </article><!-- #post-<?php the_ID(); ?> -->

                <?php
            }
        }
        Page::get_instance();
    }