<?php
    /**
     * Sticky Site Logo Section
     * 
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Customizer\Section;

    use BBE\Customizer\Section\Section as Section;
    use BBE\Customizer\Section\Sticky_Header_Trait as Sticky_Header_Trait;
    use BBE\Customizer\Section\Site_Logo as Site_Logo;
    use BBE\Customizer\Theme_Defaults as Theme_Defaults;
    use BBE\Helpers as Helpers;
    
    use function home_url;
    use function esc_url;
    use function wp_get_attachment_image;

    if( ! class_exists( __NAMESPACE__ . '\\Sticky_Site_Logo' ) ) {
        /**
         * Site Logo class
         */
        class Sticky_Site_Logo extends Section {

            use Sticky_Header_Trait;

            /**
             * Section Id
             * 
             * @since 1.0.0
             * @var string
             */
            public static $id = 'bbe-sticky-site-logo';

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->add_section( 'sticky_title_tagline' );
                $this->add_control( 'sticky_site_logo_redirect' );
                $this->add_control( 'sticky_site_logo' );
                $this->add_control( 'sticky_site_title_typography' );
                $this->add_control( 'sticky_site_logo_width' );
                $this->add_control( 'sticky_site_title_color' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'sticky_site_logo_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sticky_site_logo' =>  [
                        'sanitize_callback' =>  'absint',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sticky_site_title_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sticky_site_logo_width' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_range' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'sticky_site_title_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 = [
                    'sticky_title_tagline' =>  [
                        'title' =>  esc_html__( 'Site Identity', 'blog-bee' ),
                        'section_to_open' =>  'sticky_header_section',
                        'active_callback'   =>  function(){
                            return $this->is_active( 'site-logo' );
                        }
                    ],
                    'sticky_site_logo_redirect' =>  [
                        'label'    =>  esc_html__( 'Edit Site Identity', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'title_tagline'
                    ],
                    'sticky_site_logo' =>  [
                        'label' =>  esc_html__( 'Site Logo', 'blog-bee' ),
                        'type'  =>  'media',
                        'mime_type' =>  'image'
                    ],
                    'sticky_site_title_typography' =>  [
                        'label' =>  esc_html__( 'Site Title Typography', 'blog-bee' ),
                        'type'  =>  'typography',
                    ],
                    'sticky_site_logo_width' =>  [
                        'label' =>  esc_html__( 'Logo Width', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'responsive'    =>  true,
                        'input_attrs'   =>  [
                            'px'    =>  [
                                'min'   =>  100,
                                'max'   =>  500,
                                'step'  =>  1
                            ],
                            '%'    =>  [
                                'min'   =>  0,
                                'max'   =>  100,
                                'step'  =>  1
                            ],
                            'em'    =>  [
                                'min'   =>  0,
                                'max'   =>  2,
                                'step'  =>  0.1
                            ],
                            'rem'    =>  [
                                'min'   =>  0,
                                'max'   =>  2,
                                'step'  =>  0.1
                            ]
                        ]
                    ],
                    'sticky_site_title_color' =>  [
                        'label' =>  esc_html__( 'Site Title 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( [
                    'sticky_site_logo_redirect' => '',
                    'sticky_site_logo' => 0,
                    'sticky_site_title_typography' =>  $this->get_typography( [
                        'font_size'  =>  $this->get_responsive( '26px', '26px', '26px' ),
                        'line_height'    =>  $this->get_responsive( 1.4, 1.4, 1.4 ),
                    ] ),
                    'sticky_site_logo_width' =>  $this->get_responsive( '200px', '200px', '200px' ),
                    'sticky_site_title_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#141414' ] ),
                        'hover'   =>  $this->get_color(),
                    ],
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! $this->is_active( 'site-logo' ) ) return [];
                $dynamic_args = [
                    'sticky_site_title_typography'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'sticky_site_title_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'sticky_site_title_typography' ),
                        'selector'  =>  '.bbe-sticky-headerr .bbe-site-logo .site-title, .bbe-sticky-header .bbe-site-logo .site-title-link',
                    ],
                    'sticky_site_title_color'  =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'sticky_site_title_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'sticky_site_title_color' ),
                        'selector'  =>  '.bbe-light .bbe-sticky-header .bbe-site-logo .site-title-link',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'sticky_site_logo_width'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'site_logo_width' ),
                        'default' =>  Theme_Defaults::get_defaults( 'site_logo_width' ),
                        'selector'  =>  '.bbe-sticky-header .bbe-sticky-site-logo .sticky-logo',
                        'property'  =>  'width'
                    ],

                ];
                return $dynamic_args;
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $sticky_logo = Theme_Defaults::get_theme_option( 'sticky_site_logo' );
                if( $sticky_logo ) :
                    $block_class[] = self::$id;
                    $block_class[] = 'header-widget';

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

                        echo '<a href="'. esc_url( home_url() ) .'" class="sticky-custom-logo-link">'. wp_get_attachment_image( $sticky_logo, 'large', false, [ 'class' => 'sticky-logo' ] ) .'</a>';
                    
                    echo '</div><!-- .bbe-sticky-site-logo -->';

                else:
                    Site_Logo::get_instance()->render_html();
                endif;
            }
        }
        Sticky_Site_Logo::get_instance();
    }