<?php
    /**
     * Post List 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__ . '\\Post_List' ) ) {
        /**
         * Post_List class
         */
        class Post_List extends Section {
            /**
             * Section Id
             * 
             * @since 1.0.0
             * @var string
             */
            public static $id = 'bbe-post-list';

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->add_section( 'post_list_section' );
                $this->add_control( 'post_list_section_tab' );
                $this->add_control( 'post_list_section_label' );
                $this->add_control( 'post_list_number_of_column' );
                $this->add_control( 'post_list_container' );
                $this->add_control( 'post_list_post_query_settings' );
                $this->add_control( 'post_list_post_source' );
                $this->add_control( 'post_list_categories' );
                $this->add_control( 'post_list_posts_to_include' );
                $this->add_control( 'post_list_number_of_posts' );
                $this->add_control( 'post_list_orderby' );
                $this->add_control( 'post_list_hide_no_featured' );
                $this->add_control( 'post_list_post_elements_settings' );
                $this->add_control( 'post_list_content_toggles' );
                $this->add_control( 'post_list_content_alignment' );
                $this->add_control( 'post_list_image_settings' );
                $this->add_control( 'post_list_image_sizes' );
                $this->tab = 'design';
                $this->add_control( 'post_list_title_typography' );
                $this->add_control( 'post_list_section_background' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'post_list_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_section_label' =>  [
                        'sanitize_callback' =>  'sanitize_text_field',
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_number_of_column' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_range' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_container' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_post_query_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_post_source' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_categories' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_async_multiselect' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_posts_to_include' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_async_multiselect' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_number_of_posts' =>  [
                        'sanitize_callback' =>  'absint',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_orderby' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_hide_no_featured' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_post_elements_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_content_toggles' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle_group' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_content_alignment' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_image_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_image_sizes' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_title_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_list_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 = [
                    'post_list_section' =>  [
                        'title' =>  esc_html__( 'Post List', 'blog-bee' ),
                        'active_callback'   =>  function() {
                            return Helpers::get_reorder_value( 'section_reorder_fields', 'post-list' );
                        },
                        'section_to_open' =>  'section_reorder_section'
                    ],
                    'post_list_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'  =>  Helpers::get_customizer_section_tabs()
                    ],
                    'post_list_section_label' =>  [
                        'label' =>  esc_html__( 'Section Label', 'blog-bee' ),
                        'type'  =>  'bbe-text',
                    ],
                    'post_list_number_of_column' =>  [
                        'label'    =>  esc_html__( 'No. of Columns', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'responsive'    =>  true,
                        'show_unit' =>  false,
                        'input_attrs'   =>  [
                            'min'   =>  2,
                            'max'   =>  4,
                            'step'  =>  1
                        ],
                    ],
                    'post_list_container' =>  [
                        'label' =>  esc_html__( 'Container', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  Helpers::get_card_options()
                    ],
                    'post_list_post_query_settings' =>  [
                        'label' =>  esc_html__( 'Post Query Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'post_list_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' ),
                        ]
                    ],
                    'post_list_categories' =>  [
                        'label' =>  esc_html__( 'Post Categories', 'blog-bee' ),
                        'type'  =>  'async-multiselect',
                        'endpoint'  =>  'get_taxonomy',
                        'purpose'   =>  'category',
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'post_list_post_source',
                                    'operator'  =>  '==',
                                    'value' =>  'categories'
                                ]
                            ]
                        ]
                    ],
                    'post_list_posts_to_include' =>  [
                        'label' =>  esc_html__( 'Post to Include', 'blog-bee' ),
                        'type'  =>  'async-multiselect',
                        'endpoint'  =>  'get_posts',
                        'purpose'   =>  'post',
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'post_list_post_source',
                                    'operator'  =>  '==',
                                    'value' =>  'manual'
                                ]
                            ]
                        ]
                    ],
                    'post_list_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
                        ]
                    ],
                    'post_list_orderby' =>  [
                        'label' =>  esc_html__( 'Order By', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'   =>  Helpers::get_order_by()
                    ],
                    'post_list_hide_no_featured' =>  [
                        'label' =>  esc_html__( 'Hide Posts With No Featured Image', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'post_list_post_elements_settings' =>  [
                        'label' =>  esc_html__( 'Post Elements Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'post_list_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__( 'Category', 'blog-bee' ),
                                'id' =>  'category'
                            ],
                            [
                                'label' =>  esc_html__( 'Date', 'blog-bee' ),
                                'id' =>  'date'
                            ],
                            [
                                'label' =>  esc_html__( 'Comments', 'blog-bee' ),
                                'id' =>  'comments'
                            ],
                            [
                                'label' =>  esc_html__( 'Author', 'blog-bee' ),
                                'id' =>  'author'
                            ],
                            [
                                'label' =>  esc_html__( 'Read Time', 'blog-bee' ),
                                'id' =>  'read-time'
                            ],
                            [
                                'label' =>  esc_html__( 'Read More', 'blog-bee' ),
                                'id' =>  'read-more'
                            ],
                            [
                                'label' =>  esc_html__( 'Excerpt', 'blog-bee' ),
                                'id' =>  'excerpt'
                            ],
                        ]
                    ],
                    'post_list_content_alignment' =>  [
                        'label' =>  esc_html__( 'Alignment', 'blog-bee' ),
                        'type'  =>  'radio-tab',
                        'fields'    =>  Helpers::get_customizer_alignments()
                    ],
                    'post_list_image_settings' =>  [
                        'label' =>  esc_html__( 'Image Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle',
                    ],
                    'post_list_image_sizes' =>  [
                        'label' =>  esc_html__( 'Image Sizes', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'   =>  Helpers::get_customizer_image_sizes()
                    ],
                    'post_list_title_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'post_list_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( [
                    'post_list_section_tab' =>  'general',
                    'post_list_section_label' =>  'Post List',
                    'post_list_number_of_column' =>  $this->get_responsive( 3, 2, 1 ),
                    'post_list_container' =>  'default',
                    'post_list_post_query_settings' =>  false,
                    'post_list_post_source' =>  'latest',
                    'post_list_categories' =>  [],
                    'post_list_tags' =>  [],
                    'post_list_authors' =>  [],
                    'post_list_posts_to_include' =>  [],
                    'post_list_number_of_posts' =>  3,
                    'post_list_posts_offset' =>  0,
                    'post_list_orderby' =>  'date-desc',
                    'post_list_hide_no_featured' =>  false,
                    'post_list_post_elements_settings' =>  false,
                    'post_list_content_toggles'  =>  [
                        'image' =>  true,
                        'title' =>  true,
                        'category'  =>  true,
                        'date'  =>  false,
                        'comments'  =>  false,
                        'author'    =>  false,
                        'read-time' =>  false,
                        'read-more' =>  false,
                        'excerpt'   =>  false,
                    ],
                    'post_list_content_alignment' =>  'left',
                    'post_list_image_settings' =>  '',
                    'post_list_image_sizes' =>  'bbe_content_width',
                    'post_list_image_ratio' =>  'boxy',
                    'post_list_width_layout' =>  'boxed',
                    'post_list_title_typography'    =>  $this->get_typography( [
                        'font_weight'   =>  '600',
                        'font_size' =>  $this->get_responsive( '16px', '16px', '16px' ),
                        'line_height'   =>  $this->get_responsive( 1.48, 1.48, 1.48 ),
                    ] ),
                    'post_list_section_background'    =>  $this->get_color( [ 'value' => '' ] )
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args() {
                $dynamic_css = [
                    'post_list_section_label'  =>  [
                        'selector'  =>  '.bbe-post-list .section-head .label',
                        'parent'  =>  '.bbe-post-list .section-head',
                    ],
                    'post_list_container'    =>  [
                        'selector'  =>  '.bbe-post-list',
                        'prefix'    =>  'card--'
                    ],
                    'post_list_content_alignment'  =>  [
                        'selector'  =>  '.bbe-post-list',
                        'prefix'  =>  'align--'
                    ],
                    'post_list_title_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'post_list_title_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'post_list_title_typography' ),
                        'selector'  =>  'body .site .bbe-post-list .bbe-article .entry-title, body .site .bbe-post-list .bbe-article .entry-title a',
                    ],
                    'post_list_section_background'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'post_list_section_background' ),
                        'default' =>  Theme_Defaults::get_defaults( 'post_list_section_background' ),
                        'selector'  =>  '.bbe-light .bbe-post-list',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                ];
                return $dynamic_css;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $query_attrs = [
                    'post_source'  =>  'post_list_post_source',
                    'cat'  =>  'post_list_categories',
                    'tag__in'  =>  'post_list_tags',
                    'author'  =>  'post_list_authors',
                    'post__in'  =>  'post_list_posts_to_include',
                    'posts_per_page'  =>  'post_list_number_of_posts',
                    'offset'  =>  'post_list_posts_offset',
                    'orderby'  =>  'post_list_orderby',
                    'hide_featured'  =>  'post_list_hide_no_featured',
                ];
                $query_instance = Query_Manager::fine_tune_query_attrs( $query_attrs );
                if( ! $query_instance->have_posts() ) return;
                $column = Theme_Defaults::get_theme_option( 'post_list_number_of_column' );

                $block_class[] = self::$id;
                $block_class[] = 'card--' . Theme_Defaults::get_theme_option( 'post_list_container' );
                $block_class[] = 'align--' . Theme_Defaults::get_theme_option( 'post_list_content_alignment' );
                $block_class[] = 'width--' . Theme_Defaults::get_theme_option( 'post_list_width_layout' );
                $block_class[] = 'ratio--' . Theme_Defaults::get_theme_option( 'post_list_image_ratio' );
                $block_class[] = 'column--' . Helpers::convert_number_to_word( $column[ 'desktop' ] );
                $block_class[] = 'tab-column--' . Helpers::convert_number_to_word( $column[ 'tablet' ] );
                $block_class[] = 'mobile-column--' . Helpers::convert_number_to_word( $column[ 'mobile' ] );

                $content_toggles = Theme_Defaults::get_theme_option( 'post_list_content_toggles' );
                $default_content_toggles = Theme_Defaults::get_defaults( 'post_list_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' ];
                $category = isset( $content_toggles[ 'category' ] ) ? $content_toggles[ 'category' ] : $default_content_toggles[ 'category' ];
                $date = isset( $content_toggles[ 'date' ] ) ? $content_toggles[ 'date' ] : $default_content_toggles[ 'date' ];
                $comments = isset( $content_toggles[ 'comments' ] ) ? $content_toggles[ 'comments' ] : $default_content_toggles[ 'comments' ];
                $author = isset( $content_toggles[ 'author' ] ) ? $content_toggles[ 'author' ] : $default_content_toggles[ 'author' ];
                $read_time = isset( $content_toggles[ 'read-time' ] ) ? $content_toggles[ 'read-time' ] : $default_content_toggles[ 'read-time' ];
                $read_more = isset( $content_toggles[ 'read-more' ] ) ? $content_toggles[ 'read-more' ] : $default_content_toggles[ 'read-more' ];
                $excerpt = isset( $content_toggles[ 'excerpt' ] ) ? $content_toggles[ 'excerpt' ] : $default_content_toggles[ 'excerpt' ];
                $count = 1;
                if( ! $image ) $block_class[] = 'image--off';
                ?>
                    <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">

                                <?php

                                    Helpers::get_section_head( Theme_Defaults::get_theme_option( 'post_list_section_label' ) );

                                    echo '<div class="article-wrap">';

                                        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( 'post_list_image_sizes' ) );
                                                    
                                                    Query_Manager::get_closing_figure_tag();

                                                endif;

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

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

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

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

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

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

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

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

                                                    echo '</div>';

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

                                                    if( $read_more ) Query_Manager::get_read_more_button();

                                                echo '</div>';

                                            Query_Manager::get_closing_article_tag();

                                        endwhile;

                                        wp_reset_postdata();

                                    echo '</div>';

                                ?>

                            </div>

                        </div>
                        
                    </section><!-- .bbe-post-list -->
                <?php
            }
        }
        post_list::get_instance();
    }