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

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

    use function esc_html__;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'sidebar_options_section' );
                $this->add_control( 'sidebar_options_section_tab' );
                $this->add_control( 'sidebar_options_sidebar_left_redirect' );
                $this->add_control( 'sidebar_options_sidebar_right_redirect' );
                $this->add_control( 'sidebar_options_archive_settings' );
                $this->add_control( 'sidebar_options_archive_sticky_option' );
                $this->add_control( 'sidebar_options_archive_sidebar' );
                $this->add_control( 'archives_redirect' );
                $this->add_control( 'sidebar_options_frontpage_settings' );
                $this->add_control( 'sidebar_options_frontpage_sticky_option' );
                $this->add_control( 'sidebar_options_frontpage_sidebar' );
                $this->add_control( 'sidebar_options_single_settings' );
                $this->add_control( 'sidebar_options_single_sticky_option' );
                $this->add_control( 'sidebar_options_single_sidebar' );
                $this->add_control( 'single_redirect' );
                $this->add_control( 'sidebar_options_page_settings' );
                $this->add_control( 'sidebar_options_page_sticky_option' );
                $this->add_control( 'sidebar_options_page_sidebar' );
                $this->add_control( 'page_redirect' );
                $this->add_control( '404_redirect' );
                $this->add_control( 'sidebar_options_search_settings' );
                $this->add_control( 'sidebar_options_search_sticky_option' );
                $this->add_control( 'sidebar_options_search_sidebar' );
                $this->add_control( 'search_redirect' );
                $this->tab = 'design';
                $this->add_control( 'sidebar_options_title_typography' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'sidebar_options_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_sidebar_left_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_sidebar_right_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_archive_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_archive_sticky_option' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_archive_sidebar' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_reorder' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_frontpage_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_frontpage_sticky_option' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_frontpage_sidebar' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_reorder' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_single_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_single_sticky_option' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_single_sidebar' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_reorder' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'single_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_page_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_page_sticky_option' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_page_sidebar' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_reorder' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'page_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    '404_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_search_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_search_sticky_option' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_search_sidebar' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_reorder' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'search_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sidebar_options_title_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $sidebar_fields = [
                    [
                        'label' =>  esc_html__( 'Left', 'blog-bee' ),
                        'value' =>  'left',
                        'allow_visibility'  =>  true
                    ],
                    [
                        'label' =>  esc_html__( 'Main', 'blog-bee' ),
                        'value' =>  'main',
                        'allow_visibility'  =>  false
                    ],
                    [
                        'label' =>  esc_html__( 'Right', 'blog-bee' ),
                        'value' =>  'right',
                        'allow_visibility'  =>  true
                    ],
                ];
                $controls = [
                    'sidebar_options_section' =>  [
                        'title' =>  esc_html__( 'Sidebar Options', 'blog-bee' ),
                        'panel' =>  'global_panel',
                        'priority'  =>  50
                    ],
                    'sidebar_options_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'sidebar_options_sidebar_right_redirect' =>  [
                        'label' =>  esc_html__( 'Edit Right Sidebar', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'sidebar-widgets-sidebar-1'
                    ],
                    'sidebar_options_sidebar_left_redirect' =>  [
                        'label' =>  esc_html__( 'Edit Left Sidebar', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'sidebar-widgets-sidebar-left'
                    ],
                    'sidebar_options_archive_settings' =>  [
                        'label' =>  esc_html__( 'Archive Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'sidebar_options_archive_sticky_option' =>  [
                        'label' =>  esc_html__( 'Enable sticky', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'sidebar_options_archive_sidebar' =>  [
                        'label' =>  esc_html__( 'Archive Sidebar', 'blog-bee' ),
                        'type'  =>  'reorder',
                        'fields'    =>  $sidebar_fields,
                        'direction' =>  'horizontal'
                    ],
                    'archives_redirect' =>  [
                        'label'    =>  esc_html__( 'Edit Archive', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'archives_section'
                    ],
                    'sidebar_options_frontpage_settings' =>  [
                        'label' =>  esc_html__( 'Frontpage Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'sidebar_options_frontpage_sticky_option' =>  [
                        'label' =>  esc_html__( 'Enable sticky', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'sidebar_options_frontpage_sidebar' =>  [
                        'label' =>  esc_html__( 'Frontpage Sidebar', 'blog-bee' ),
                        'type'  =>  'reorder',
                        'fields'    =>  $sidebar_fields,
                        'direction' =>  'horizontal'
                    ],
                    'sidebar_options_single_settings' =>  [
                        'label' =>  esc_html__( 'Single Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'sidebar_options_single_sticky_option' =>  [
                        'label' =>  esc_html__( 'Enable sticky', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'sidebar_options_single_sidebar' =>  [
                        'label' =>  esc_html__( 'Single Sidebar', 'blog-bee' ),
                        'type'  =>  'reorder',
                        'fields'    =>  $sidebar_fields,
                        'direction' =>  'horizontal'
                    ],
                    'single_redirect' =>  [
                        'label'    =>  esc_html__( 'Edit Single', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'single_section'
                    ],
                    'sidebar_options_page_settings' =>  [
                        'label' =>  esc_html__( 'Page and 404 Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'sidebar_options_page_sticky_option' =>  [
                        'label' =>  esc_html__( 'Enable sticky', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'sidebar_options_page_sidebar' =>  [
                        'label' =>  esc_html__( 'Page Sidebar', 'blog-bee' ),
                        'type'  =>  'reorder',
                        'fields'    =>  $sidebar_fields,
                        'direction' =>  'horizontal'
                    ],
                    'page_redirect' =>  [
                        'label'    =>  esc_html__( 'Edit Page', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'page_section'
                    ],
                    '404_redirect' =>  [
                        'label'    =>  esc_html__( 'Edit 404', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'error_page_settings_section'
                    ],
                    'sidebar_options_search_settings' =>  [
                        'label' =>  esc_html__( 'Search Page Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'sidebar_options_search_sticky_option' =>  [
                        'label' =>  esc_html__( 'Enable sticky', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'sidebar_options_search_sidebar' =>  [
                        'label' =>  esc_html__( 'Search Page Sidebar', 'blog-bee' ),
                        'type'  =>  'reorder',
                        'fields'    =>  $sidebar_fields,
                        'direction' =>  'horizontal'
                    ],
                    'search_redirect' =>  [
                        'label'    =>  esc_html__( 'Edit Search Page', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'search_page_section'
                    ],
                    'sidebar_options_title_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'description' =>  esc_html__( 'Affects only the Blog Bee widget post titles.', 'blog-bee' ),
                        'type'  =>  'typography',
                    ]
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'sidebar_options_section_tab' =>  'general',
                    'sidebar_options_sidebar_left_redirect' =>  '',
                    'sidebar_options_sidebar_right_redirect' =>  '',
                    'sidebar_options_archive_settings' =>  true,
                    'sidebar_options_archive_sticky_option' =>  true,
                    'sidebar_options_archive_sidebar' =>  [
                        [
                            'id'    =>  'left',
                            'value' =>  false,
                        ],
                        [
                            'id'    =>  'main',
                            'value' =>  true,
                        ],
                        [
                            'id'    =>  'right',
                            'value' =>   false
                        ],
                    ],
                    'archives_redirect' =>  '',
                    'sidebar_options_frontpage_settings' =>  false,
                    'sidebar_options_frontpage_sticky_option' =>  true,
                    'sidebar_options_frontpage_sidebar' =>  [
                        [
                            'id'    =>  'left',
                            'value' =>  false,
                        ],
                        [
                            'id'    =>  'main',
                            'value' =>  true,
                        ],
                        [
                            'id'    =>  'right',
                            'value' =>   true
                        ],
                    ],
                    'sidebar_options_single_settings' =>  false,
                    'sidebar_options_single_sticky_option' =>  true,
                    'sidebar_options_single_sidebar' =>  [
                        [
                            'id'    =>  'left',
                            'value' =>  false,
                        ],
                        [
                            'id'    =>  'main',
                            'value' =>  true,
                        ],
                        [
                            'id'    =>  'right',
                            'value' =>   true
                        ],
                    ],
                    'single_redirect' =>  '',
                    'sidebar_options_page_settings' =>  false,
                    'sidebar_options_page_sticky_option' =>  true,
                    'sidebar_options_page_sidebar' =>  [
                        [
                            'id'    =>  'left',
                            'value' =>  false,
                        ],
                        [
                            'id'    =>  'main',
                            'value' =>  true,
                        ],
                        [
                            'id'    =>  'right',
                            'value' =>   true
                        ],
                    ],
                    'page_redirect' =>  '',
                    '404_redirect' =>  '',
                    'sidebar_options_search_settings' =>  false,
                    'sidebar_options_search_sticky_option' =>  true,
                    'sidebar_options_search_sidebar' =>  [
                        [
                            'id'    =>  'left',
                            'value' =>  false,
                        ],
                        [
                            'id'    =>  'main',
                            'value' =>  true,
                        ],
                        [
                            'id'    =>  'right',
                            'value' =>   true
                        ],
                    ],
                    'search_redirect' =>  '',
                    'sidebar_options_title_typography'    =>  $this->get_typography( [
                        'font_weight'   =>  '500',
                        'font_size' =>  $this->get_responsive( '14px', '14px', '14px' ),
                        'line_height'   =>  $this->get_responsive( 1.46, 1.46, 1.46 )
                    ] )
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                $dynamic_args = [
                    'sidebar_options_archive_sticky_option' =>  [
                        'selector'  =>  'body.home.blog .content-wrapper, body.archive .content-wrapper, body.tax .content-wrapper, body.author .content-wrapper, body.date .content-wrapper',
                        'on'    =>  'is-sticky'
                    ],
                    'sidebar_options_frontpage_sticky_option' =>  [
                        'selector'  =>  'body.home .content-wrapper',
                        'on'    =>  'is-sticky'
                    ],
                    'sidebar_options_single_sticky_option' =>  [
                        'selector'  =>  'body.single .content-wrapper',
                        'on'    =>  'is-sticky'
                    ],
                    'sidebar_options_page_sticky_option' =>  [
                        'selector'  =>  'body.page .content-wrapper',
                        'on'    =>  'is-sticky'
                    ],
                    'sidebar_options_search_sticky_option' =>  [
                        'selector'  =>  'body.search .content-wrapper',
                        'on'    =>  'is-sticky'
                    ],
                    'sidebar_options_title_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'sidebar_options_title_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'sidebar_options_title_typography' ),
                        'selector'  =>  '.widget_blog_bee_post_list_widget .post-list-widget .bbe-article .entry-title, .widget_blog_bee_post_list_widget .post-list-widget .bbe-article .entry-title a'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;

                ?>
                    <div class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>">
                    
                    </div>
                <?php
            }
        }
        Sidebar_Options::get_instance();
    }