<?php
    /**
     * Footer logo section
     * 
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Customizer\Section;

    use BBE\Customizer\Section\Section as Section;
    use BBE\Customizer\Section\Site_Logo as Site_Logo;
    use BBE\Customizer\Theme_Defaults as Theme_Defaults;
    use BBE\Helpers as Helpers;

    use function esc_html__;
    use function esc_url;
    use function home_url;
    use function esc_attr;

    if( ! class_exists( __NAMESPACE__ . '\\Footer_Logo' ) ) {
        /**
         * Footer_Logo Class
         */
        class Footer_Logo extends Section {
            /**
             * Section id
             * 
             * @since 1.0.0 
             */
            public static $id = 'bbe-footer-logo';

            /**
             * Is section active
             * 
             * @since 1.0.0
             */
            private function is_active(): bool {
                $footer_builder = Helpers::is_widget_in_builder( 'footer_builder', 'footer-logo' );
                return $footer_builder;
            }

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'footer_logo_section' );
                $this->add_control( 'footer_logo_section_tab' );
                $this->add_control( 'footer_logo_type' );
                $this->add_control( 'footer_logo_custom' );
                $this->add_control( 'footer_logo_width' );
                $this->tab = 'design';
                $this->add_control( 'footer_logo_typography' );
                $this->add_control( 'footer_logo_color' );
            }

            /**
             * Settings
             * 
             * @param string $id    The setting id
             * @since 1.0.0
             */
            public function get_settings( $id = '' ): array {
                $settings = [
                    'footer_logo_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'footer_logo_type' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'footer_logo_custom' =>  [
                        'sanitize_callback' =>  'absint',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'footer_logo_width' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_range' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'footer_logo_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'footer_logo_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 = [
                    'footer_logo_section' =>  [
                        'title' =>  esc_html__( 'Footer Logo', 'blog-bee' ),
                        'active_callback'   =>  function(){
                            return $this->is_active();
                        },
                        'section_to_open' =>  'footer_builder_section'
                    ],
                    'footer_logo_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'footer_logo_type' =>  [
                        'label' =>  esc_html__( 'Type', 'blog-bee' ),
                        'type'  =>  'radio-tab',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Custom', 'blog-bee' ),
                                'value' =>  'custom'
                            ],
                            [
                                'label' =>  esc_html__( 'Default', 'blog-bee' ),
                                'value' =>  'default'
                            ]
                        ]
                    ],                 
                    'footer_logo_custom' =>  [
                        'label' =>  esc_html__( 'Footer Logo', 'blog-bee' ),
                        'type'  =>  'media',
                        'conditions'    =>  [
                            'relation' =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'footer_logo_type',
                                    'operator'  =>  '==',
                                    'value' =>  'custom'
                                ]
                            ]
                        ]
                    ],
                    'footer_logo_width' =>  [
                        'label' =>  esc_html__( 'Width', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'input_attrs'   =>  [
                            'px'    =>  [
                                'min'   =>  0,
                                'max'   =>  300,
                                'step'   =>  1,
                            ],
                            '%'    =>  [
                                'min'   =>  0,
                                'max'   =>  100,
                                'step'   =>  1,
                            ],
                        ],
                        'conditions'    =>  [
                            'relation' =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'footer_logo_type',
                                    'operator'  =>  '==',
                                    'value' =>  'custom'
                                ]
                            ]
                        ]
                    ],
                    'footer_logo_typography' =>  [
                        'label' =>  esc_html__( 'Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],                 
                    'footer_logo_color' =>  [
                        'label' =>  esc_html__( 'Color', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'include_hover' =>  true
                    ],                 
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Set defaults
             * 
             * @since 1.0.0
             * 
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'footer_logo_section_tab' =>  'general',
                    'footer_logo_type' =>  'default',
                    'footer_logo_custom' =>  0,
                    'footer_logo_custom_dark' =>  0,
                    'footer_logo_width' =>  '210px',
                    'footer_logo_typography' =>  $this->get_typography( [
                        'font_weight'  =>  '600',
                        'font_size'  =>  $this->get_responsive( '26px', '26px', '26px' ),
                        'line_height'    =>  $this->get_responsive( 1.4, 1.4, 1.4 ),
                    ] ),
                    'footer_logo_color' =>  [
                        'initial'   =>  $this->get_color( [ 'value' => '#30344b' ] ),
                        'hover' =>  $this->get_color()
                    ]
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                if( ! $this->is_active() ) return [];
                $dynamic_css_args = [
                    'footer_logo_typography' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'footer_logo_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'footer_logo_typography' ),
                        'selector'  =>  '.bbe-footer .bbe-site-logo .site-title, .bbe-footer .bbe-site-logo .site-title-link'
                    ],
                    'footer_logo_color' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'footer_logo_color' ),
                        'default' =>  Theme_Defaults::get_defaults( 'footer_logo_color' ),
                        'selector'  =>  '.bbe-light .bbe-footer .bbe-site-logo .site-title-link',
                        'property'  =>  '--bbe-site-color'
                    ],
                    'footer_logo_width' =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'footer_logo_width' ),
                        'default' =>  Theme_Defaults::get_defaults( 'footer_logo_width' ),
                        'selector'  =>  '.bbe-footer .bbe-footer-logo > a img',
                        'property'  =>  'width'
                    ],
                ];
                return $dynamic_css_args;
            }

            /**
             * Render html
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;
                $type = Theme_Defaults::get_theme_option( 'footer_logo_type' );
                $custom_logo = Theme_Defaults::get_theme_option( 'footer_logo_custom' );
                $custom_dark_logo = Theme_Defaults::get_theme_option( 'footer_logo_custom_dark' );

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

                    if( $type === 'default' ) :

                        Site_Logo::get_instance()->render_html();

                    else:
                        
                        echo '<a href="'. esc_url( home_url() ) .'" class="footer-custom-logo-link footer-light-logo-link">'. wp_get_attachment_image( $custom_logo, 'large', false, [ 'class' => 'custom-logo' ] ) .'</a>';

                        echo '<a href="'. esc_url( home_url() ) .'" class="footer-custom-logo-link footer-dark-logo-link">'. wp_get_attachment_image( $custom_dark_logo, 'large', false, [ 'class' => 'custom-dark-logo' ] ) .'</a>';

                    endif;

                echo '</div><!-- .bbe-footer-logo -->';
            }
        }
        Footer_Logo::get_instance();
    }