<?php
    /**
     * Header divider 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 esc_attr;

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

            /**
             * Is section active
             * 
             * @since 1.0.0
             */
            private function is_active(): bool {
                $header_builder = Helpers::is_widget_in_builder( 'header_builder', 'header-divider-1' );
                $header_responsive_builder = Helpers::is_widget_in_builder( 'header_responsive_builder', 'header-divider-1' );
                $sticky_builder = Helpers::is_widget_in_builder( 'sticky_header_builder', 'header-divider-1' );
                return ( $header_builder || $header_responsive_builder || $sticky_builder );
            }

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'header_divider_one_section' );
                $this->add_control( 'header_divider_one_section_tab' );
                $this->add_control( 'header_divider_one_notice' );
                $this->add_control( 'header_divider_one_style' );
                $this->add_control( 'header_divider_one_width' );
                $this->add_control( 'header_divider_one_height' );
                $this->tab = 'design';
                $this->add_control( 'header_divider_one_color' );
                $this->add_control( 'header_divider_one_margin' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'header_divider_one_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'header_divider_one_notice' =>  [
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'header_divider_one_style' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'header_divider_one_width' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_range' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'header_divider_one_height' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_range' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'header_divider_one_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'header_divider_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 = [
                    'header_divider_one_section' =>  [
                        'title' =>  esc_html__( 'Header Divider 1', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'header_divider_one_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'header_divider_one_notice' =>  [
                        'label' =>  esc_html__( 'For this widget to work, there has to be atleast 1 other widget in the row. Other divider widgets do not count.', 'blog-bee' ),
                        'type'  =>  'notice',
                    ],
                    'header_divider_one_style' =>  [
                        'label' =>  esc_html__( 'Style', 'blog-bee' ),
                        'type'  =>  'radio-tab',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Solid', 'blog-bee' ),
                                'value' =>  'solid'
                            ],
                            [
                                'label' =>  esc_html__( 'Dashed', 'blog-bee' ),
                                'value' =>  'dashed'
                            ],
                            [
                                'label' =>  esc_html__( 'Dotted', 'blog-bee' ),
                                'value' =>  'dotted'
                            ],
                            [
                                'label' =>  esc_html__( 'Double', 'blog-bee' ),
                                'value' =>  'double'
                            ],
                        ]
                    ],
                    'header_divider_one_width' =>  [
                        'label' =>  esc_html__( 'Width', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'input_attrs'   =>  [
                            'px'    =>  [
                                'min'   =>  0,
                                'max'   =>  100,
                                'step'  =>  1
                            ]
                        ]
                    ],
                    'header_divider_one_height' =>  [
                        'label' =>  esc_html__( 'Height', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'input_attrs'   =>  [
                            '%'    =>  [
                                'min'   =>  0,
                                'max'   =>  100,
                                'step'  =>  1
                            ]
                        ]
                    ],
                    'header_divider_one_color' =>  [
                        'label' =>  esc_html__( 'Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                    ],
                    'header_divider_one_margin' =>  [
                        'label' =>  esc_html__( 'Margin', 'blog-bee' ),
                        'type'  =>  'dimension',
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'header_divider_one_section_tab' =>  'general',
                    'header_divider_one_notice' =>  '',
                    'header_divider_one_style' =>  'solid',
                    'header_divider_one_width' =>  '1px',
                    'header_divider_one_height' =>  '50%',
                    'header_divider_one_color' =>  $this->get_color( [ 'value' => '#30344b' ] ),
                    'header_divider_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 = [
                    'header_divider_one_style' =>  [
                        'selector' =>  '.bbe-header-divider-1',
                        'prefix' =>  'style--'
                    ],
                    'header_divider_one_width' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'header_divider_one_width' ),
                        'default' =>  Theme_Defaults::get_defaults( 'header_divider_one_width' ),
                        'selector' =>  '.bbe-header .bbe-header-divider-1',
                        'property' =>  'border-right-width'
                    ],
                    'header_divider_one_height' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'header_divider_one_height' ),
                        'default' =>  Theme_Defaults::get_defaults( 'header_divider_one_height' ),
                        'selector' =>  '.bbe-header .bbe-header-divider-1',
                        'property' =>  'height'
                    ],
                    'header_divider_one_color' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'header_divider_one_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'header_divider_one_color' ),
                        'selector' =>  '.bbe-light .bbe-header .bbe-header-divider-1',
                        'property' =>  '--bbe-site-border-color'
                    ],
                    'header_divider_one_margin' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'header_divider_one_margin' ),
                        'default' =>  Theme_Defaults::get_defaults( 'header_divider_one_margin' ),
                        'selector' =>  '.bbe-header .bbe-header-divider-1',
                        'property' =>  'margin'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $block_class[] = 'style--' . Theme_Defaults::get_theme_option( 'header_divider_one_style' );
                $block_class[] = 'bbe-builder-divider';

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