<?php
    /**
     * Carousel 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 get_template_directory_uri;
    use function blog_bee_posted_by;
    use function blog_bee_posted_on;
    use function get_post_format;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'carousel_section' );
                $this->add_control( 'carousel_section_tab' );
                $this->add_control( 'carousel_section_label' );
                $this->add_control( 'carousel_container' );
                $this->add_control( 'carousel_post_query_settings' );
                $this->add_control( 'carousel_post_source' );
                $this->add_control( 'carousel_categories' );
                $this->add_control( 'carousel_posts_to_include' );
                $this->add_control( 'carousel_number_of_posts' );
                $this->add_control( 'carousel_orderby' );
                $this->add_control( 'carousel_hide_no_featured' );
                $this->add_control( 'carousel_post_elements_settings' );
                $this->add_control( 'carousel_content_toggles' );
                $this->add_control( 'carousel_content_alignment' );
                $this->add_control( 'carousel_image_settings' );
                $this->add_control( 'carousel_image_sizes' );
                $this->tab = 'design';
                $this->add_control( 'carousel_title_typography' );
                $this->add_control( 'carousel_section_background' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'carousel_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_section_label' =>  [
                        'sanitize_callback' =>  'sanitize_text_field',
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_container' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_post_query_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_post_source' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_categories' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_async_multiselect' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_posts_to_include' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_async_multiselect' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_number_of_posts' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_range' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_orderby' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_hide_no_featured' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_post_elements_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_content_toggles' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle_group' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_content_alignment' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_image_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_image_sizes' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_title_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'carousel_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 = [
                    'carousel_section' =>  [
                        'title' =>  esc_html__( 'Carousel', 'blog-bee' ),
                        'active_callback'   =>  function() {
                            return Helpers::get_reorder_value( 'section_reorder_fields', 'carousel' );
                        },
                        'section_to_open' =>  'section_reorder_section'
                    ],
                    'carousel_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'carousel_section_label' =>  [
                        'label' =>  esc_html__( 'Section Label', 'blog-bee' ),
                        'type'  =>  'bbe-text'
                    ],
                    'carousel_container' =>  [
                        'label' =>  esc_html__( 'Container', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  Helpers::get_card_options()
                    ],
                    'carousel_post_query_settings' =>  [
                        'label' =>  esc_html__( 'Post Query Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'carousel_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' ),
                        ]
                    ],
                    'carousel_categories' =>  [
                        'label' =>  esc_html__( 'Post Categories', 'blog-bee' ),
                        'type'  =>  'async-multiselect',
                        'endpoint'  =>  'get_taxonomy',
                        'purpose'   =>  'category',
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'carousel_post_source',
                                    'operator'  =>  '==',
                                    'value' =>  'categories'
                                ]
                            ]
                        ]
                    ],
                    'carousel_posts_to_include' =>  [
                        'label' =>  esc_html__( 'Post to Include', 'blog-bee' ),
                        'type'  =>  'async-multiselect',
                        'endpoint'  =>  'get_posts',
                        'purpose'   =>  'post',
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'carousel_post_source',
                                    'operator'  =>  '==',
                                    'value' =>  'manual'
                                ]
                            ]
                        ]
                    ],
                    'carousel_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
                        ]
                    ],
                    'carousel_orderby' =>  [
                        'label' =>  esc_html__( 'Order By', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'   =>  Helpers::get_order_by()
                    ],
                    'carousel_hide_no_featured' =>  [
                        'label' =>  esc_html__( 'Hide Posts With No Featured Image', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'carousel_post_elements_settings' =>  [
                        'label' =>  esc_html__( 'Post Elements Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'carousel_content_toggles' =>  [
                        'type'  =>  'toggle-button-group',
                        'fields'    =>  [
                            [
                                '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'
                            ],
                        ]
                    ],
                    'carousel_content_alignment' =>  [
                        'label' =>  esc_html__( 'Alignment', 'blog-bee' ),
                        'type'  =>  'radio-tab',
                        'fields'    =>  Helpers::get_customizer_alignments()
                    ],
                    'carousel_image_settings' =>  [
                        'label' =>  esc_html__( 'Image Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle',
                    ],
                    'carousel_image_sizes' =>  [
                        'label' =>  esc_html__( 'Image Sizes', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'   =>  Helpers::get_customizer_image_sizes()
                    ],
                    'carousel_title_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'carousel_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( [
                    'carousel_section_tab' =>  'general',
                    'carousel_layouts' =>  'one',
                    'carousel_section_label' =>  'Carousel',
                    'carousel_view_all_or_dots' =>  'dots',
                    'carousel_view_all_button_settings' =>  false,
                    'carousel_view_all_button_label'  =>  'View all',
                    'carousel_view_all_button_url'  =>  esc_url( home_url() ),
                    'carousel_view_all_target'  =>  '_blank',
                    'carousel_view_all_rel_attribute'  =>  'noopener noreferrer',
                    'carousel_view_all_button_style'    =>  'filled circle',
                    'carousel_container'    =>  'default',
                    'carousel_post_query_settings' =>  false,
                    'carousel_post_source' =>  'latest',
                    'carousel_categories' =>  [],
                    'carousel_tags' =>  [],
                    'carousel_authors' =>  [],
                    'carousel_posts_to_include' =>  [],
                    'carousel_number_of_posts' =>  5,
                    'carousel_posts_offset' =>  0,
                    'carousel_orderby' =>  'date-desc',
                    'carousel_hide_no_featured' =>  false,
                    'carousel_post_elements_settings' =>  false,
                    'carousel_content_toggles'  =>  [
                        'title' =>  true,
                        'category'  =>  true,
                        'date'  =>  true,
                        'comments'  =>  false,
                        'author'    =>  true,
                        'read-time' =>  true,
                        'read-more' =>  false,
                        'excerpt'   =>  false,
                    ],
                    'carousel_content_alignment' =>  'left',
                    'carousel_excerpt_length' =>  25,
                    'carousel_slider_settings' =>  false,
                    'carousel_content_slider_toggles'  =>  [
                        'arrows' =>  true,
                        'autoplay'  =>  true,
                        'centerMode'  =>  false,
                    ],
                    'carousel_arrows_position' =>  'bottom',
                    'carousel_speed' =>  300,
                    'carousel_slides_to_show' =>  4,
                    'carousel_image_settings' =>  '',
                    'carousel_image_sizes' =>  'bbe_featured_large',
                    'carousel_image_ratio' =>  'boxy',
                    'carousel_width_layout' =>  'boxed',
                    'carousel_title_typography'    =>  $this->get_typography( [
                        'font_size' =>  $this->get_responsive( '18px', '16px', '14px' ),
                    ] ),
                    'carousel_section_background'    =>  $this->get_color( [ 'value' => '' ] ),
                    'carousel_gap'    =>  '15px',
                    'carousel_view_all_button_design_settings'    =>  false,
                    'carousel_view_all_button_color'    =>  [
                        'initial'   =>  $this->get_color(),
                        'hover'   =>  $this->get_color(),
                    ],
                    'carousel_view_all_button_background'    =>  [
                        'initial'   =>  $this->get_color(),
                        'hover'   =>  $this->get_color(),
                    ],
                    'carousel_view_all_button_border_color'    =>  [
                        'initial'   =>  $this->get_color(),
                        'hover'   =>  $this->get_color(),
                    ],
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! Helpers::get_reorder_value( 'section_reorder_fields', 'carousel' ) ) return [];
                $dynamic_args = [
                    'carousel_section_label'  =>  [
                        'selector'  =>  '.bbe-carousel .section-head .label',
                        'parent'  =>  '.bbe-carousel .section-head',
                    ],
                    'carousel_container'    =>  [
                        'selector'  =>  '.bbe-carousel',
                        'prefix'    =>  'card--'
                    ],
                    'carousel_content_alignment'  =>  [
                        'selector'  =>  '.bbe-carousel',
                        'prefix'  =>  'align--'
                    ],
                    'carousel_title_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'carousel_title_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'carousel_title_typography' ),
                        'selector'  =>  '.bbe-carousel .entry-inner .entry-title, .bbe-carousel .entry-inner .entry-title a',
                    ],
                    'carousel_section_background' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'carousel_section_background' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'carousel_section_background' ),
                        'selector'  =>  '.bbe-light .bbe-carousel',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $category = Query_Manager::get_ids_only( Theme_Defaults::get_theme_option( 'carousel_categories' ) );
                $category = ! empty( $category ) ? explode( ',', $category ) : [];
                $query_attrs = [
                    'post_source'  =>  apply_filters( 'blog_bee_carousel_post_source_filter', Theme_Defaults::get_theme_option( 'carousel_post_source' ) ),
                    'category__in'  =>  apply_filters( 'blog_bee_carousel_cat_array_filter', $category ),
                    'post__in'  =>  Theme_Defaults::get_theme_option( 'carousel_posts_to_include' ),
                    'posts_per_page'  =>  Theme_Defaults::get_theme_option( 'carousel_number_of_posts' ),
                    'orderby'  =>  Theme_Defaults::get_theme_option( 'carousel_orderby' ),
                    'hide_featured'  =>  Theme_Defaults::get_theme_option( 'carousel_hide_no_featured' ),
                ];
                $query_instance = Query_Manager::fine_tune_query_attrs( $query_attrs, true );
                if( ! $query_instance->have_posts() ) return;
                
                $block_class[] = self::$id;
                $layout = Theme_Defaults::get_theme_option( 'carousel_layouts' );
                $block_class[] = 'layout--' . $layout;
                $block_class[] = 'width--' . Theme_Defaults::get_theme_option( 'carousel_width_layout' );
                $block_class[] = 'align--' . Theme_Defaults::get_theme_option( 'carousel_content_alignment' );
                $block_class[] = 'card--' . Theme_Defaults::get_theme_option( 'carousel_container' );
                $block_class[] = 'ratio--' . Theme_Defaults::get_theme_option( 'carousel_image_ratio' );
                $arrow_position = 'bottom';
                $slider_toggles = Theme_Defaults::get_theme_option( 'carousel_content_slider_toggles' );
                $default_slider_toggles = Theme_Defaults::get_defaults( 'carousel_content_slider_toggles' );
                $arrows = isset( $slider_toggles[ 'arrows' ] ) ? $slider_toggles[ 'arrows' ] : $default_slider_toggles[ 'arrows' ];
                if( $arrows ) {
                    $arrow_position = Theme_Defaults::get_theme_option( 'carousel_arrows_position' );
                    $block_class[] = 'arrow--' . $arrow_position;
                }

                $view_all = Theme_Defaults::get_theme_option( 'carousel_view_all_or_dots' );
                $section_label = Theme_Defaults::get_theme_option( 'carousel_section_label' );
                $content_toggles = Theme_Defaults::get_theme_option( 'carousel_content_toggles' );
                $default_content_toggles = Theme_Defaults::get_defaults( 'carousel_content_toggles' );
                $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' ];
                $block_class = apply_filters( 'blog_bee_carousel_classes_filter', $block_class );
                ?>
                    <section class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>" <?php Helpers::the_aos_data_attr(); ?>>
                        <?php 
                            if( ! in_array( 'is-widget', $block_class ) ) :
                                echo '<div class="bbe-container"><div class="row">';
                                Helpers::get_section_head( $section_label );
                            endif;
                            if( $view_all === 'none' || $arrow_position === 'center' ) echo '<div class="entry-content-wrap">';
                        ?>
                        <div class="entry-content">
                            <?php
                                while( $query_instance->have_posts() ) {
                                    $query_instance->the_post();
                                    $post_format = get_post_format();

                                    Query_Manager::get_opening_article_tag();

                                        Query_Manager::get_opening_figure_tag();

                                            Query_Manager::get_featured_image( Theme_Defaults::get_theme_option( 'carousel_image_sizes' ) );

                                        if( ( $layout !== 'two' && ( $post_format !== 'image' ) ) || ( $layout === 'three' ) ) Query_Manager::get_closing_figure_tag();

                                            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( Theme_Defaults::get_theme_option( 'carousel_excerpt_length' ) );

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

                                            echo '</div>';
                                            
                                        if( ( $layout === 'two' ) || ( ( $layout === 'one' ) && ( $post_format === 'image' ) ) ) Query_Manager::get_closing_figure_tag();

                                    Query_Manager::get_closing_article_tag();
                                }
                                wp_reset_postdata();
                            ?>
                        </div>
                        <?php
                            if( $view_all === 'none' || $arrow_position === 'center' ) :
                                Helpers::get_slick_arrows( true, true, '', $arrows );
                                echo '</div><!-- .entry-content-wrap -->';
                            endif;
                            $this->slider_arrows( ( ( $arrow_position !== 'center' ) && ( $view_all !== 'none' ) && $arrows ) );
                            if( ! in_array( 'is-widget', $block_class ) ) :
                                echo '</div></div>';
                            endif;
                        ?>
                    </section><!-- .bbe-carousel -->
                <?php
            }

            /**
             * Get slider arrows
             * 
             * @param string $render Whether or not to display arrows
             * @since 1.0.0
             */
            private function slider_arrows( $render = true ) {
                $view_all = Theme_Defaults::get_theme_option( 'carousel_view_all_or_dots' );
                if( $view_all === 'none' ) return;
                if( $view_all === 'all' ) {
                    $url = Theme_Defaults::get_theme_option( 'carousel_view_all_button_url' );
                    $rel = Theme_Defaults::get_theme_option( 'carousel_view_all_rel_attribute' );
                    $style = Theme_Defaults::get_theme_option( 'carousel_view_all_button_style' );
                    $label = Theme_Defaults::get_theme_option( 'carousel_view_all_button_label' );
                    $target = Theme_Defaults::get_theme_option( 'carousel_view_all_target' );
                    $icon_attrs = [
                        'type'  =>  'icon',
                        'value' =>  'fa-solid fa-chevron-right',
                        'tag'   =>  'span'
                    ];

                    $view_all_class[] = 'view-all';
                    $view_all_class[] = Theme_Defaults::get_theme_option( 'carousel_view_all_button_style' );
                    
                    $children = '<a class="' . esc_attr( implode( ' ', $view_all_class ) ) . '" href="' . esc_url( $url ) . '" rel="'. esc_attr( $rel ) .'" target="' . esc_attr( $target ) . '">';
                    $children .= '<span class="label">' . esc_html( $label ) . '</span>';
                    $children .= Helpers::get_icon_html( $icon_attrs );
                    $children .= '</a>';
                } else {
                    $class = 'bbe-slick-dots type--' . $view_all;
                    $children = '<div class="' . esc_attr( $class ) . '"></div>';
                }
                Helpers::get_slick_arrows( true, true, $children, $render );
            }
        }
        Carousel::get_instance();
    }