<?php
    /**
     * Frontpage Widget Area 1 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 function esc_html__;
    use function is_active_sidebar;
    use function esc_attr;
    use function dynamic_sidebar;

    if( ! class_exists( __NAMESPACE__ . '\\Frontpage_Widget_Area_One' ) ) {
        /**
         * Frontpage_Widget_Area_One class
         */
        class Frontpage_Widget_Area_One extends Section {
            /**
             * Section Id
             * 
             * @since 1.0.0
             * @var string
             */
            public static $id = 'bbe-frontpage-widget-area-one';

            /**
             * Is section active
             * 
             * @since 1.0.0
             */
            private function is_active(): bool {
                $widget = Helpers::get_reorder_value( 'section_reorder_fields', 'widget-one' );
                return $widget;
            }

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'frontpage_widget_area_one_section' );
                $this->add_control( 'frontpage_widget_area_one_redirect' );
                $this->add_control( 'frontpage_widget_area_one_width_layout' );
                $this->add_control( 'frontpage_widget_area_one_heading_typography' );
                $this->add_control( 'frontpage_widget_area_one_content_typography' );
                $this->add_control( 'frontpage_widget_area_one_background' );
                $this->add_control( 'frontpage_widget_area_one_heading_color' );
                $this->add_control( 'frontpage_widget_area_one_content_color' );
                $this->add_control( 'frontpage_widget_area_one_link_color' );
                $this->add_control( 'frontpage_widget_area_one_margin' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'frontpage_widget_area_one_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'frontpage_widget_area_one_width_layout' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_image' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'frontpage_widget_area_one_heading_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'frontpage_widget_area_one_content_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'frontpage_widget_area_one_background' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'frontpage_widget_area_one_heading_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'frontpage_widget_area_one_content_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'frontpage_widget_area_one_link_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'frontpage_widget_area_one_margin' =>  [
                        '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 = [
                    'frontpage_widget_area_one_section' =>  [
                        'title' =>  esc_html__( 'Frontpage Widget Area 1', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'section_reorder_section'
                    ],
                    'frontpage_widget_area_one_redirect' =>  [
                        'label' =>  esc_html__( 'Edit Frontpage Widget Area 1', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'sidebar-widgets-bbe-frontpage-widget-area-one'
                    ],
                    'frontpage_widget_area_one_width_layout' =>  [
                        'label' =>  esc_html__( 'Width', 'blog-bee' ),
                        'type'  =>  'radio-image',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Full Width', 'blog-bee' ),
                                'value' =>  'full-width',
                                'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/full-width.webp'
                            ],
                            [
                                'label' =>  esc_html__( 'Wide', 'blog-bee' ),
                                'value' =>  'wide',
                                'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/wide.webp'
                            ],
                            [
                                'label' =>  esc_html__( 'Boxed', 'blog-bee' ),
                                'value' =>  'boxed',
                                'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/boxed.webp'
                            ],
                        ]
                    ],
                    'frontpage_widget_area_one_heading_typography' =>  [
                        'label' =>  esc_html__( 'Heading Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'frontpage_widget_area_one_content_typography' =>  [
                        'label' =>  esc_html__( 'Content Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'frontpage_widget_area_one_background'    =>  [
                        'label' =>  esc_html__( 'Section Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'color_types'   =>  [ 'solid', 'gradient', 'image' ]
                    ],
                    'frontpage_widget_area_one_heading_color' =>  [
                        'label' =>  esc_html__( 'Heading Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                    ],
                    'frontpage_widget_area_one_content_color' =>  [
                        'label' =>  esc_html__( 'Content Color', 'blog-bee' ),
                        'type'  =>  'bbe-color'
                    ],
                    'frontpage_widget_area_one_link_color' =>  [
                        'label' =>  esc_html__( 'Link Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'frontpage_widget_area_one_margin' =>  [
                        'label' =>  esc_html__( 'Margin', 'blog-bee' ),
                        'type'  =>  'dimension',
                        'input_attrs'   =>  [
                            'min'   =>  -100,
                            'max'   =>  100,
                            'step'  =>  1
                        ]
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'frontpage_widget_area_one_redirect' =>  '',
                    'frontpage_widget_area_one_heading_typography' =>  $this->get_typography( [
                        'font_weight'  =>  '700',
                        'font_size'   =>  $this->get_responsive( '18px', '18px', '18px' ),
                        'line_height' =>  $this->get_responsive( 1.4, 1.4, 1.4 ),
                        'text_transform'    =>  'uppercase'
                    ] ),
                    'frontpage_widget_area_one_width_layout' =>  'boxed',
                    'frontpage_widget_area_one_content_typography' =>  $this->get_typography( [
                        'font_weight' => 'regular',
                        'font_size'   =>  $this->get_responsive( '15px', '15px', '15px' ),
                    ] ),
                    'frontpage_widget_area_one_background'    =>  $this->get_color( [ 'value' => '' ] ),
                    'frontpage_widget_area_one_heading_color'  =>  $this->get_color( [ 'value' => '#30344b' ] ),
                    'frontpage_widget_area_one_content_color'  =>  $this->get_color( [ 'value' => '#30344b' ] ),
                    'frontpage_widget_area_one_link_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#0073aa' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#E78371' ] ),
                    ],
                    'frontpage_widget_area_one_margin' =>  $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 = [
                    'frontpage_widget_area_one_width_layout' =>  [
                        'selector'  =>  '.bbe-frontpage-widget-area-one',
                        'prefix'    =>  'width--'
                    ],
                    'frontpage_widget_area_one_heading_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'frontpage_widget_area_one_heading_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'frontpage_widget_area_one_heading_typography' ),
                        'selector'  =>  '.bbe-frontpage-widget-area-one .widget h1.wp-block-heading, .bbe-frontpage-widget-area-one .widget h1.widget-title .label, .bbe-frontpage-widget-area-one .widget h2.wp-block-heading, .bbe-frontpage-widget-area-one .widget h2.widget-title .label, .bbe-frontpage-widget-area-one .widget h3.wp-block-heading, .bbe-frontpage-widget-area-one .widget h3.widget-title .label, .bbe-frontpage-widget-area-one .widget h4.wp-block-heading, .bbe-frontpage-widget-area-one .widget h4.widget-title .label, .bbe-frontpage-widget-area-one .widget h5.wp-block-heading, .bbe-frontpage-widget-area-one .widget h5.widget-title .label, .bbe-frontpage-widget-area-one .widget h6.wp-block-heading, .bbe-frontpage-widget-area-one .widget h6.widget-title .label',
                    ],
                    'frontpage_widget_area_one_content_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'frontpage_widget_area_one_content_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'frontpage_widget_area_one_content_typography' ),
                        'selector'  =>  'body .bbe-frontpage-widget-area-one .widget',
                    ],
                    'frontpage_widget_area_one_background'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'frontpage_widget_area_one_background' ),
                        'default' =>  Theme_Defaults::get_defaults( 'frontpage_widget_area_one_background' ),
                        'selector'  =>  '.bbe-light .bbe-frontpage-widget-area-one',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                    'frontpage_widget_area_one_heading_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'frontpage_widget_area_one_heading_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'frontpage_widget_area_one_heading_color' ),
                        'selector'  =>  '.bbe-light .bbe-frontpage-widget-area-one .widget h1, .bbe-light .bbe-frontpage-widget-area-one .widget h2, .bbe-light .bbe-frontpage-widget-area-one .widget h3, .bbe-light .bbe-frontpage-widget-area-one .widget h4, .bbe-light .bbe-frontpage-widget-area-one .widget h5, .bbe-light .bbe-frontpage-widget-area-one .widget h6',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'frontpage_widget_area_one_content_color'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'frontpage_widget_area_one_content_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'frontpage_widget_area_one_content_color' ),
                        'selector'  =>  '.bbe-light .bbe-frontpage-widget-area-one .widget',
                        'property'  =>  '--bbe-site-excerpt-color'
                    ],
                    'frontpage_widget_area_one_link_color'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'frontpage_widget_area_one_link_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'frontpage_widget_area_one_link_color' ),
                        'selector'  =>  'body.bbe-light .site .bbe-frontpage-widget-area-one .widget a',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'frontpage_widget_area_one_margin' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'frontpage_widget_area_one_margin' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'frontpage_widget_area_one_margin' ),
                        'selector'  =>  '.bbe-frontpage-widget-area-one',
                        'property'  =>  'margin'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                if( ! is_active_sidebar( self::$id ) ) return;
                
                $block_class[] = self::$id;
                $block_class[] = 'frontpage-widget-area';
                $block_class[] = 'width--' . Theme_Defaults::get_theme_option( 'frontpage_widget_area_one_width_layout' );

                ?>
                    <section class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>" <?php Helpers::the_aos_data_attr(); ?>>
                        <div class="bbe-container">
                            <div class="row">
                                <?php
                                    dynamic_sidebar( self::$id );
                                ?>
                            </div>
                        </div>
                    </section><!-- .bbe-frontpage-widget-area-one -->
                <?php
            }
        }
        Frontpage_Widget_Area_One::get_instance();
    }