<?php
    /**
     * 3 Blocks Section
     * 
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Customizer\Section;

    use BBE\Customizer\Section\Section as Section;
    use BBE\Customizer\Section\Three_Blocks_Left as Three_Blocks_Left;
    use BBE\Customizer\Section\Three_Blocks_Right as Three_Blocks_Right;
    use BBE\Customizer\Section\Three_Blocks_Center as Three_Blocks_Center;
    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;

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

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

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'three_blocks_section' );
                $this->add_control( 'three_blocks_section_tab' );
                $this->add_control( 'three_blocks_container' );
                $this->add_control( 'three_blocks_reorder' );
                $this->add_control( 'three_blocks_left_redirect' );
                $this->add_control( 'three_blocks_center_redirect' );
                $this->add_control( 'three_blocks_right_redirect' );
                $this->tab = 'design';
                $this->add_control( 'three_blocks_section_background' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'three_blocks_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_container' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_reorder' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_reorder' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_left_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_right_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_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 = [
                    'three_blocks_section' =>  [
                        'title' =>  esc_html__( '3 Blocks', 'blog-bee' ),
                        'active_callback'   =>  function() {
                            return Helpers::get_reorder_value( 'section_reorder_fields', 'three-blocks' );
                        },
                        'section_to_open' =>  'section_reorder_section'
                    ],
                    'three_blocks_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'three_blocks_container' =>  [
                        'label' =>  esc_html__( 'Container', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  Helpers::get_card_options()
                    ],
                    'three_blocks_reorder' =>  [
                        'label' =>  esc_html__( 'Reorder', 'blog-bee' ),
                        'type'  =>  'reorder',
                        'direction' =>  'horizontal',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( '1', 'blog-bee' ),
                                'value' =>  'left',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( '2', 'blog-bee' ),
                                'value' =>  'center',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( '3', 'blog-bee' ),
                                'value' =>  'right',
                                'allow_visibility'  =>  true
                            ],
                        ]
                    ],
                    'three_blocks_left_redirect' =>  [
                        'label' =>  esc_html__( 'Edit First Blocks', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'three_blocks_left_section',
                        'active_callback'   =>  function(){
                            return Helpers::get_reorder_value( 'three_blocks_reorder', 'left' );
                        }
                    ],
                    'three_blocks_center_redirect' =>  [
                        'label' =>  esc_html__( 'Edit Second Blocks', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'three_blocks_center_section',
                        'active_callback'   =>  function(){
                            return Helpers::get_reorder_value( 'three_blocks_reorder', 'center' );
                        }
                    ],
                    'three_blocks_right_redirect' =>  [
                        'label' =>  esc_html__( 'Edit Third Blocks', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'three_blocks_right_section',
                        'active_callback'   =>  function(){
                            return Helpers::get_reorder_value( 'three_blocks_reorder', 'right' );
                        }
                    ],
                    'three_blocks_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( [
                    'three_blocks_section_tab' =>  'general',
                    'three_blocks_layouts' =>  'one',
                    'three_blocks_alter_layout' =>  false,
                    'three_blocks_container'   =>  'default',
                    'three_blocks_reorder' =>  [
                        [
                            'id'  =>  'left',
                            'value' =>  true
                        ],
                        [
                            'id'  =>  'center',
                            'value' =>  true
                        ],
                        [
                            'id'  =>  'right',
                            'value' =>  true
                        ]
                    ],
                    'three_blocks_left_redirect' =>  '',
                    'three_blocks_center_redirect' =>  '',
                    'three_blocks_right_redirect' =>  '',
                    'three_blocks_section_background'    =>  $this->get_color( [ 'value' => '' ] ),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! Helpers::get_reorder_value( 'section_reorder_fields', 'three-blocks' ) ) return [];
                $dynamic_args = [
                    'three_blocks_container'    =>  [
                        'selector'  =>  '.bbe-three-blocks',
                        'prefix'    =>  'card--'
                    ],
                    'three_blocks_section_background'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'three_blocks_section_background' ),
                        'default' =>  Theme_Defaults::get_defaults( 'three_blocks_section_background' ),
                        'selector'  =>  '.bbe-light .bbe-three-blocks',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $blocks_reorder = Theme_Defaults::get_theme_option( 'three_blocks_reorder' );
                if( empty( $blocks_reorder ) || ! is_array( $blocks_reorder ) ) return;
                
                $block_class[] = self::$id;
                $block_class[] = 'layout--' . $this->get_layout();
                $block_class[] = 'card--' . Theme_Defaults::get_theme_option( 'three_blocks_container' );

                ?>
                    <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
                                    foreach( $blocks_reorder as $block ) :

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

                                        switch ( $block[ 'id' ] ) :

                                            case 'left':
                                                    self::get_instance()->render_blocks_html( 'left' );
                                                break;

                                            case 'center':
                                                    self::get_instance()->render_blocks_html( 'center' );
                                                break;

                                            case 'right':
                                                self::get_instance()->render_blocks_html( 'right' );
                                                break;

                                        endswitch;

                                    endforeach;
                                ?>
                            </div>
                        </div>
                    </section><!-- .bbe-three-blocks -->
                <?php
            }

            /**
             * Render HTML
             * 
             * @param string $blocks
             * @since 1.0.0
             */
            public static function render_blocks_html( $blocks ) {
                $prefix = 'three_blocks_' . $blocks;
                $layout = Theme_Defaults::get_theme_option( $prefix . '_layout' );
                $query_attrs = [
                    'post_source'   =>  $prefix . '_post_source',
                    'cat'   =>  $prefix . '_categories',
                    'post__in'  =>  $prefix . '_posts_to_include',
                    'posts_per_page'    =>  $prefix . '_number_of_posts',
                    'orderby'   =>  $prefix . '_orderby',
                    'hide_featured' =>  $prefix . '_hide_no_featured',
                ];
                $query_instance = Query_Manager::fine_tune_query_attrs( $query_attrs );
                if( ! $query_instance->have_posts() ) return;

                $block_class[] = 'block-item';
                $block_class[] = $blocks . '-block';
                $overlay = Theme_Defaults::get_theme_option( $prefix . '_enable_overlay' );
                $block_class[] = 'layout--' . $layout;
                $block_class[] = 'align--' . Theme_Defaults::get_theme_option( $prefix . '_content_alignment' );

                $content_toggles = Theme_Defaults::get_theme_option( $prefix . '_content_toggles' );
                $default_content_toggles = Theme_Defaults::get_defaults( $prefix . '_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' ];
                $read_more = isset( $content_toggles[ 'read-more' ] ) ? $content_toggles[ 'read-more' ] : $default_content_toggles[ 'read-more' ];

                ?>
                    <div class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>">
                        <?php Helpers::get_section_head( Theme_Defaults::get_theme_option( $prefix . '_section_label' ) ); ?>
                        <div class="article-wrap">
                            <?php
                                while( $query_instance->have_posts() ) :
                                    $query_instance->the_post();

                                    Query_Manager::get_opening_article_tag();

                                        Query_Manager::get_opening_figure_tag();

                                            Query_Manager::get_featured_image( Theme_Defaults::get_theme_option( $prefix . '_image_sizes' ) );
                                        
                                        if( ! $overlay ) Query_Manager::get_closing_figure_tag();

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

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

                                                if( $meta ) :
                                                    
                                                    echo '<div class="entry-meta">';

                                                        Query_Manager::get_author();

                                                        Query_Manager::get_date();

                                                    echo '</div>';

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

                                                if( $read_more ) Query_Manager::get_read_more_button();
                                            
                                            echo '</div>';

                                        if( $overlay ) Query_Manager::get_closing_figure_tag();

                                    Query_Manager::get_closing_article_tag();
                                endwhile;
                                wp_reset_postdata();
                            ?>
                        </div>
                    </div>
                <?php
                echo '<!-- .bbe-three-blocks-' . esc_html( $blocks ) . ' -->';
            }
        }
        Three_Blocks::get_instance();
    }