<?php
    /**
     * Main Banner Right 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;
    use WP_Query;
    use BBE\Query_Manager as Query_Manager;

    use function esc_html__;
    use function esc_attr;

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

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

            /**
             * Get layout
             * 
             * @since 1.0.0
             */
            private function get_layout() {
                if( $this->layout === null ) {
                    $this->layout = Theme_Defaults::get_theme_option( 'main_banner_right_layout' );
                }
                return $this->layout;
            }

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'main_banner_right_section' );
                $this->add_control( 'main_banner_right_section_tab' );
                $this->add_control( 'main_banner_right_post_source' );
                $this->add_control( 'main_banner_right_categories' );
                $this->add_control( 'main_banner_right_posts_to_include' );
                $this->add_control( 'main_banner_right_number_of_posts' );
                $this->add_control( 'main_banner_right_orderby' );
                $this->add_control( 'main_banner_right_hide_no_featured' );
                $this->add_control( 'main_banner_right_content_alignment' );
                $this->add_control( 'main_banner_right_image_settings' );
                $this->add_control( 'main_banner_right_image_sizes' );
                $this->tab = 'design';
                $this->add_control( 'main_banner_right_post_title_typography' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'main_banner_right_section_tab' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'main_banner_right_post_source' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'main_banner_right_categories' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_async_multiselect' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'main_banner_right_posts_to_include' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_async_multiselect' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'main_banner_right_number_of_posts' =>  [
                        'sanitize_callback' =>  'absint',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'main_banner_right_orderby' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'main_banner_right_hide_no_featured' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'main_banner_right_content_alignment' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'main_banner_right_image_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'main_banner_right_image_sizes' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'main_banner_right_post_title_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $controls = [
                    'main_banner_right_section' =>  [
                        'title' =>  esc_html__( 'Main Banner Right', 'blog-bee' ),
                        'active_callback'   =>  function() {
                            return Helpers::get_reorder_value( 'section_reorder_fields', 'main-banner' ) && Helpers::get_reorder_value( 'main_banner_reorder', 'right' );
                        },
                        'section_to_open' =>  'main_banner_section'
                    ],
                    'main_banner_right_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'main_banner_right_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' ),
                            'post_tags' =>  esc_html__( 'Tags', 'blog-bee' ),
                            'authors'   =>  esc_html__( 'Authors', 'blog-bee' ),
                            'manual'    =>  esc_html__( 'Manual', 'blog-bee' ),
                            'featured'  =>  esc_html__( 'Featured / Sticky Posts', 'blog-bee' ),
                        ]
                    ],
                    'main_banner_right_categories' =>  [
                        'label' =>  esc_html__( 'Post Categories', 'blog-bee' ),
                        'type'  =>  'async-multiselect',
                        'endpoint'  =>  'get_taxonomy',
                        'purpose'   =>  'category',
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'main_banner_right_post_source',
                                    'operator'  =>  '==',
                                    'value' =>  'categories'
                                ]
                            ]
                        ]
                    ],
                    'main_banner_right_posts_to_include' =>  [
                        'label' =>  esc_html__( 'Post to Include', 'blog-bee' ),
                        'type'  =>  'async-multiselect',
                        'endpoint'  =>  'get_posts',
                        'purpose'   =>  'post',
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'main_banner_right_post_source',
                                    'operator'  =>  '==',
                                    'value' =>  'manual'
                                ]
                            ]
                        ]
                    ],
                    'main_banner_right_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
                        ]
                    ],
                    'main_banner_right_orderby' =>  [
                        'label' =>  esc_html__( 'Order By', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'   =>  Helpers::get_order_by()
                    ],
                    'main_banner_right_hide_no_featured' =>  [
                        'label' =>  esc_html__( 'Hide Posts With No Featured Image', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'main_banner_right_content_alignment' =>  [
                        'label' =>  esc_html__( 'Alignment', 'blog-bee' ),
                        'type'  =>  'radio-tab',
                        'fields'    =>  Helpers::get_customizer_alignments()
                    ],
                    'main_banner_right_image_settings' =>  [
                        'label' =>  esc_html__( 'Image Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'main_banner_right_image_sizes' =>  [
                        'label' =>  esc_html__( 'Images Sizes', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'   =>  Helpers::get_customizer_image_sizes()
                    ],
                    'main_banner_right_post_title_typography' =>  [
                        'label' =>  esc_html__( 'Title Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'main_banner_right_section_tab' =>  'general',
                    'main_banner_right_layout' =>  'list',
                    'main_banner_right_image_position' =>  'right',
                    'main_banner_right_post_query_settings' =>  false,
                    'main_banner_right_post_source' =>  'latest',
                    'main_banner_right_categories' =>  [],
                    'main_banner_right_tags' =>  [],
                    'main_banner_right_authors' =>  [],
                    'main_banner_right_posts_to_include' =>  [],
                    'main_banner_right_number_of_posts' =>  5,
                    'main_banner_right_posts_offset' =>  0,
                    'main_banner_right_orderby' =>  'date-desc',
                    'main_banner_right_hide_no_featured' =>  false,
                    'main_banner_right_post_element_settings' =>  false,
                    'main_banner_right_content_toggles' =>  [
                        'image' =>  true,
                        'category' =>  true,
                        'title' =>  true,
                        'excerpt'   =>  false,
                        'meta'  =>  false
                    ],
                    'main_banner_right_content_alignment'    =>  'left',
                    'main_banner_right_image_settings'   =>  '',
                    'main_banner_right_image_sizes'  =>  'bbe_post_card',
                    'main_banner_right_post_title_typography'    =>  $this->get_typography([
                        'font_weight'   =>  '600',
                        'font_size'   =>  $this->get_responsive( '17px', '17px', '17px' ),
                        'line_height' =>  $this->get_responsive( 1.48, 1.48, 1.48 )
                    ]),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! Helpers::get_reorder_value( 'section_reorder_fields', 'main-banner' ) || ! Helpers::get_reorder_value( 'main_banner_reorder', 'right' ) ) return [];
                $dynamic_args = [
                    'main_banner_right_content_alignment'    =>  [
                        'selector'  =>  '.bbe-main-banner-right',
                        'prefix'    =>  'align--'
                    ],
                    'main_banner_right_post_title_typography'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'main_banner_right_post_title_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'main_banner_right_post_title_typography' ),
                        'selector'  =>  '.bbe-main-banner .bbe-main-banner-right .entry-inner .entry-title, .bbe-main-banner .bbe-main-banner-right .entry-inner .entry-title a'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Get wp_query instance
             * 
             * @since 1.0.0
             */
            private function get_query_instance() {
                $query_attrs = [
                    'post_source'   =>  'main_banner_right_post_source',
                    'cat'   =>  'main_banner_right_categories',
                    'post__in'  =>  'main_banner_right_posts_to_include',
                    'posts_per_page'    =>  'main_banner_right_number_of_posts',
                    'orderby'   =>  'main_banner_right_orderby',
                    'hide_featured' =>  'main_banner_right_hide_no_featured',
                ];
                $query_instance = Query_Manager::fine_tune_query_attrs( $query_attrs );
                return $query_instance;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $query_instance = $this->get_query_instance();
                if( ! $query_instance->have_posts() ) return;

                $block_class[] = self::$id;
                $block_class[] = 'right-block banner-sidebar';
                $layout = $this->get_layout();
                $block_class[] = 'layout--' . $layout;
                $block_class[] = 'align--' . Theme_Defaults::get_theme_option( 'main_banner_right_content_alignment' );
                if( $this->check_for_scroll() ) $block_class[] = 'scroll--off';
                if( $layout === 'list' ) $block_class[] = 'position--' . Theme_Defaults::get_theme_option( 'main_banner_right_image_position' );

                $content_toggles = Theme_Defaults::get_theme_option( 'main_banner_right_content_toggles' );
                $default_content_toggles = Theme_Defaults::get_defaults( 'main_banner_right_content_toggles' );
                $category = isset( $content_toggles[ 'category' ] ) ? $content_toggles[ 'category' ] : $default_content_toggles[ 'category' ];
                $title = isset( $content_toggles[ 'title' ] ) ? $content_toggles[ 'title' ] : $default_content_toggles[ 'title' ];
                $excerpt = isset( $content_toggles[ 'excerpt' ] ) ? $content_toggles[ 'excerpt' ] : $default_content_toggles[ 'excerpt' ];
                $meta = isset( $content_toggles[ 'meta' ] ) ? $content_toggles[ 'meta' ] : $default_content_toggles[ 'meta' ];
                $image = isset( $content_toggles[ 'image' ] ) ? $content_toggles[ 'image' ] : $default_content_toggles[ 'image' ];
                if( ! $image ) $block_class[] = 'image--off';
                ?>
                    <div class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>">

                        <div class="article-wrap">

                            <?php

                                while( $query_instance->have_posts() ) :

                                    $query_instance->the_post();

                                    Query_Manager::get_opening_article_tag();

                                        if( $image ) :

                                            Query_Manager::get_opening_figure_tag();
    
                                                Query_Manager::get_featured_image( Theme_Defaults::get_theme_option( 'main_banner_right_image_sizes' ), [ 'post_format' => ( $layout === 'grid' ), 'skip_filter' => true ] );
                                            
                                            if( $layout !== 'overlay' ) Query_Manager::get_closing_figure_tag();

                                        endif;

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

                                                if( $category ) Query_Manager::get_categories();

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

                                                if( $excerpt ) Query_Manager::get_excerpt();

                                                if( $meta ) :

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

                                                        Query_Manager::get_author();

                                                        Query_Manager::get_date();

                                                    echo '</div>';

                                                endif;
                                            
                                            echo '</div>';

                                        if( $image && ( $layout === 'overlay' ) ) Query_Manager::get_closing_figure_tag();

                                    Query_Manager::get_closing_article_tag();

                                endwhile;

                                wp_reset_postdata();

                            ?>

                        </div>
                        
                    </div><!-- .bbe-main-banner-right -->
                <?php
            }

            /**
             * Enable scroll or not
             * 
             * @since 1.0.0
             */
            private function check_for_scroll() {
                $post_count = $this->get_query_instance()->post_count;
                if( ( $this->get_layout() === 'list' ) && ( $post_count <= 5 ) ) {
                    return true;
                } elseif ( in_array( $this->get_layout(), [ 'grid', 'overlay' ] ) && ( $post_count <= 2 ) ) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        Main_Banner_Right::get_instance();
    }