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

    use BBE\Helpers as Helpers;
    use BBE\Query_Manager as Query_Manager;
    use BBE\Customizer\Section\Section as Section;
    use BBE\Customizer\Section\Author_Box as Author_Box;
    use BBE\Customizer\Section\Post_Navigation as Post_Navigation;
    use BBE\Customizer\Section\Table_Of_Content as Table_Of_Content;
    use BBE\Customizer\Section\Advertisement as Advertisement;
    use BBE\Customizer\Theme_Defaults as Theme_Defaults;

    use function esc_html__;
    use function esc_attr;

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

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

            /**
             * Image size
             * 
             * @since 1.0.0
             */
            private $image_size = null;

            /**
             * Image option
             * 
             * @since 1.0.0
             */
            private $image_option = null;

            /**
             * Overlay
             * 
             * @since 1.0.0
             */
            private $overlay = null;

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

            /**
             * Image size Getter
             * 
             * @since 1.0.0
             */
            public function get_image_size() {
                if( $this->image_size === null ) {
                    $this->image_size = Theme_Defaults::get_theme_option( 'single_image_sizes' );
                }
                return $this->image_size;
            }

            /**
             * Image option Getter
             * 
             * @since 1.0.0
             */
            public function get_image_option() {
                if( $this->image_option === null ) {
                    $this->image_option = Theme_Defaults::get_theme_option( 'single_post_thumbnail_option' );
                }
                return $this->image_option;
            }

            /**
             * Overlay Getter
             * 
             * @since 1.0.0
             */
            public function get_overlay() {
                if( $this->overlay === null ) {
                    $this->overlay = ( $this->get_layout() === 'two' );
                }
                return $this->overlay;
            }

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'single_section' );
                $this->add_control( 'single_section_tab' );
                $this->add_control( 'single_author_box_option' );
                $this->add_control( 'single_post_navigation_option' );
                $this->add_control( 'single_related_posts_option' );
                $this->add_control( 'single_related_posts_redirect' );
                $this->add_control( 'single_container' );
                $this->add_control( 'single_post_elements_settings' );
                $this->add_control( 'single_post_thumbnail_option' );
                $this->add_control( 'single_content_reorder' );
                $this->add_control( 'single_meta_reorder' );
                $this->add_control( 'single_image_settings' );
                $this->add_control( 'single_image_sizes' );
                $this->tab = 'design';
                $this->add_control( 'single_title_typography' );
                $this->add_control( 'single_content_typography' );
                $this->add_control( 'single_content_background' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'single_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_author_box_option' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_post_navigation_option' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_related_posts_option' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_related_posts_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_container' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_post_elements_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_post_thumbnail_option' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_content_reorder' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_reorder' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_meta_reorder' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_reorder' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_image_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_image_sizes' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_title_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_content_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_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 = '' ) {
                $alignment = Helpers::get_customizer_alignments();
                $content_alignment = $alignment;
                $content_alignment[] = [
                    'label' =>  esc_html__( 'Justify', 'blog-bee' ),
                    'value' =>  'justify'
                ];
                $controls = [
                    'single_section' =>  [
                        'title' =>  esc_html__( 'Single Post', 'blog-bee' ),
                        'priority'  =>  20,
                        'panel' =>  'blog_panel'
                    ],
                    'single_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'single_author_box_option'  =>  [
                        'label' =>  esc_html__( 'Show Author Box', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'single_post_navigation_option' =>  [
                        'label' =>  esc_html__( 'Show Post Navigation', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'single_related_posts_option'   =>  [
                        'label' =>  esc_html__( 'Show Related Posts', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'single_related_posts_redirect' =>  [
                        'label' =>  esc_html__( 'Edit Related Posts', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'related_posts_section',
                        'conditions' =>  $this->get_condition_args( 'single_related_posts_option' )
                    ],
                    'single_container' =>  [
                        'label' =>  esc_html__( 'Container', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  Helpers::get_card_options()
                    ],
                    'single_post_elements_settings' =>  [
                        'label' =>  esc_html__( 'Post Element Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'single_post_thumbnail_option'  =>  [
                        'label' =>  esc_html__( 'Show Featured Image', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'single_content_reorder'    =>  [
                        'label' =>  esc_html__( 'Content Reorder', 'blog-bee' ),
                        'type'  =>  'reorder',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Category', 'blog-bee' ),
                                'value' =>  'category',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Title', 'blog-bee' ),
                                'value' =>  'title',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Meta', 'blog-bee' ),
                                'value' =>  'meta',
                                'allow_visibility'  =>  true
                            ],
                        ],
                    ],
                    'single_meta_reorder'   =>  [
                        'label' =>  esc_html__( 'Meta Reorder', 'blog-bee' ),
                        'type'  =>  'reorder',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Date', 'blog-bee' ),
                                'value' =>  'date',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Author', 'blog-bee' ),
                                'value' =>  'author',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Comment', 'blog-bee' ),
                                'value' =>  'comment',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Read Time', 'blog-bee' ),
                                'value' =>  'read-time',
                                'allow_visibility'  =>  true
                            ],
                        ],
                    ],
                    'single_image_settings' =>  [
                        'label' =>  esc_html__( 'Image Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'single_image_sizes'    =>  [
                        'label' =>  esc_html__( 'Image Sizes', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  Helpers::get_customizer_image_sizes()
                    ],
                    'single_title_typography'    =>  [
                        'label' =>  esc_html__( 'Title Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'single_content_typography'    =>  [
                        'label' =>  esc_html__( 'Content Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'single_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;
            }

            /**
             * get full conditions args
             * 
             * @param string $setting_id setting id to check condition for
             * @since 1.0.0
             */
            private function get_condition_args( $setting_id ): array {
                return [
                    'relation'  =>  'AND',
                    'rules' =>  [
                        [
                            'setting'   =>  $setting_id,
                            'operator'  =>  '==',
                            'value'     =>  true
                        ]
                    ]
                ];
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'single_section_tab' =>  'general',
                    'single_layouts'    =>  'one',
                    'single_author_box_option'    =>  true,
                    'single_author_box_redirect'    =>  '',
                    'single_post_navigation_option'    =>  true,
                    'single_post_navigation_redirect'    =>  '',
                    'single_table_of_content_option'    =>  false,
                    'single_table_of_content_redirect'    =>  '',
                    'single_related_posts_option'    =>  true,
                    'single_related_posts_redirect'    =>  '',
                    'single_container' =>  'default',
                    'single_post_elements_settings'    =>  false,
                    'single_post_thumbnail_option'    =>  true,
                    'single_content_reorder'    =>  [
                        [
                            'id'    =>  'category',
                            'value' =>  true
                        ],
                        [
                            'id'    =>  'title',
                            'value' =>  true
                        ],
                        [
                            'id'    =>  'meta',
                            'value' =>  true
                        ],
                    ],
                    'single_meta_reorder'    =>  [
                        [
                            'id'    =>  'date',
                            'value' =>  true
                        ],
                        [
                            'id'    =>  'author',
                            'value' =>  true
                        ],
                        [
                            'id'    =>  'comment',
                            'value' =>  true
                        ],
                        [
                            'id'    =>  'read-time',
                            'value' =>  true
                        ],
                    ],
                    'single_element_alignment'    =>  'left',
                    'single_content_alignment'    =>  'left',
                    'single_image_settings'    =>  false,
                    'single_image_sizes'    =>  'bbe_featured_large',
                    'single_image_ratio'    =>  'boxy',
                    'single_title_typography'    =>  $this->get_typography( [
                        'font_weight'   =>  '600',
                        'font_size' =>  $this->get_responsive( '30px', '30px', '24px' ),
                        'line_height'   =>  $this->get_responsive( 1.46, 1.46, 1.48 )
                    ] ),
                    'single_content_typography'    =>  $this->get_typography( [
                        'font_size' =>  $this->get_responsive( '14px', '14px', '14px' ),
                        'line_height'   =>  $this->get_responsive( 1.64, 1.64, 1.64 )
                    ] ),
                    'single_content_background'    =>  $this->get_color( [ 'value' => '' ] ),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                $dynamic_args = [
                    'single_container'  =>   [
                        'selector'  =>  'body.single .blog-bee-main-wrap',
                        'prefix'    =>  'card--'
                    ],
                    'single_title_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'single_title_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'single_title_typography' ),
                        'selector'  =>  'body.single .single-article .entry-inner .entry-title, body.single .single-header .entry-inner .entry-title'
                    ],
                    'single_content_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'single_content_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'single_content_typography' ),
                        'selector'  =>  'body.single .bbe-article .entry-excerpt p'
                    ],
                    'single_content_background'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'single_content_background' ),
                        'default' =>  Theme_Defaults::get_defaults( 'single_content_background' ),
                        'selector'  =>  'body.single .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';
                $article_class[] = 'bbe-article';
                $content_alignment = Theme_Defaults::get_theme_option( 'single_content_alignment' );
                $excerpt_class = 'entry-excerpt';
                $excerpt_class .= ' align--' . $content_alignment;
                ?>

                    <div class="single-article">
                        <article id="post-<?php the_ID(); ?>" <?php post_class( implode( ' ', $article_class ) ); ?>>

                            <?php

                                echo '<div class="entry-content">';

                                    if( in_array( $this->get_layout(), [ 'one' ] ) ) $this->get_single_header();

                                    if( $this->get_layout() === 'three' ) $this->get_single_featured_image();

                                    if( Theme_Defaults::get_theme_option( 'single_table_of_content_option' ) ) Table_Of_Content::get_instance()->render_html();

                                    echo '<div class="social-content-wrap">';

                                        echo '<div class="' . esc_attr( $excerpt_class ) . '">';
                                        
                                            Advertisement::get_instance()->get_advertisement( 'before-content' );
                                        
                                            the_content();

                                            blog_bee_tags_list();

                                            Advertisement::get_instance()->get_advertisement( 'after-content' );

                                        echo '</div><!-- .entry-excerpt -->';
                                        
                                    echo '</div>';

                                echo '</div><!-- .entry-content -->';

                            ?>

                            <footer class="entry-footer">

                                <?php blog_bee_entry_footer(); ?>

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

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

                if( Theme_Defaults::get_theme_option( 'single_post_navigation_option' ) ) Post_Navigation::get_instance()->render_html();

                if( Theme_Defaults::get_theme_option( 'single_author_box_option' ) ) Author_Box::get_instance()->render_html();
            }

            /**
             * Meta reorder
             * 
             * @since 1.0.0
             */
            private function meta_reorder() {
                $meta_reorder = Theme_Defaults::get_theme_option( 'single_meta_reorder' );
                if( ! empty( $meta_reorder ) && is_array( $meta_reorder ) ) :

                    echo '<div class="entry-meta">';

                        foreach( $meta_reorder as $meta ) :
                            
                            if( ! $meta[ 'value' ] ) continue;

                            switch( $meta[ 'id' ] ) :

                                case 'author':
                                        Query_Manager::get_author();
                                    break;

                                case 'comment':
                                        Query_Manager::get_comments();
                                    break;

                                case 'read-time':
                                        Query_Manager::get_read_time();
                                    break;

                                case 'date':
                                        Query_Manager::get_date();
                                    break;

                            endswitch;

                        endforeach;

                    echo '</div><!-- .entry-meta -->';

                endif;
            }

            /**
             * Get single header part
             * 
             * @since 1.0.0
             */
            public function get_single_header() {
                
                $content_reorder = Theme_Defaults::get_theme_option( 'single_content_reorder' );

                echo '<div class="single-header">';

                    if( $this->get_layout() !== 'three' ) $this->get_single_featured_image();

                    if( ! empty( $content_reorder ) && is_array( $content_reorder ) ) :

                        echo '<div class="entry-inner">';

                            foreach( $content_reorder as $content ) :

                                if( ! $content[ 'value' ] ) continue;

                                switch( $content[ 'id' ] ) :

                                    case 'title':
                                            Query_Manager::get_title( false );
                                        break;

                                    case 'meta':
                                            $this->meta_reorder();
                                        break;

                                    case 'category':
                                            Query_Manager::get_categories();
                                        break;

                                endswitch;

                            endforeach;

                        echo '</div>';

                    endif;

                    if( $this->get_image_option() && $this->get_overlay() ) Query_Manager::get_closing_figure_tag();

                echo '</div><!-- .single-header -->';
            }

            /**
             * Single post featured image
             * 
             * @since 1.0.0
             */
            public function get_single_featured_image() {
                
                if( $this->get_image_option() ) {

                    Query_Manager::get_opening_figure_tag();

                        blog_bee_post_thumbnail( $this->get_image_size() );

                    if( ! $this->get_overlay() ) Query_Manager::get_closing_figure_tag();

                }
            }
        }
        Single::get_instance();
    }