<?php
    /**
     * Custom button 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 home_url;
    use function esc_attr;
    use function esc_url;
    use function esc_html;

    if( ! class_exists( __NAMESPACE__ . '\\Custom_Button' ) ) {
        /**
         * Custom_Button Class
         */
        class Custom_Button extends Section {
            /**
             * Section id
             * 
             * @since 1.0.0 
             */
            public static $id = 'bbe-custom-button';

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'custom_button_section' );
                $this->add_control( 'custom_button_section_tab' );
                $this->add_control( 'custom_button_label' );
                $this->add_control( 'custom_button_url' );
                $this->add_control( 'custom_button_target' );
                $this->add_control( 'custom_button_rel_attribute' );
                $this->tab = 'design';
                $this->add_control( 'custom_button_typography' );
                $this->add_control( 'custom_button_color' );
                $this->add_control( 'custom_button_background' );
                $this->add_control( 'custom_button_border' );
                $this->add_control( 'custom_button_border_color' );
                $this->add_control( 'custom_button_border_radius' );
                $this->add_control( 'custom_button_padding' );
            }

            /**
             * Settings
             * 
             * @param string $id    The setting id
             * @since 1.0.0
             */
            public function get_settings( $id = '' ): array {
                $settings = [
                    'custom_button_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'custom_button_label' =>  [
                        'sanitize_callback' =>  'sanitize_text_field',
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'custom_button_url' =>  [
                        'sanitize_callback' =>  'esc_url_raw',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'custom_button_target' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'custom_button_rel_attribute' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_multiselect' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'custom_button_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'custom_button_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'custom_button_background' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'custom_button_border' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_border' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'custom_button_border_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'custom_button_border_radius' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_dimension' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'custom_button_padding' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_dimension' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @param string    $id     The control id
             * @since 1.0.0
             */
            public function get_controls( $id = '' ): array {
                $controls = [
                    'custom_button_section' =>  [
                        'title' =>  esc_html__( 'Custom Button', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'custom_button_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'custom_button_label' =>  [
                        'label' =>  esc_html__( 'Label', 'blog-bee' ),
                        'type'  =>  'bbe-text'
                    ],
                    'custom_button_url' =>  [
                        'label' =>  esc_html__( 'URL', 'blog-bee' ),
                        'type'  =>  'url'
                    ],
                    'custom_button_target' =>  [
                        'label' =>  esc_html__( 'Open in', 'blog-bee' ),
                        'type'  =>  'radio-tab',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'New Tab', 'blog-bee' ),
                                'value' =>  '_blank'
                            ],
                            [
                                'label' =>  esc_html__( 'Same Tab', 'blog-bee' ),
                                'value' =>  '_self'
                            ],
                        ]
                    ],
                    'custom_button_rel_attribute' =>  [
                        'label' =>  esc_html__( 'Rel attribute', 'blog-bee' ),
                        'description' =>  esc_html__( 'Specifies the relationship between the current document and the linked document.', 'blog-bee' ),
                        'type'  =>  'multiselect',
                        'fields'  => [
                            [
                                'label'   => esc_html__( 'No Follow', 'blog-bee' ),
                                'value' =>  'nofollow'
                            ],
                            [
                                'label'        => esc_html__( 'UGC (User Generated Content)', 'blog-bee' ),
                                'value' =>  'ugc'
                            ],
                            [
                                'label'  => esc_html__( 'Sponsored', 'blog-bee' ),
                                'value' =>  'sponsored'
                            ],
                            [
                                'label'   => esc_html__( 'No Opener', 'blog-bee' ),
                                'value' =>  'noopener'
                            ],
                            [
                                'label' => esc_html__( 'No Referrer', 'blog-bee' ),
                                'value' =>  'noreferrer'
                            ],
                        ],
                    ],
                    'custom_button_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'custom_button_color' =>  [
                        'label' =>  esc_html__( 'Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'custom_button_background' =>  [
                        'label' =>  esc_html__( 'Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                        'color_types'   =>  [ 'solid', 'gradient' ]
                    ],
                    'custom_button_border' =>  [
                        'label' =>  esc_html__( 'Border', 'blog-bee' ),
                        'type'  =>  'border',
                        'exclude'   =>  [ 'color' ]
                    ],
                    'custom_button_border_color' =>  [
                        'label' =>  esc_html__( 'Border Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'custom_button_border_radius' =>  [
                        'label' =>  esc_html__( 'Border Radius', 'blog-bee' ),
                        'type'  =>  'dimension',
                    ],
                    'custom_button_padding' =>  [
                        'label' =>  esc_html__( 'Padding', 'blog-bee' ),
                        'type'  =>  'dimension'
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Set defaults
             * 
             * @since 1.0.0
             * 
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'custom_button_section_tab' =>  'general',
                    'custom_button_label' =>  'Button',
                    'custom_button_url' =>  home_url(),
                    'custom_button_target' =>  '_blank',
                    'custom_button_rel_attribute' =>  'noopener noreferrer',
                    'custom_button_typography'   =>  $this->get_typography(),
                    'custom_button_color'  =>  [
                        'initial'   =>  $this->get_color( [ 'value' =>  '#fff' ] ),
                        'hover'   =>  $this->get_color( [ 'value' =>  '#fff' ] ),
                    ],
                    'custom_button_background' =>  [
                        'initial'   =>  $this->get_color( [ 'value' =>  '#3B5CFF' ] ),
                        'hover'   =>  $this->get_color( [ 'value' =>  '#5d76f1' ] ),
                    ],
                    'custom_button_border' =>  $this->get_border( [
                        'style' =>  'none',
                        'width' =>  [
                            'top'   =>  1,
                            'right'   =>  1,
                            'bottom'   =>  1,
                            'left'   =>  1,
                            'link'   =>  true
                        ]
                    ] ),
                    'custom_button_border_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' =>  '#000' ] ),
                        'hover'   =>  $this->get_color( [ 'value' =>  '#000' ] ),
                    ],
                    'custom_button_border_radius' =>  $this->get_dimension( [
                        'top'       =>  6,
                        'right'     =>  6,
                        'bottom'    =>  6,
                        'left'      =>  6,
                    ] ),
                    'custom_button_padding'    =>  $this->get_dimension( [ 'top' => 8, 'right' => 18, 'bottom' => 8, 'left' => 18, 'link' => false ] ),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! $this->is_active() ) return [];
                $dynamic_css_args = [
                    'custom_button_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'custom_button_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'custom_button_typography' ),
                        'selector'  =>  '.bbe-header .bbe-custom-button .custom-button-link, .bbe-header .bbe-custom-button .label',
                    ],
                    'custom_button_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'custom_button_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'custom_button_color' ),
                        'selector'  =>  '.bbe-header .bbe-custom-button .custom-button-link',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'custom_button_background' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'custom_button_background' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'custom_button_background' ),
                        'selector'  =>  '.bbe-header .bbe-custom-button .custom-button-link',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                    'custom_button_border' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'custom_button_border' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'custom_button_border' ),
                        'selector'  =>  '.bbe-header .bbe-custom-button .custom-button-link',
                    ],
                    'custom_button_border_color' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'custom_button_border_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'custom_button_border_color' ),
                        'selector'  =>  '.bbe-header .bbe-custom-button .custom-button-link',
                        'property'  =>  '--bbe-site-border-color'
                    ],
                    'custom_button_border_radius' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'custom_button_border_radius' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'custom_button_border_radius' ),
                        'selector'  =>  '.bbe-header .bbe-custom-button .custom-button-link',
                        'property'  =>  'border-radius'
                    ],
                    'custom_button_padding' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'custom_button_padding' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'custom_button_padding' ),
                        'selector'  =>  '.bbe-header .bbe-custom-button .custom-button-link',
                        'property'  =>  'padding'
                    ],
                ];
                return $dynamic_css_args;
            }

            /**
             * Add partial
             * 
             * @override
             * @since 1.0.0
             */
            protected function register_partials() {
                $this->manager->selective_refresh->add_partial( 'custom_button_partial', [
                    'selector'  =>  '.bbe-custom-button .custom-button-link',
                    'render_callback'   =>  [ $this, 'render_partial' ],
                    'settings'  =>  [ 'custom_button_label' ]
                ] );
            }

            /**
             * Render html
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $block_class[] = 'header-widget';

                $label = Theme_Defaults::get_theme_option( 'custom_button_label' );
                $href = Theme_Defaults::get_theme_option( 'custom_button_url' );
                $target = Theme_Defaults::get_theme_option( 'custom_button_target' );
                $rel = Theme_Defaults::get_theme_option( 'custom_button_rel_attribute' );

                if( ! $label ) return;

                $link_class[] = 'custom-button-link';

                echo '<div class="'. esc_attr( implode( ' ', $block_class ) ) .'">';

                    echo '<a class="'. esc_attr( implode( ' ', $link_class ) ) .'" href="'. esc_url( $href ) .'" target="'. esc_attr( $target ) .'" rel="'. esc_attr( $rel ) .'">';

                        $this->render_partial();

                    echo '</a>';
                    
                echo '</div><!-- .bbe-custom-button -->';
            }

            /**
             * Partial Html
             * 
             * @since 1.0.0
             */
            public function render_partial() {
                $label = Theme_Defaults::get_theme_option( 'custom_button_label' );

                if( $label ) echo '<span class="label">'. esc_html( $label ) .'</span>';
            }
        }
        Custom_Button::get_instance();
    }