<?php
    /**
     * Date time 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__ . '\\Date_Time' ) ) {
        /**
         * Date_Time Class
         */
        class Date_Time extends Section {
            /**
             * Section id
             * 
             * @since 1.0.0 
             */
            public static $id = 'bbe-date-time';

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'date_time_section' );
                $this->add_control( 'date_time_section_tab' );
                $this->add_control( 'date_time_enable_time' );
                $this->tab = 'design';
                $this->add_control( 'date_time_typography' );
                $this->add_control( 'date_time_date_color' );
            }

            /**
             * Settings
             * 
             * @param string $id    The setting id
             * @since 1.0.0
             */
            public function get_settings( $id = '' ): array {
                $settings = [
                    'date_time_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'date_time_enable_time' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'date_time_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'date_time_date_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        '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 = [
                    'date_time_section' =>  [
                        'title' =>  esc_html__( 'Date Time', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'date_time_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'date_time_enable_time' =>  [
                        'label' =>  esc_html__( 'Enable Time', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'date_time_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'date_time_date_color' =>  [
                        'label' =>  esc_html__( 'Color', 'blog-bee' ),
                        'type'  =>  'bbe-color'
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Set defaults
             * 
             * @since 1.0.0
             * 
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'date_time_section_tab' =>  'general',
                    'date_time_date_format' =>  'l, jS F Y',
                    'date_time_enable_time' =>  false,
                    'date_time_typography'  =>  $this->get_typography( [
                        'font_weight' => '500',
                        'font_size'   => $this->get_responsive( '14px', '14px', '14px' ),
                        'letter_spacing'    => $this->get_responsive( '-0.2px', '-0.2px', '-0.2px' )
                    ] ),
                    'date_time_date_color'  =>  $this->get_color( [ 'value' => '#000' ] ),
                    'date_time_date_background' =>  $this->get_color( [ 'value' => '' ] ),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! $this->is_active() ) return [];
                $dynamic_css_args = [
                    'date_time_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'date_time_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'date_time_typography' ),
                        'selector'  =>  '.bbe-header .bbe-date-time, .bbe-header .bbe-date-time > span',
                        'variable'  =>  false
                    ],
                    'date_time_date_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'date_time_date_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'date_time_date_color' ),
                        'selector'  =>  '.bbe-light .bbe-header .bbe-date-time, .bbe-light .bbe-header .bbe-date-time > span',
                        'property'  =>  '--bbe-site-color'
                    ],
                ];
                return $dynamic_css_args;
            }

            /**
             * Render html
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = 'bbe-date-time';
                $block_class[] = 'header-widget';
                
                $enable_time = Theme_Defaults::get_theme_option( 'date_time_enable_time' );

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

                    echo '<span class="date">'. esc_html( date( Theme_Defaults::get_theme_option( 'date_time_date_format' ) ) ) .'</span>';

                    if( $enable_time ) echo '<span class="time">'. esc_html( date( 'h:i:s A' ) ) .'</span>';
                    
                echo '</div><!-- .bbe-date-time -->';
            }
        }
        Date_Time::get_instance();
    }