<?php
    /**
     * Section Reorder 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;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'section_reorder_section' );
                $this->add_control( 'section_reorder_section_tab' );
                $this->add_control( 'section_reorder_fields' );
                $this->tab = 'design';
                $this->add_control( 'section_reorder_margin' );
                $this->add_control( 'section_reorder_padding' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'section_reorder_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'section_reorder_fields' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_reorder' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'section_reorder_margin' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_dimension' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'section_reorder_padding' =>  [
                        '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 = [
                    'section_reorder_section' =>  [
                        'title' =>  esc_html__( 'Frontpage Sections', 'blog-bee' ),
                        'priority' =>  50,
                    ],
                    'section_reorder_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'section_reorder_fields' =>  [
                        'label' =>  esc_html__( 'Section Reorder', 'blog-bee' ),
                        'type'  =>  'reorder',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Web Stories', 'blog-bee' ),
                                'value' =>  'web-stories',
                                'redirect'  =>  'web_stories_section',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Main Banner', 'blog-bee' ),
                                'value' =>  'main-banner',
                                'redirect'  =>  'main_banner_section',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Carousel', 'blog-bee' ),
                                'value' =>  'carousel',
                                'redirect'  =>  'carousel_section',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Category Collection', 'blog-bee' ),
                                'value' =>  'category-collection',
                                'redirect'  =>  'category_collection_section',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( '3 Blocks', 'blog-bee' ),
                                'value' =>  'three-blocks',
                                'redirect'  =>  'three_blocks_section',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Post Grid', 'blog-bee' ),
                                'value' =>  'post-grid',
                                'redirect'  =>  'post_grid_section',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Post List', 'blog-bee' ),
                                'value' =>  'post-list',
                                'redirect'  =>  'post_list_section',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Latest', 'blog-bee' ),
                                'value' =>  'latest',
                                'redirect'  =>  'archives_section',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Shortcode 1', 'blog-bee' ),
                                'value' =>  'shortcode-one',
                                'redirect'  =>  'frontpage_shortcode_one_section',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Widget Area 1', 'blog-bee' ),
                                'value' =>  'widget-one',
                                'redirect'  =>  'frontpage_widget_area_one_section',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Content Sidebar', 'blog-bee' ),
                                'value' =>  'content-sidebar',
                                'redirect'  =>  'content_sidebar_section',
                                'allow_visibility'  =>  true
                            ],
                        ]
                    ],
                    'section_reorder_margin'    =>  [
                        'label' =>  esc_html__( 'Margin', 'blog-bee' ),
                        'type'  =>  'dimension',
                        'exclude'   =>  [ 'left', 'right' ],
                        'input_attrs'   =>  [
                            'min'   =>  -100,
                            'max'   =>  100,
                            'step'  =>  1
                        ]
                    ],
                    'section_reorder_padding'    =>  [
                        'label' =>  esc_html__( 'Padding', 'blog-bee' ),
                        'type'  =>  'dimension',
                        'exclude'   =>  [ 'left', 'right' ]
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'section_reorder_section_tab' =>  'general',
                    'section_reorder_fields' =>  [
                        [
                            'id'    =>  'web-stories',
                            'value' =>  false
                        ],
                        [
                            'id'    =>  'main-banner',
                            'value' =>  true
                        ],
                        [
                            'id'    =>  'carousel',
                            'value' =>  false
                        ],
                        [
                            'id'    =>  'category-collection',
                            'value' =>  false
                        ],
                        [
                            'id'    =>  'three-blocks',
                            'value' =>  false
                        ],
                        [
                            'id'    =>  'post-grid',
                            'value' =>  false
                        ],
                        [
                            'id'    =>  'post-list',
                            'value' =>  false
                        ],
                        [
                            'id'    =>  'latest',
                            'value' =>  true
                        ],
                        [
                            'id'    =>  'content-sidebar',
                            'value' =>  false
                        ],
                        [
                            'id'    =>  'shortcode-one',
                            'value' =>  false
                        ],
                        [
                            'id'    =>  'widget-one',
                            'value' =>  false
                        ],
                    ],
                    'section_reorder_margin' =>  $this->get_dimension( [
                        'top'    =>  0,
                        'bottom'    =>  0,
                        'link'  =>  false
                    ] ),
                    'section_reorder_padding' =>  $this->get_dimension( [
                        'top'    =>  30,
                        'bottom'    =>  30,
                        'link'  =>  false
                    ] ),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                $dynamic_args = [
                    'section_reorder_margin'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'section_reorder_margin' ),
                        'default' =>  Theme_Defaults::get_defaults( 'section_reorder_margin' ),
                        'selector'  =>  '.home .bbe-sections-wrap',
                        'property'  =>  '--bbe-section-margin'
                    ],
                    'section_reorder_padding'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'section_reorder_padding' ),
                        'default' =>  Theme_Defaults::get_defaults( 'section_reorder_padding' ),
                        'selector'  =>  '.home .bbe-sections-wrap > *, .bbe-you-may-have-missed',
                        'property'  =>  '--bbe-section-padding'
                    ]
                ];
                return $dynamic_args;
            }

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