<?php
    /**
     * Ticker News 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 as Helpers;
    use Utility;
    use WP_Query;
    use BBE\Query_Manager as Query_Manager;

    use function esc_html__;
    use function esc_attr;
    use function esc_html;
    use function absint;
    use function wp_reset_postdata;

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

            /**
             * Is section active
             * 
             * @since 1.0.0
             */
            public function is_active(): bool {
                $header_builder = Helpers::is_widget_in_builder( 'header_builder', 'ticker-news' );
                $header_responsive_builder = Helpers::is_widget_in_builder( 'header_responsive_builder', 'ticker-news' );
                return ( $header_builder || $header_responsive_builder );
            }

            /**
             * Query Instance 
             * 
             * @since 1.0.0
             */
            private $query_instance = null;

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'ticker_news_section' );
                $this->add_control( 'ticker_news_section_tab' );
                $this->add_control( 'ticker_news_label_settings' );
                $this->add_control( 'ticker_news_label' );
                $this->add_control( 'ticker_news_post_queries_settings' );
                $this->add_control( 'ticker_news_post_source' );
                $this->add_control( 'ticker_news_categories' );
                $this->add_control( 'ticker_news_posts_to_include' );
                $this->add_control( 'ticker_news_number_of_posts' );
                $this->add_control( 'ticker_news_orderby' );
                $this->add_control( 'ticker_news_post_elements_settings' );
                $this->add_control( 'ticker_news_show_featured_image' );
                $this->add_control( 'ticker_news_show_date' );
                $this->tab = 'design';
                $this->add_control( 'ticker_news_design_label_settings' );
                $this->add_control( 'ticker_news_label_typography' );
                $this->add_control( 'ticker_news_label_color' );
                $this->add_control( 'ticker_news_content_settings' );
                $this->add_control( 'ticker_news_title_typography' );
                $this->add_control( 'ticker_news_title_color' );
                $this->add_control( 'ticker_news_background' );
                $this->add_control( 'ticker_news_controller_settings' );
                $this->add_control( 'ticker_news_controller_icon_color' );
                $this->add_control( 'ticker_news_controller_border_color' );
                $this->add_control( 'ticker_news_controller_border_radius' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'ticker_news_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_label_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_label' =>  [
                        'sanitize_callback' =>  'sanitize_text_field',
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_post_queries_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_post_source' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_categories' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_async_multiselect' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_posts_to_include' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_async_multiselect' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_number_of_posts' =>  [
                        'sanitize_callback' =>  'absint',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_orderby' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_post_elements_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_show_featured_image' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_show_date' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_design_label_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_label_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_label_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_content_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_title_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_title_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_background' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_controller_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_controller_icon_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_controller_border_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'ticker_news_controller_border_radius' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_dimension' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $controls = [
                    'ticker_news_section' =>  [
                        'title' =>  esc_html__( 'Ticker News', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'ticker_news_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'ticker_news_label_settings' =>  [
                        'label' =>  esc_html__( 'Label Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle',
                    ],
                    'ticker_news_label' =>  [
                        'label' =>  esc_html__( 'Label', 'blog-bee' ),
                        'type'  =>  'bbe-text',
                    ],
                    'ticker_news_post_queries_settings' =>  [
                        'label' =>  esc_html__( 'Post Query Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle',
                    ],
                    'ticker_news_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' ),
                        ]
                    ],
                    'ticker_news_categories' =>  [
                        'label' =>  esc_html__( 'Post Categories', 'blog-bee' ),
                        'type'  =>  'async-multiselect',
                        'endpoint'  =>  'get_taxonomy',
                        'purpose'   =>  'category',
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'ticker_news_post_source',
                                    'operator'  =>  '==',
                                    'value' =>  'categories'
                                ]
                            ]
                        ]
                    ],
                    'ticker_news_posts_to_include' =>  [
                        'label' =>  esc_html__( 'Post to Include', 'blog-bee' ),
                        'type'  =>  'async-multiselect',
                        'endpoint'  =>  'get_posts',
                        'purpose'   =>  'post',
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'ticker_news_post_source',
                                    'operator'  =>  '==',
                                    'value' =>  'manual'
                                ]
                            ]
                        ]
                    ],
                    'ticker_news_number_of_posts' =>  [
                        'label' =>  esc_html__( 'Number of Posts', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'show_unit' =>  false,
                        'input_attrs'   =>  [
                            'min'   =>  1,
                            'max'   =>  5,
                            'step'  =>  1
                        ]
                    ],
                    'ticker_news_orderby' =>  [
                        'label' =>  esc_html__( 'Order', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'   =>  Helpers::get_order_by()
                    ],
                    'ticker_news_post_elements_settings' =>  [
                        'label' =>  esc_html__( 'Post Elements Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'ticker_news_show_featured_image' =>  [
                        'label' =>  esc_html__( 'Show featured image', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'ticker_news_show_date' =>  [
                        'label' =>  esc_html__( 'Show date', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'ticker_news_hide_no_featured' =>  [
                        'label' =>  esc_html__( 'Hide Posts With No Featured Image', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'ticker_news_design_label_settings' =>  [
                        'label' =>  esc_html__( 'Label Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle',
                    ],
                    'ticker_news_label_typography' =>  [
                        'label' =>  esc_html__( 'Label Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'ticker_news_label_color' =>  [
                        'label' =>  esc_html__( 'Label Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                    ],
                    'ticker_news_content_settings' =>  [
                        'label' =>  esc_html__( 'Content Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle',
                    ],
                    'ticker_news_title_typography' =>  [
                        'label' =>  esc_html__( 'Title Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'ticker_news_title_color' =>  [
                        'label' =>  esc_html__( 'Title Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'ticker_news_background' =>  [
                        'label' =>  esc_html__( 'Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'color_types'   =>  [ 'solid', 'gradient' ]
                    ],
                    'ticker_news_controller_settings' =>  [
                        'label' =>  esc_html__( 'Controller Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'ticker_news_controller_icon_color' =>  [
                        'label' =>  esc_html__( 'Icon Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                    ],
                    'ticker_news_controller_border_color' =>  [
                        'label' =>  esc_html__( 'Border Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'ticker_news_controller_border_radius' =>  [
                        'label' =>  esc_html__( 'Border Radius', 'blog-bee' ),
                        'type'  =>  'dimension'
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'ticker_news_section_tab' =>  'general',
                    'ticker_news_slider_type' =>  'marquee',
                    'ticker_news_label_settings'    =>  true,
                    'ticker_news_label' =>  'Breaking News',
                    'ticker_news_post_queries_settings' =>  false,
                    'ticker_news_post_source' =>  'latest',
                    'ticker_news_categories' =>  [],
                    'ticker_news_posts_to_include' =>  [],
                    'ticker_news_number_of_posts'   =>  5,
                    'ticker_news_orderby' =>  'date-desc',
                    'ticker_news_post_elements_settings'    =>  false,
                    'ticker_news_show_featured_image'   =>  false,
                    'ticker_news_show_date' =>  true,
                    'ticker_news_design_label_settings' =>  true,
                    'ticker_news_label_typography'  =>  $this->get_typography( [
                        'font_size' =>  $this->get_responsive( '14px', '14px', '14px' ),
                        'line_height'   =>  $this->get_responsive( 1.4, 1.4, 1.4 ),
                        'letter_spacing'    =>  $this->get_responsive( 0.2, 0.2, 0.2 )
                    ] ),
                    'ticker_news_label_color'   =>  $this->get_color( [ 'value' => '--bbe-theme-color' ] ),
                    'ticker_news_content_settings'  =>  false,
                    'ticker_news_title_typography'  =>  $this->get_typography( [
                        'font_weight'   =>  '600'
                    ] ),
                    'ticker_news_title_color'   =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                        'hover'   =>  $this->get_color(),
                    ],
                    'ticker_news_background'    =>  $this->get_color( [ 'value' => '' ] ),
                    'ticker_news_controller_settings'    =>  false,
                    'ticker_news_controller_icon_color'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                    'ticker_news_controller_border_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' =>  '#e5e7eb' ] ),
                        'hover'   =>  $this->get_color( [ 'value' =>  '#e5e7eb' ] ),
                    ],
                    'ticker_news_controller_border_radius'    =>  $this->get_dimension(),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! $this->is_active() ) return [];
                $dynamic_args = [
                    'ticker_news_label' =>  [
                        'selector'  =>  '.bbe-ticker-news .label',
                        'parent'    =>  '.bbe-ticker-news',
                        'context'   =>  'before'
                    ],
                    'ticker_news_label_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'ticker_news_label_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'ticker_news_label_typography' ),
                        'selector'  =>  '.bbe-header .bbe-ticker-news .label',
                    ],
                    'ticker_news_label_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'ticker_news_label_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'ticker_news_label_color' ),
                        'selector'  =>  '.bbe-header .bbe-ticker-news .label',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'ticker_news_title_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'ticker_news_title_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'ticker_news_title_typography' ),
                        'selector'  =>  '.bbe-header .bbe-ticker-news .bbe-article .entry-title, .bbe-header .bbe-ticker-news .bbe-article .entry-title a',
                    ],
                    'ticker_news_title_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'ticker_news_title_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'ticker_news_title_color' ),
                        'selector'  =>  '.bbe-header .bbe-ticker-news .bbe-article .entry-title a',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'ticker_news_background' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'ticker_news_background' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'ticker_news_background' ),
                        'selector'  =>  '.bbe-header .bbe-ticker-news .entry-content',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                    'ticker_news_controller_icon_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'ticker_news_controller_icon_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'ticker_news_controller_icon_color' ),
                        'selector'  =>  '.bbe-header .bbe-ticker-news .controller-wrap .icon i',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'ticker_news_controller_border_color' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'ticker_news_controller_border_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'ticker_news_controller_border_color' ),
                        'selector'  =>  '.bbe-header .bbe-ticker-news .controller-wrap .slick-arrow, .bbe-header .bbe-ticker-news.slider--marquee .controller-wrap',
                        'property'  =>  '--bbe-site-border-color-muted'
                    ],
                    'ticker_news_controller_border_radius'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'ticker_news_controller_border_radius' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'ticker_news_controller_border_radius' ),
                        'selector'  =>  '.bbe-header .bbe-ticker-news .controller-wrap .slick-arrow, .bbe-header .bbe-ticker-news.slider--marquee .controller-wrap',
                        'property'  =>  'border-radius'
                    ],
                ];
                return $dynamic_args;
            }

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

                $block_class[] = self::$id;
                $slider_type = Theme_Defaults::get_theme_option( 'ticker_news_slider_type' );
                $block_class[] = 'slider--' . $slider_type;

                $label = Theme_Defaults::get_theme_option( 'ticker_news_label' );

                $is_marquee = ( $slider_type === 'marquee' );

                ?>
                    <div class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>">
                        <?php
                            if( $label ) echo '<span class="label">' . esc_html( $label ) . '</span>';
                        
                            if( $is_marquee ) echo '<div class="entry-content-wrapper"><div class="entry-track">';

                                if( $is_marquee ) $this->get_entry_content();
                            
                                $this->get_entry_content();

                            if( $is_marquee ) echo '</div></div>';

                            if( $slider_type === 'slick' ) Helpers::get_slick_arrows( true, true );

                            if( $is_marquee ) $this->controller_html();
                            
                        ?>
                    </div><!-- .bbe-ticker-news -->
                <?php
            }

            /**
             * Get query instance
             * 
             * @since 1.0.0
             */
            private function get_query_instance() {
                if( $this->query_instance !== null ) return $this->query_instance;
                $this->query_instance = Query_Manager::fine_tune_query_attrs( [
                    'posts_per_page'    =>  'ticker_news_number_of_posts',
                    'orderby'   =>  'ticker_news_orderby',
                    'cat'   =>  'ticker_news_categories',
                    'post__in'  =>  'ticker_news_posts_to_include',
                    'post_source'  =>  'ticker_news_post_source',
                ] );
                if( ! $this->query_instance->have_posts() ) return null;
                return $this->query_instance;
            }

            /**
             * Get entry content
             * 
             * @since 1.0.0
             */
            private function get_entry_content() {
                $query_instance = self::get_query_instance();
                if( ! $query_instance ) return;

                $featured_image = Theme_Defaults::get_theme_option( 'ticker_news_show_featured_image' );
                $date = Theme_Defaults::get_theme_option( 'ticker_news_show_date' );
                ?>

                    <div class="entry-content">

                        <?php

                            while( $query_instance->have_posts() ) : $query_instance->the_post();

                                Query_Manager::get_opening_article_tag();

                                    if( $featured_image ) :
                                        
                                        Query_Manager::get_opening_figure_tag();
                                        
                                        Query_Manager::get_featured_image( 'thumbnail', [ 'post_format' => false ] );
                                        
                                        Query_Manager::get_closing_figure_tag();

                                    endif;

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

                                        Query_Manager::get_title();

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

                                    echo '</div>';

                                Query_Manager::get_closing_article_tag();

                            endwhile;

                            wp_reset_postdata()

                        ?>

                    </div>

                <?php
            }

            /**
             * Controller htmls
             * 
             * @since 1.0.0
             */
            private function controller_html() {
                $enable_controller = true;
                if( ! $enable_controller ) return;

                $controller_icon_attributes = [
                    'type'  =>  'icon',
                    'value' =>  'fa-solid fa-pause',
                    'class' =>  'controller-icon',
                    'aria_label'    =>  esc_html__( 'Play / Pause Ticker', 'blog-bee' )
                ];

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

                    echo Helpers::get_icon_html( $controller_icon_attributes );

                echo '</div>';
            }
        }
        Ticker_News::get_instance();
    }