<?php
namespace Softech_Addons_for_Elementor\Widgets;
if ( ! defined( 'ABSPATH' ) ) exit;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;

class sitsel_site_logo extends Widget_Base {

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

    public function get_title() {
        return __( 'SITS Site Logo', 'softech-addons-for-elementor' );
    }

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

    public function get_categories() {
        return [ 'sits-category' ];
    }

     protected function register_controls() {
        $this->start_controls_section(
            'section_content',
            [
                'label' => __( 'Site Logo', 'softech-addons-for-elementor' ),
            ]
        );

        $this->add_control(
            'logo_type',
            [
                'label' => esc_html__( 'Logo Type', 'softech-addons-for-elementor' ),
                'type' => \Elementor\Controls_Manager::SELECT,
                'options' => [
                    'dynamic' => esc_html__( 'Site Logo', 'softech-addons-for-elementor' ),
                    'custom'  => esc_html__( 'Custom Logo', 'softech-addons-for-elementor' ),
                ],
                'default' => 'dynamic',
            ]
        );

        $this->add_control(
            'custom_logo',
            [
                'label'     => esc_html__( 'Upload Logo', 'softech-addons-for-elementor' ),
                'type'      => \Elementor\Controls_Manager::MEDIA,
                'condition' => [
                    'logo_type' => 'custom',
                ],
            ]
        );

        // ---- Link Controls ----
        $this->add_control(
            'link_to',
            [
                'label'   => esc_html__( 'Link', 'softech-addons-for-elementor' ),
                'type'    => \Elementor\Controls_Manager::SELECT,
                'options' => [
                    'homepage' => esc_html__( 'Homepage', 'softech-addons-for-elementor' ),
                    'custom'   => esc_html__( 'Custom URL', 'softech-addons-for-elementor' ),
                    'none'     => esc_html__( 'None', 'softech-addons-for-elementor' ),
                ],
                'default' => 'homepage',
            ]
        );

        $this->add_control(
            'custom_link',
            [
                'label'       => esc_html__( 'Custom URL', 'softech-addons-for-elementor' ),
                'type'        => \Elementor\Controls_Manager::URL,
                'placeholder' => esc_html__( 'https://your-link.com', 'softech-addons-for-elementor' ),
                'condition'   => [
                    'link_to' => 'custom',
                ],
            ]
        );

        $this->add_control(
            'link_target',
            [
                'label'     => esc_html__( 'Open in New Tab', 'softech-addons-for-elementor' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'label_on'  => esc_html__( 'Yes', 'softech-addons-for-elementor' ),
                'label_off' => esc_html__( 'No', 'softech-addons-for-elementor' ),
                'default'   => '',
                'condition' => [
                    'link_to!' => 'none',
                ],
            ]
        );

        $this->end_controls_section();

        // ---------------- Style Tab ----------------

        $this->start_controls_section('Logo Style', [
            'label' => esc_html__( 'Logo', 'softech-addons-for-elementor' ),
            'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
        ]);

        $this->add_responsive_control(
            'logo_width',
            [
                'label'      => esc_html__( 'Width', 'softech-addons-for-elementor' ),
                'type'       => \Elementor\Controls_Manager::SLIDER,
                'size_units' => [ 'px', '%', 'rem', 'vh' ],
                'range'      => [
                    'px'  => [ 'min' => 10, 'max' => 500 ],
                    '%'   => [ 'min' => 1,  'max' => 100 ],
                    'rem' => [ 'min' => 1,  'max' => 30  ],
                    'vh'  => [ 'min' => 1,  'max' => 100 ],
                ],
                'default' => [
                    'size' => 100,
                    'unit' => '%',
                ],
                'selectors' => [
                    '{{WRAPPER}} .site-logo' => 'width: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name'     => 'logo_border',
                'selector' => '{{WRAPPER}} .site-logo',
            ]
        );

        $this->add_responsive_control('logo_border_radius', [
            'label'      => esc_html__( 'Border Radius', 'softech-addons-for-elementor' ),
            'type'       => \Elementor\Controls_Manager::DIMENSIONS,
            'size_units' => [ 'px', '%', 'em', 'rem' ],
            'default'    => [
                'top'    => 0,
                'right'  => 0,
                'bottom' => 0,
                'left'   => 0,
                'unit'   => 'px',
            ],
            'selectors' => [
                '{{WRAPPER}} .site-logo' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]);

        $this->add_group_control(
            \Elementor\Group_Control_Box_Shadow::get_type(),
            [
                'name'     => 'logo_shadow',
                'selector' => '{{WRAPPER}} .site-logo',
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();

        // Build the <img> tag
        if ( 'custom' === $settings['logo_type'] && ! empty( $settings['custom_logo']['url'] ) ) {
            $img_html = '<img src="' . esc_url( $settings['custom_logo']['url'] ) . '" class="site-logo" alt="Logo" loading="lazy" decoding="async">';
        } else {
            $custom_logo_id = get_theme_mod( 'custom_logo' );
            $logo           = wp_get_attachment_image_src( $custom_logo_id, 'full' );
            if ( has_custom_logo() && $logo ) {
                $img_html = '<img src="' . esc_url( $logo[0] ) . '" class="site-logo" alt="Logo" loading="lazy" decoding="async">';
            } else {
                return; // Nothing to render
            }
        }

        // Determine link href
        $link_to = $settings['link_to'];
        $target  = ! empty( $settings['link_target'] ) ? ' target="_blank" rel="noopener noreferrer"' : '';

        if ( 'homepage' === $link_to ) {
            $href = esc_url( home_url( '/' ) );
            echo wp_kses_post( '<a href="' . esc_url( $href ) . '"' . esc_attr( $target ) . ' aria-label="' . esc_attr( get_bloginfo( 'name' ) ) . '">' . $img_html . '</a>' );

        } elseif ( 'custom' === $link_to && ! empty( $settings['custom_link']['url'] ) ) {
            $href = esc_url( $settings['custom_link']['url'] );
           echo wp_kses_post( '<a href="' . esc_url( $href ) . '"' . esc_attr( $target ) . '>' . $img_html . '</a>' );

        } else {
            // No link
            echo wp_kses_post( $img_html );
        }
    }
}
?>