<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_Sitelogo extends Widget_Base {
    use Sifency_Animation_Controls;

    public function get_name() {
        return 'sitelogo';
    }

    public function get_title() {
        return __( 'Sifency Site Logo', 'sifency-addons' );
    }

    public function get_icon() {
        return 'eicon-site-logo';
    }

    public function get_categories() {
        return [ 'sifency-addons-widgets' ];
    }

    public function get_keywords() {
        return [ 'logo', 'site', 'brand', 'animation' ];
    }
    public function get_style_depends() {
	    return [ 'sifency-frontend-style' ];
    }

    public function get_script_depends() {
        return [ 'sifency-frontend-script' ];
    }

    protected function register_controls() {

        // --- Content Section ---
        $this->start_controls_section(
            'content_section',
            [ 'label' => esc_html__( 'Logo', 'sifency-addons' ) ]
        );
        $this->add_responsive_control(
            'align',
            [
                'label' => esc_html__( 'Alignment', 'sifency-addons' ),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'left' => [ 'title' => esc_html__( 'Left', 'sifency-addons' ), 'icon' => 'eicon-h-align-left' ],
                    'center' => [ 'title' => esc_html__( 'Center', 'sifency-addons' ), 'icon' => 'eicon-h-align-center' ],
                    'right' => [ 'title' => esc_html__( 'Right', 'sifency-addons' ), 'icon' => 'eicon-h-align-right' ],
                ],
                'selectors' => [ '{{WRAPPER}} .sifency-sitelogo-wrapper' => 'text-align: {{VALUE}};' ],
            ]
        );
        $this->add_control(
            'link_to',
            [
                'label' => esc_html__( 'Link', 'sifency-addons' ),
                'type' => Controls_Manager::SELECT,
                'default' => 'home',
                'options' => [ 'home' => esc_html__( 'Home Page', 'sifency-addons' ), 'custom' => esc_html__( 'Custom URL', 'sifency-addons' ), 'none' => esc_html__( 'None', 'sifency-addons' ) ],
            ]
        );
        $this->add_control(
            'custom_link',
            [
                'label' => esc_html__( 'Custom Link', 'sifency-addons' ),
                'type' => Controls_Manager::URL,
                'dynamic' => [ 'active' => true ],
                'condition' => [ 'link_to' => 'custom' ],
                'show_label' => false,
            ]
        );
        $this->end_controls_section();

        // --- Style Section ---
        $this->start_controls_section(
            'style_section',
            [ 'label' => esc_html__( 'Style', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ]
        );
        $this->add_responsive_control(
            'logo_width',
            [
                'label' => esc_html__( 'Width', 'sifency-addons' ),
                'type' => Controls_Manager::SLIDER,
                'size_units' => [ 'px', '%', 'vw' ],
                'range' => [ 'px' => [ 'min' => 20, 'max' => 500 ], '%' => [ 'min' => 5, 'max' => 100 ], 'vw' => [ 'min' => 1, 'max' => 50 ] ],
                'selectors' => [ '{{WRAPPER}} .sifency-sitelogo img' => 'width: {{SIZE}}{{UNIT}};' ],
            ]
        );
        $this->add_responsive_control(
            'logo_max_width',
            [
                'label' => esc_html__( 'Max Width', 'sifency-addons' ),
                'type' => Controls_Manager::SLIDER,
                'size_units' => [ 'px', '%', 'vw' ],
                'range' => [ 'px' => [ 'min' => 20, 'max' => 1000 ], '%' => [ 'min' => 5, 'max' => 100 ], 'vw' => [ 'min' => 1, 'max' => 100 ] ],
                'selectors' => [ '{{WRAPPER}} .sifency-sitelogo img' => 'max-width: {{SIZE}}{{UNIT}};' ],
            ]
        );
        $this->add_control(
            'separator_hover',
            [ 'type' => Controls_Manager::DIVIDER, 'style' => 'thick' ]
        );
        $this->start_controls_tabs( 'logo_effects' );
		$this->start_controls_tab( 'normal', [ 'label' => esc_html__( 'Normal', 'sifency-addons' ) ] );
        $this->add_control( 'opacity', [ 'label' => esc_html__( 'Opacity', 'sifency-addons' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.1, 'step' => 0.01 ] ], 'selectors' => [ '{{WRAPPER}} .sifency-sitelogo img' => 'opacity: {{SIZE}};' ] ]);
        $this->end_controls_tab();
		$this->start_controls_tab( 'hover', [ 'label' => esc_html__( 'Hover', 'sifency-addons' ) ] );
        $this->add_control( 'opacity_hover', [ 'label' => esc_html__( 'Opacity', 'sifency-addons' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.1, 'step' => 0.01 ] ], 'selectors' => [ '{{WRAPPER}} .sifency-sitelogo a:hover img' => 'opacity: {{SIZE}};' ] ]);
        $this->add_control( 'hover_transition', [ 'label' => esc_html__( 'Transition Duration', 'sifency-addons' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 3, 'step' => 0.1 ] ], 'selectors' => [ '{{WRAPPER}} .sifency-sitelogo img' => 'transition-duration: {{SIZE}}s' ] ]);
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [ 'name' => 'logo_border', 'selector' => '{{WRAPPER}} .sifency-sitelogo img', 'separator' => 'before' ]
        );
        $this->add_control(
            'logo_border_radius',
            [ 'label' => esc_html__( 'Border Radius', 'sifency-addons' ), 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .sifency-sitelogo img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ]
        );
        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [ 'name' => 'logo_box_shadow', 'selector' => '{{WRAPPER}} .sifency-sitelogo img' ]
        );
        $this->end_controls_section();

        $this->add_sifency_animation_controls();
        $this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        $logo_html = get_custom_logo();
        if ( empty( $logo_html ) ) {
            if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                ?>
                <div class="sifency-sitelogo-placeholder">
                    <?php esc_html_e( 'No Site Logo is set. Please set it in Appearance > Customize > Site Identity.', 'sifency-addons' ); ?>
                </div>
                <?php
            }
            return;
        }

        $this->add_render_attribute( 'link', 'class', 'sifency-sitelogo-link' );
        if ( 'none' !== $settings['link_to'] ) {
            if ( 'custom' === $settings['link_to'] && ! empty($settings['custom_link']['url']) ) {
                $this->add_link_attributes('link', $settings['custom_link']);
            } else {
                $this->add_render_attribute('link', 'href', esc_url(home_url('/')));
            }
            $logo_html = preg_replace( '/<a\s(.+?)>/i', '<a ' . $this->get_render_attribute_string('link') . '>', $logo_html );
        } else {
            $logo_html = preg_replace( ['/<a\s(.+?)>/i', '/<\/a>/i'], '', $logo_html );
        }
        $inner_wrapper_classes = ['sifency-sitelogo'];
        $inner_wrapper_attrs = $animation_manager->get_wrapper_attributes($inner_wrapper_classes);
        ?>
        <div class="sifency-sitelogo-wrapper">
            <div <?php echo wp_kses_post ($inner_wrapper_attrs); ?><?php  ?>>
                <?php echo wp_kses_post( $logo_html ); ?>
            </div>
        </div>
        <?php
    }
}