<?php
    /**
     * Content Sidebar 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\Theme_Defaults as Theme_Defaults;

    use function esc_html__;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'content_sidebar_section' );
                $this->add_control( 'content_sidebar_section_tab' );
                $this->add_control( 'content_sidebar_reorder' );
                $this->add_control( 'content_sidebar_layouts' );
                $this->add_control( 'content_sidebar_sidebar_redirect' );
                $this->add_control( 'content_sidebar_section_label' );
                $this->add_control( 'content_sidebar_container' );
                $this->add_control( 'content_sidebar_post_query_settings' );
                $this->add_control( 'content_sidebar_post_source' );
                $this->add_control( 'content_sidebar_categories' );
                $this->add_control( 'content_sidebar_posts_to_include' );
                $this->add_control( 'content_sidebar_number_of_posts' );
                $this->add_control( 'content_sidebar_orderby' );
                $this->add_control( 'content_sidebar_hide_no_featured' );
                $this->add_control( 'content_sidebar_post_elements_settings' );
                $this->add_control( 'content_sidebar_content_toggles' );
                $this->tab = 'design';
                $this->add_control( 'content_sidebar_typogrpahy' );
                $this->add_control( 'content_sidebar_section_background' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'content_sidebar_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_reorder' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_reorder' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_layouts' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_image' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_sidebar_redirect' =>  [
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_section_label' =>  [
                        'sanitize_callback' =>  'sanitize_text_field',
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_container' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_post_query_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_post_source' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_categories' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_async_multiselect' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_posts_to_include' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_async_multiselect' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_number_of_posts' =>  [
                        'sanitize_callback' =>  'absint',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_orderby' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_hide_no_featured' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_post_elements_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_content_toggles' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle_group' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_typogrpahy' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'content_sidebar_section_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 = [
                    'content_sidebar_section' =>  [
                        'title' =>  esc_html__( 'Content Sidebar', 'blog-bee' ),
                        'active_callback'   =>  function() {
                            return Helpers::get_reorder_value( 'section_reorder_fields', 'content-sidebar' );
                        },
                        'section_to_open' =>  'section_reorder_section'
                    ],
                    'content_sidebar_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'content_sidebar_reorder' =>  [
                        'label' =>  esc_html__( 'Reorder', 'blog-bee' ),
                        'type'  =>  'reorder',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Sidebar', 'blog-bee' ),
                                'value' =>  'sidebar',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Content', 'blog-bee' ),
                                'value' =>  'content',
                                'allow_visibility'  =>  false
                            ],
                        ],
                        'direction' =>  'horizontal'
                    ],
                    'content_sidebar_layouts' =>  [
                        'label' =>  esc_html__( 'Layouts', 'blog-bee' ),
                        'type'  =>  'radio-image',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Layout 1', 'blog-bee' ),
                                'value' =>  'one',
                                'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/content-sidebar-one.webp'
                            ],
                        ]
                    ],
                    'content_sidebar_sidebar_redirect' =>  [
                        'label' =>  esc_html__( 'Edit Sidebar.', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'sidebar-widgets-bbe-content-sidebar-block-widget-area',
                        'active_callback'   =>  function(){
                            return Helpers::get_reorder_value( 'content_sidebar_reorder', 'sidebar' );
                        }
                    ],
                    'content_sidebar_section_label' =>  [
                        'label' =>  esc_html__( 'Section Label', 'blog-bee' ),
                        'type'  =>  'bbe-text'
                    ],
                    'content_sidebar_container' =>  [
                        'label' =>  esc_html__( 'Container', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  Helpers::get_card_options()
                    ],
                    'content_sidebar_post_query_settings' =>  [
                        'label' =>  esc_html__( 'Post Query Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'content_sidebar_post_source' =>  [
                        'label' =>  esc_html__( 'Post Source', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'   =>  [
                            'latest'    =>  esc_html__( 'Latest', 'blog-bee' ),
                            'categories'    =>  esc_html__( 'Category', 'blog-bee' ),
                            'manual'    =>  esc_html__( 'Manual', 'blog-bee' ),
                        ]
                    ],
                    'content_sidebar_categories' =>  [
                        'label' =>  esc_html__( 'Post Categories', 'blog-bee' ),
                        'type'  =>  'async-multiselect',
                        'endpoint'  =>  'get_taxonomy',
                        'purpose'   =>  'category',
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'content_sidebar_post_source',
                                    'operator'  =>  '==',
                                    'value' =>  'categories'
                                ]
                            ]
                        ]
                    ],
                    'content_sidebar_posts_to_include' =>  [
                        'label' =>  esc_html__( 'Post to Include', 'blog-bee' ),
                        'type'  =>  'async-multiselect',
                        'endpoint'  =>  'get_posts',
                        'purpose'   =>  'post',
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'content_sidebar_post_source',
                                    'operator'  =>  '==',
                                    'value' =>  'manual'
                                ]
                            ]
                        ]
                    ],
                    'content_sidebar_number_of_posts' =>  [
                        'label' =>  esc_html__( 'Number of posts', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'show_unit' =>  false,
                        'input_attrs'   =>  [
                            'min'   =>  1,
                            'max'   =>  15,
                            'step'  =>  1
                        ]
                    ],
                    'content_sidebar_orderby' =>  [
                        'label' =>  esc_html__( 'Order By', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'   =>  Helpers::get_order_by()
                    ],
                    'content_sidebar_hide_no_featured' =>  [
                        'label' =>  esc_html__( 'Hide Posts With No Featured Image', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'content_sidebar_post_elements_settings' =>  [
                        'label' =>  esc_html__( 'Post Elements Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'content_sidebar_content_toggles' =>  [
                        'type'  =>  'toggle-button-group',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Featured Image', 'blog-bee' ),
                                'id' =>  'image'
                            ],
                            [
                                'label' =>  esc_html__( 'Title', 'blog-bee' ),
                                'id' =>  'title'
                            ],
                            [
                                'label' =>  esc_html__( 'Date', 'blog-bee' ),
                                'id' =>  'date'
                            ],
                            [
                                'label' =>  esc_html__( 'Author', 'blog-bee' ),
                                'id' =>  'author'
                            ],
                            [
                                'label' =>  esc_html__( 'Read Time', 'blog-bee' ),
                                'id' =>  'time'
                            ],
                            [
                                'label' =>  esc_html__( 'Comment', 'blog-bee' ),
                                'id' =>  'comment'
                            ],
                        ]
                    ],
                    'content_sidebar_typogrpahy' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'content_sidebar_section_background' =>  [
                        'label' =>  esc_html__( 'Section Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'color_types'   =>  [ 'solid', 'gradient', 'image' ]
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'content_sidebar_section_tab' =>  'general',
                    'content_sidebar_reorder' =>  [
                        [
                            'id'    =>  'content',
                            'value' =>  true,
                        ],
                        [
                            'id'    =>  'sidebar',
                            'value' =>  true,
                        ],
                    ],
                    'content_sidebar_layouts' =>  'one',
                    'content_sidebar_sidebar_redirect' =>  '',
                    'content_sidebar_section_label' =>  'Content Sidebar',
                    'content_sidebar_container'    =>  'default',
                    'content_sidebar_post_query_settings' =>  false,
                    'content_sidebar_post_source' =>  'latest',
                    'content_sidebar_categories' =>  [],
                    'content_sidebar_tags' =>  [],
                    'content_sidebar_authors' =>  [],
                    'content_sidebar_posts_to_include' =>  [],
                    'content_sidebar_number_of_posts' =>  5,
                    'content_sidebar_posts_offset' =>  0,
                    'content_sidebar_orderby' =>  'date-desc',
                    'content_sidebar_hide_no_featured' =>  false,
                    'content_sidebar_post_elements_settings' =>  false,
                    'content_sidebar_content_toggles'   =>  [
                        'image' =>  true,
                        'title' =>  true,
                        'date' =>  true,
                        'author' =>  false,
                        'time' =>  true,
                        'comment' =>  true,
                    ],
                    'content_sidebar_typogrpahy'    =>  $this->get_typography( [
                        'font_weight'   =>  '600',
                        'font_size'   =>  $this->get_responsive( '30px', '26px', '20px' ),
                        'line_height'  =>  $this->get_responsive( 1.48, 1.48, 1.48 )
                    ] ),
                    'content_sidebar_section_background'    =>  $this->get_color( [ 'value' => '' ] ),
                    'content_sidebar_layout_two_settings'    =>  '',
                    'content_sidebar_text_color'    =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#fff' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#fff' ] ),
                    ],
                    'content_sidebar_content_background'    =>  $this->get_color( [ 'value' => '#ff6b6b' ] ),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(){
                $dynamic_css = [
                    'content_sidebar_section_label'  =>  [
                        'selector'  =>  '.bbe-content-sidebar .section-head .label',
                        'parent'  =>  '.bbe-content-sidebar .section-head',
                    ],
                    'content_sidebar_container'    =>  [
                        'selector'  =>  '.bbe-content-sidebar',
                        'prefix'    =>  'card--'
                    ],
                    'content_sidebar_typogrpahy'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'content_sidebar_typogrpahy' ),
                        'default' =>  Theme_Defaults::get_defaults( 'content_sidebar_typogrpahy' ),
                        'selector'  =>  '.bbe-content-sidebar .content-wrapper .content-block .bbe-article .entry-title, .bbe-content-sidebar .content-wrapper .content-block .bbe-article .entry-title a',
                    ],
                    'content_sidebar_section_background'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'content_sidebar_section_background' ),
                        'default' =>  Theme_Defaults::get_defaults( 'content_sidebar_section_background' ),
                        'selector'  =>  '.bbe-content-sidebar',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                ];
                return $dynamic_css;
            }

            /**
             * Get wp_query instance
             * 
             * @since 1.0.0
             */
            private function get_query_instance() {
                $query_attrs = [
                    'posts_per_page'    =>  'content_sidebar_number_of_posts',
                    'offset'    =>  'content_sidebar_posts_offset',
                    'orderby'   =>  'content_sidebar_orderby',
                    'hide_featured' =>  'content_sidebar_hide_no_featured',
                    'cat'   =>  'content_sidebar_categories',
                    'tag__in'   =>  'content_sidebar_tags',
                    'author'    =>  'content_sidebar_authors',
                    'post__in'  =>  'content_sidebar_posts_to_include',
                    'post_source'  =>  'content_sidebar_post_source',
                ];
                $query_instance = Query_Manager::fine_tune_query_attrs( $query_attrs );
                return $query_instance;
            }

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

                if( ! $this->get_query_instance()->have_posts() ) {
                    return;
                }

                $section_label = Theme_Defaults::get_theme_option( 'content_sidebar_section_label' );
                $card_enable   = Theme_Defaults::get_theme_option( 'content_sidebar_container' );
                $reorder       = Theme_Defaults::get_theme_option( 'content_sidebar_reorder' );

                $block_class[] = self::$id;
                $layouts = Theme_Defaults::get_theme_option( 'content_sidebar_layouts' );
                $block_class[] = 'layout--' . $layouts;
                $block_class[] = 'card--' . $card_enable;
                ?>

                <section id="<?php echo esc_attr( self::$id ); ?>" class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>" <?php Helpers::the_aos_data_attr(); ?>>

                    <div class="bbe-container">

                        <div class="row">

                            <div class="content-wrapper">

                                <?php
                                if( ! empty( $reorder ) && is_array( $reorder ) ) :

                                    foreach( $reorder as $block ) :

                                        if( ! $block['value'] ) {
                                            continue;
                                        }

                                        switch( $block['id'] ) :

                                            case 'content' :
                                                ?>

                                                    <div class="content-block">

                                                        <?php

                                                            Helpers::get_section_head( $section_label );
                                                            
                                                            $this->get_content_block();

                                                        ?>

                                                    </div>

                                                <?php
                                            break;

                                            case 'sidebar' :
                                                ?>

                                                    <?php $this->get_sidebar_block(); ?>                                              

                                                <?php
                                            break;

                                        endswitch;

                                    endforeach;

                                endif;
                                ?>

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

                        </div><!-- .row -->

                    </div><!-- .bbe-container -->

                </section><!-- .bbe-content-sidebar -->

                <?php
            }
            /**
             * Get content block
             * 
             * @since 1.0.0
             */
            private function get_content_block() {
                $query_instance = $this->get_query_instance();
                $content_toggles = Theme_Defaults::get_theme_option( 'content_sidebar_content_toggles' );
                $posts_per_page = Theme_Defaults::get_theme_option( 'content_sidebar_number_of_posts' );
                $default_content_toggles = Theme_Defaults::get_defaults( 'content_sidebar_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' ];
                $date = isset( $content_toggles[ 'date' ] ) ? $content_toggles[ 'date' ] : $default_content_toggles[ 'date' ];
                $author = isset( $content_toggles[ 'author' ] ) ? $content_toggles[ 'author' ] : $default_content_toggles[ 'author' ];
                $time = isset( $content_toggles[ 'time' ] ) ? $content_toggles[ 'time' ] : $default_content_toggles[ 'time' ];
                $comment = isset( $content_toggles[ 'comment' ] ) ? $content_toggles[ 'comment' ] : $default_content_toggles[ 'comment' ];
                $count = 0;

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

                    while( $query_instance->have_posts() ) :
                        $query_instance->the_post();
                        $is_first_post = false;

                        if( $count === 0 ) {
                            $class = 'featured-post';
                            $is_first_post = true;
                        } else {
                            $class = 'list-post';
                        }

                        if( $count < 2 ) echo '<div class="' . esc_attr( $class ) . '">';

                            Query_Manager::get_opening_article_tag();

                                if( $image ) :

                                    Query_Manager::get_opening_figure_tag();

                                        Query_Manager::get_featured_image( 'medium_large' );

                                    Query_Manager::get_closing_figure_tag();
                                    
                                endif;

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

                                    if( $is_first_post ) Query_Manager::get_categories();
                                    
                                    if( $title ) Query_Manager::get_title();

                                    if( $is_first_post ) {

                                        echo '<div class="divider"></div>';

                                        Query_Manager::get_excerpt();
                                        
                                    }

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

                                        if( $author ) Query_Manager::get_author();

                                        if( $comment ) Query_Manager::get_comments();

                                        if( $time ) Query_Manager::get_read_time();

                                        if( $date ) Query_Manager::get_date();

                                    echo '</div>';

                                echo '</div>';

                            Query_Manager::get_closing_article_tag();

                        if( ( $count === 0 ) || ( ( $count + 1 ) === $posts_per_page ) || ( $query_instance->post_count === ( $count + 1 ) ) ) echo '</div>';

                        $count++;

                    endwhile;

                    wp_reset_postdata();

                echo '</div><!-- .content-block -->';
            }

            /**
             * Get sidebar block
             * 
             * @since 1.0.0
             */
            private function get_sidebar_block() {
                $sidebar_id = 'bbe-content-sidebar-block-widget-area';

                if( is_active_sidebar( $sidebar_id ) ) :

                    echo '<div class="sidebar-block">';

                        dynamic_sidebar( $sidebar_id );

                    echo '</div><!-- .sidebar-block -->';

                endif;

            }
        }
        Content_Sidebar::get_instance();
    }