<?php
    /**
     * Copyright 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;
    use function wp_kses_post;

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

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'copyright_section' );
                $this->add_control( 'copyright_section_tab' );
                $this->add_control( 'copyright_editor' );
                $this->tab = 'design';
                $this->add_control( 'copyright_typography' );
                $this->add_control( 'copyright_text_color' );
                $this->add_control( 'copyright_link_color' );
                $this->add_control( 'copyright_margin' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'copyright_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'copyright_editor' =>  [
                        'sanitize_callback' =>  'wp_kses_post',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'copyright_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'copyright_text_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'copyright_link_color' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'copyright_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 = [
                    'copyright_section' =>  [
                        'title' =>  esc_html__( 'Copyright', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return Helpers::is_widget_in_builder( 'footer_builder', 'copyright' );
                        },
                        'section_to_open' =>  'footer_builder_section'
                    ],
                    'copyright_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'copyright_editor' =>  [
                        'label' => esc_html__( 'Copyright Text', 'blog-bee' ),
                        'description' => esc_html__( 'Add %year% to retrieve current year.', 'blog-bee' ),
                        'type'  =>  'editor-control',
                    ],
                    'copyright_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'copyright_text_color' =>  [
                        'label' =>  esc_html__( 'Text Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                    ],
                    'copyright_link_color' =>  [
                        'label' =>  esc_html__( 'Link Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],
                    'copyright_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( [
                    'copyright_section_tab' =>  'general',
                    'copyright_editor'    =>  wp_kses_post( '<p>Blog Bee - Blog WordPress Theme %year%.</p>' ),
                    'copyright_typography'    =>  $this->get_typography( [
                        'font-weight' =>  '400',
                        'font-size'   =>  $this->get_responsive( '15px', '15px', '15px' )
                    ] ),
                    'copyright_text_color'    =>  $this->get_color( [ 'value' => '#30344b' ] ),
                    'copyright_link_color'    =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#0073aa' ] ),
                        'hover'   =>  $this->get_color()
                    ],
                    'copyright_margin'    =>  $this->get_dimension(),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! Helpers::is_widget_in_builder( 'footer_builder', 'copyright' ) ) return [];
                $dynamic_args = [
                    'copyright_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'copyright_typography' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'copyright_typography' ),
                        'selector'  =>  '.bbe-footer .bbe-copyright p',
                    ],
                    'copyright_text_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'copyright_text_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'copyright_text_color' ),
                        'selector'  =>  '.bbe-light .bbe-footer .bbe-copyright p',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'copyright_link_color'   =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'copyright_link_color' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'copyright_link_color' ),
                        'selector'  =>  '.bbe-footer .bbe-copyright a',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'copyright_margin' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'copyright_margin' ),
                        'default'   =>  Theme_Defaults::get_defaults( 'copyright_margin' ),
                        'selector'  =>  '.bbe-footer .bbe-copyright p',
                        'property'  =>  'margin'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                ?>
                    <div class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>">
                        <?php
                            echo wp_kses_post( str_replace( '%year%', date('Y'), Theme_Defaults::get_theme_option( 'copyright_editor' ) ) );
                        ?>
                    </div><!-- .bbe-copyright -->
                <?php
            }
        }
        Copyright::get_instance();
    }