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

            /**
             * Is section active
             * 
             * @since 1.0.0
             */
            private function is_active(): bool {
                $header_builder = Helpers::is_widget_in_builder( 'header_builder', 'time' );
                $header_responsive_builder = Helpers::is_widget_in_builder( 'header_responsive_builder', 'time' );
                $sticky_builder = Helpers::is_widget_in_builder( 'sticky_header_builder', '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( 'time_section' );
                $this->add_control( 'time_typography' );
                $this->add_control( 'time_color' );
            }

            /**
             * Settings
             * 
             * @param string $id    The setting id
             * @since 1.0.0
             */
            public function get_settings( $id = '' ): array {
                $settings = [
                    'time_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'time_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 = [
                    'time_section' =>  [
                        'title' =>  esc_html__( 'Time', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'header_builder_section'
                    ],
                    'time_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'time_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( [
                    'time_section_tab' =>  'general',
                    'time_typography'  =>  $this->get_typography( [
                        'font-weight'   =>  '500',
                        'font-size'     =>  $this->get_responsive( '14px', '14px', '14px' ),
                        'letter-spacing'    =>  $this->get_responsive( '-0.2em', '-0.2em', '-0.2em' )
                    ] ),
                    'time_color'  =>  $this->get_color( [ 'value' => '#30344b' ] ),
                    'time_background' =>  $this->get_color( [ 'value' => '#fff' ] ),
                ] );
            }

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

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

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

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