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

    use BBE\Customizer\Section\Section as Section;
    use BBE\Customizer\Theme_Defaults as Theme_Defaults;
    use BBE\Helpers;
    use WP_Query;
    use BBE\Query_Manager as Query_Manager;

    use function esc_html__;
    use function esc_attr;
    use function get_template_directory_uri;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'three_blocks_center_section' );
                $this->add_control( 'three_blocks_center_section_tab' );
                $this->add_control( 'three_blocks_center_layout' );
                $this->add_control( 'three_blocks_center_section_label' );;
                $this->add_control( 'three_blocks_center_post_query_settings' );
                $this->add_control( 'three_blocks_center_post_source' );
                $this->add_control( 'three_blocks_center_categories' );
                $this->add_control( 'three_blocks_center_posts_to_include' );
                $this->add_control( 'three_blocks_center_number_of_posts' );
                $this->add_control( 'three_blocks_center_orderby' );
                $this->add_control( 'three_blocks_center_hide_no_featured' );
                $this->add_control( 'three_blocks_center_post_element_settings' );
                $this->add_control( 'three_blocks_center_content_toggles' );
                $this->add_control( 'three_blocks_center_content_alignment' );
                $this->add_control( 'three_blocks_center_image_settings' );
                $this->add_control( 'three_blocks_center_image_sizes' );
                $this->tab = 'design';
                $this->add_control( 'three_blocks_center_post_title_typography' );
                $this->add_control( 'three_blocks_center_background' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'three_blocks_center_section_tab' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_layout' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_image' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_section_label' =>  [
                        'sanitize_callback' =>  'sanitize_text_field',
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_post_query_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_post_source' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_categories' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_async_multiselect' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_posts_to_include' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_async_multiselect' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_number_of_posts' =>  [
                        'sanitize_callback' =>  'absint',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_orderby' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_hide_no_featured' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_post_element_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_content_toggles' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle_group' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_content_alignment' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_image_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_image_sizes' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_post_title_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'three_blocks_center_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_center_section' =>  [
                        'title' =>  esc_html__( '3 Blocks Center', 'blog-bee' ),
                        'active_callback'   =>  function() {
                            return Helpers::get_reorder_value( 'section_reorder_fields', 'three-blocks' ) && Helpers::get_reorder_value( 'three_blocks_reorder', 'center' );
                        },
                        'section_to_open' =>  'three_blocks_section'
                    ],
                    'three_blocks_center_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'three_blocks_center_layout' =>  [
                        'label' =>  esc_html__( 'Layouts', 'blog-bee' ),
                        'type'  =>  'radio-image',
                        'fields'   =>  [
                            [
                                'label' =>  esc_html__( 'List', 'blog-bee' ),
                                'value' =>  'list',
                                'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/archive-list.webp'
                            ],
                            [
                                'label' =>  esc_html__( 'Grid', 'blog-bee' ),
                                'value' =>  'grid',
                                'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/archive-grid.webp'
                            ],
                        ]
                    ],
                    'three_blocks_center_section_label' =>  [
                        'label' =>  esc_html__( 'Blocks Label', 'blog-bee' ),
                        'type'  =>  'bbe-text'
                    ],
                    'three_blocks_center_post_query_settings' =>  [
                        'label' =>  esc_html__( 'Post Query Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'three_blocks_center_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' ),
                        ]
                    ],
                    'three_blocks_center_categories' =>  [
                        'label' =>  esc_html__( 'Post Categories', 'blog-bee' ),
                        'type'  =>  'async-multiselect',
                        'endpoint'  =>  'get_taxonomy',
                        'purpose'   =>  'category',
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'three_blocks_center_post_source',
                                    'operator'  =>  '==',
                                    'value' =>  'categories'
                                ]
                            ]
                        ]
                    ],
                    'three_blocks_center_posts_to_include' =>  [
                        'label' =>  esc_html__( 'Post to Include', 'blog-bee' ),
                        'type'  =>  'async-multiselect',
                        'endpoint'  =>  'get_posts',
                        'purpose'   =>  'post',
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'three_blocks_center_post_source',
                                    'operator'  =>  '==',
                                    'value' =>  'manual'
                                ]
                            ]
                        ]
                    ],
                    'three_blocks_center_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
                        ]
                    ],
                    'three_blocks_center_orderby' =>  [
                        'label' =>  esc_html__( 'Order By', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'   =>  Helpers::get_order_by()
                    ],
                    'three_blocks_center_hide_no_featured' =>  [
                        'label' =>  esc_html__( 'Hide Posts With No Featured Image', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'three_blocks_center_post_element_settings' =>  [
                        'label' =>  esc_html__( 'Post Element Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'three_blocks_center_content_toggles' =>  [
                        'type'  =>  'toggle-button-group',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Category', 'blog-bee' ),
                                'id' =>  'category'
                            ],
                            [
                                'label' =>  esc_html__( 'Title', 'blog-bee' ),
                                'id' =>  'title'
                            ],
                            [
                                'label' =>  esc_html__( 'Excerpt', 'blog-bee' ),
                                'id' =>  'excerpt'
                            ],
                            [
                                'label' =>  esc_html__( 'Meta', 'blog-bee' ),
                                'id' =>  'meta'
                            ],
                            [
                                'label' =>  esc_html__( 'Read More', 'blog-bee' ),
                                'id' =>  'read-more'
                            ],
                        ]
                    ],
                    'three_blocks_center_content_alignment' =>  [
                        'label' =>  esc_html__( 'Alignment', 'blog-bee' ),
                        'type'  =>  'radio-tab',
                        'fields'    =>  Helpers::get_customizer_alignments()
                    ],
                    'three_blocks_center_image_settings' =>  [
                        'label' =>  esc_html__( 'Image Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'three_blocks_center_image_sizes' =>  [
                        'label' =>  esc_html__( 'Images Sizes', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'   =>  Helpers::get_customizer_image_sizes()
                    ],
                    'three_blocks_center_post_title_typography' =>  [
                        'label' =>  esc_html__( 'Title Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'three_blocks_center_background' =>  [
                        'label' =>  esc_html__( 'Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'color_types'   =>  [ 'solid', 'gradient', 'image' ],
                        'conditions'    =>  [
                            'relation'  =>  'OR',
                            'rules' =>  [
                                [
                                    'setting'   =>  'card_settings_container',
                                    'operator'  =>  '==',
                                    'value' =>  'boxed'
                                ],
                                [
                                    'setting'   =>  'three_blocks_container',
                                    'operator'  =>  '==',
                                    'value' =>  'boxed'
                                ]
                            ]
                        ]
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'three_blocks_center_section_tab' =>  'general',
                    'three_blocks_center_layout' =>  'list',
                    'three_blocks_center_text_alignment' =>  false,
                    'three_blocks_center_section_label' =>  'Second',
                    'three_blocks_center_enable_overlay' =>  false,
                    'three_blocks_center_post_query_settings' =>  false,
                    'three_blocks_center_post_source' =>  'latest',
                    'three_blocks_center_categories' =>  [],
                    'three_blocks_center_tags' =>  [],
                    'three_blocks_center_authors' =>  [],
                    'three_blocks_center_posts_to_include' =>  [],
                    'three_blocks_center_number_of_posts' =>  4,
                    'three_blocks_center_posts_offset' =>  0,
                    'three_blocks_center_orderby' =>  'date-desc',
                    'three_blocks_center_hide_no_featured' =>  false,
                    'three_blocks_center_post_element_settings' =>  false,
                    'three_blocks_center_content_toggles' =>  [
                        'category' =>  false,
                        'title' =>  true,
                        'excerpt'   =>  false,
                        'meta'  =>  true,
                        'read-more'  =>  false
                    ],
                    'three_blocks_center_content_alignment'    =>  'left',
                    'three_blocks_center_image_settings'   =>  '',
                    'three_blocks_center_image_sizes'  =>  'bbe_post_card',
                    'three_blocks_center_post_title_typography'    =>  $this->get_typography(),
                    'three_blocks_center_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' ) || ! Helpers::get_reorder_value( 'three_blocks_reorder', 'center' ) ) return [];
                $dynamic_args = [
                    'three_blocks_center_section_label'    =>  [
                        'selector'  =>  '.bbe-three-blocks .block-item.center-block .section-head .label',
                        'parent'  =>  '.bbe-three-blocks .block-item.center-block .section-head'
                    ],
                    'three_blocks_center_content_alignment'    =>  [
                        'selector'  =>  '.bbe-three-blocks .block-item.center-block',
                        'prefix'    =>  'align--'
                    ],
                    'three_blocks_center_post_title_typography'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'three_blocks_center_post_title_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'three_blocks_center_post_title_typography' ),
                        'selector'  =>  '.bbe-three-blocks .block-item.center-block .entry-title a'
                    ],
                    'three_blocks_center_background' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'three_blocks_center_background' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'three_blocks_center_background' ),
                        'selector'  =>  '.bbe-light .bbe-three-blocks .row .center-block',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {}
        }
        Three_Blocks_Center::get_instance();
    }