<?php
    /**
     * Post Button 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__;
    use function esc_url;
    use function esc_attr;
    use function esc_html;
    use function get_permalink;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'post_button_section' );
                $this->add_control( 'post_button_section_tab' );
                $this->add_control( 'post_button_text' );
                $this->add_control( 'post_button_style' );
                $this->tab = 'design';
                $this->add_control( 'post_button_typography' );
                $this->add_control( 'post_button_color' );
                $this->add_control( 'post_button_background' );
                $this->add_control( 'post_button_border_color' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'post_button_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_button_text' =>  [
                        'sanitize_callback' =>  'sanitize_text_field',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_button_style' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_multiselect' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_button_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_button_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_button_background' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'post_button_border_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $controls = [
                    'post_button_section' =>  [
                        'title' =>  esc_html__( 'Post Button', 'blog-bee' ),
                        'panel' =>  'global_panel',
                        'priority'  =>  40
                    ],
                    'post_button_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'post_button_text' =>  [
                        'label' =>  esc_html__( 'Label', 'blog-bee' ),
                        'type'  =>  'bbe-text'
                    ],
                    'post_button_style' =>  [
                        'label' =>  esc_html__( 'Style', 'blog-bee' ),
                        'type'  =>  'multiselect',
                        'fields'    =>  Helpers::get_customizer_icon_style()
                    ],
                    'post_button_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'post_button_color' =>  [
                        'label' =>  esc_html__( 'Text Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'post_button_background' =>  [
                        'label' =>  esc_html__( 'Background Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true,
                        'color_types'   =>  [ 'solid', 'gradient' ]
                    ],
                    'post_button_border_color' =>  [
                        'label' =>  esc_html__( 'Border Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'post_button_section_tab'   =>  'general',
                    'post_button_text'   =>  'Read Full Blog',
                    'post_button_icon_picker'   =>  $this->get_icon_picker( [ 'value' => 'fa-solid fa-arrow-right' ] ),
                    'post_button_style'   =>  '',
                    'post_button_typography'   =>  $this->get_typography( [
                        'font_weight'   =>  '500',
                        'font_size'   =>  $this->get_responsive( '11px', '11px', '11px' ),
                        'letter_spacing'    =>  $this->get_responsive( '0.13em', '0.13em', '0.13em' ),
                        'text_transform'   =>  'uppercase'
                    ] ),
                    'post_button_color'   =>   [
                        'initial'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                        'hover'   =>  $this->get_color(),
                    ],
                    'post_button_background'   =>  [
                        'initial'   =>  $this->get_color( [ 'value' =>  '#efefef' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '' ] ),
                    ],
                    'post_button_border_color'   =>    [
                        'initial'   =>  $this->get_color( [ 'value' => '#000' ] ),
                        'hover'   =>  $this->get_color( [ 'value' => '#000' ] ),
                    ],
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                $dynamic_args = [
                    'post_button_typography'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'post_button_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'post_button_typography' ),
                        'selector'  =>  '.bbe-article .entry-read-more a',
                    ],
                    'post_button_color'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'post_button_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'post_button_color' ),
                        'selector'  =>  '.bbe-post-button.entry-read-more.filled a, .bbe-post-button.entry-read-more.outline.filled a, .bbe-post-button.entry-read-more.outline a, .bbe-post-button.entry-read-more a',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'post_button_background'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'post_button_background' ),
                        'default' =>  Theme_Defaults::get_defaults( 'post_button_background' ),
                        'selector'  =>  '.bbe-post-button.entry-read-more.filled a, .bbe-post-button.entry-read-more.outline.filled a ',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                    'post_button_border_color' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'post_button_border_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'post_button_border_color' ),
                        'selector'  =>  '.bbe-post-button.entry-read-more.outline a',
                        'property'  =>  '--bbe-site-border-color'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $block_class[] = 'entry-read-more';
                $block_class[] = Theme_Defaults::get_theme_option( 'post_button_style' );

                $label = Theme_Defaults::get_theme_option( 'post_button_text' );
                $icon = Theme_Defaults::get_theme_option( 'post_button_icon_picker' );
                $icon[ 'tag' ] = 'span';
                $icon[ 'class' ] = 'post-button-icon';
                ?>
                    <div class="<?php echo esc_attr( implode( ' ', $block_class ) ) ?>">

                        <a href="<?php echo esc_url( get_permalink() ); ?>" rel="bookmark">

                            <?php

                                echo esc_html( $label );

                                echo Helpers::get_icon_html( $icon );

                            ?>

                        </a>

                    </div>
                    
                <?php
            }
        }
        Post_Button::get_instance();
    }