<?php
/**
 * Widget Elementor: Epigrafe
 *
 * Mostra l'epigrafe/manifesto funebre
 *
 * @package AnnunciFunebri
 * @since 4.8.0
 */

if (!defined('ABSPATH')) exit;

class ANNFU_Widget_Epigrafe extends ANNFU_Widget_Base {

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

    public function get_title() {
        return __('Epigrafe/Manifesto', 'annuncifunebri-onoranza');
    }

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

    protected function ha_dati_widget() {
        $annuncio = $this->ottieni_dati_annuncio();
        return !empty($annuncio['fileEpigrafe']);
    }

    protected function register_controls() {
        // === TAB CONTENT ===
        $this->start_controls_section(
            'sezione_contenuto',
            [
                'label' => __('Contenuto', 'annuncifunebri-onoranza'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'titolo',
            [
                'label' => __('Titolo', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => 'Manifesto funebre',
                'placeholder' => 'Manifesto funebre',
            ]
        );

        $this->add_control(
            'mostra_lightbox',
            [
                'label' => __('Mostra in Lightbox', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Sì', 'annuncifunebri-onoranza'),
                'label_off' => __('No', 'annuncifunebri-onoranza'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'mostra_download',
            [
                'label' => __('Mostra Link Download', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Sì', 'annuncifunebri-onoranza'),
                'label_off' => __('No', 'annuncifunebri-onoranza'),
                'return_value' => 'yes',
                'default' => 'no',
            ]
        );

        $this->add_control(
            'testo_download',
            [
                'label' => __('Testo Download', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => 'Scarica manifesto',
                'condition' => [
                    'mostra_download' => 'yes',
                ],
            ]
        );

        $this->end_controls_section();

        // === TAB STYLE ===
        $this->start_controls_section(
            'sezione_stile',
            [
                'label' => __('Stile', 'annuncifunebri-onoranza'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'tipografia_titolo',
                'label' => __('Tipografia Titolo', 'annuncifunebri-onoranza'),
                'selector' => '{{WRAPPER}} .annfu-epigrafe-titolo',
            ]
        );

        $this->add_control(
            'colore_titolo',
            [
                'label' => __('Colore Titolo', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .annfu-epigrafe-titolo' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'larghezza_immagine',
            [
                'label' => __('Larghezza Immagine', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', '%'],
                'range' => [
                    'px' => [
                        'min' => 100,
                        'max' => 1000,
                    ],
                    '%' => [
                        'min' => 10,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => '%',
                    'size' => 100,
                ],
                'selectors' => [
                    '{{WRAPPER}} .annfu-epigrafe-immagine img' => 'width: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' => 'bordo_immagine',
                'label' => __('Bordo Immagine', 'annuncifunebri-onoranza'),
                'selector' => '{{WRAPPER}} .annfu-epigrafe-immagine img',
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'ombra_immagine',
                'label' => __('Ombra Immagine', 'annuncifunebri-onoranza'),
                'selector' => '{{WRAPPER}} .annfu-epigrafe-immagine img',
            ]
        );

        $this->add_responsive_control(
            'allineamento',
            [
                'label' => __('Allineamento', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::CHOOSE,
                'options' => [
                    'left' => [
                        'title' => __('Sinistra', 'annuncifunebri-onoranza'),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => __('Centro', 'annuncifunebri-onoranza'),
                        'icon' => 'eicon-text-align-center',
                    ],
                    'right' => [
                        'title' => __('Destra', 'annuncifunebri-onoranza'),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'default' => 'center',
                'selectors' => [
                    '{{WRAPPER}} .annfu-epigrafe-wrapper' => 'text-align: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'margine',
            [
                'label' => __('Margine', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .annfu-epigrafe-wrapper' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

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

        if (\Elementor\Plugin::$instance->editor->is_edit_mode() && !$this->ha_dati_widget()) {
            $this->render_placeholder_editor('eicon-image', __('Epigrafe', 'annuncifunebri-onoranza'), __('Manifesto funebre', 'annuncifunebri-onoranza'));
            return;
        }

        if (!$this->ha_dati_widget()) {
            return;
        }

        $titolo = $settings['titolo'];
        $mostra_lightbox = $settings['mostra_lightbox'] === 'yes';
        $mostra_download = $settings['mostra_download'] === 'yes';
        $testo_download = $settings['testo_download'];

        $url_epigrafe = $annuncio['fileEpigrafe'];

        ?>
        <div class="annfu-epigrafe-wrapper">
            <?php if (!empty($titolo)): ?>
                <h3 class="annfu-epigrafe-titolo"><?php echo esc_html($titolo); ?></h3>
            <?php endif; ?>

            <div class="annfu-epigrafe-immagine">
                <?php if ($mostra_lightbox): ?>
                    <a href="<?php echo esc_url($url_epigrafe); ?>" data-lightbox="epigrafe" title="<?php echo esc_attr($titolo); ?>">
                        <img src="<?php echo esc_url($url_epigrafe); ?>" alt="<?php echo esc_attr($titolo); ?>">
                    </a>
                <?php else: ?>
                    <img src="<?php echo esc_url($url_epigrafe); ?>" alt="<?php echo esc_attr($titolo); ?>">
                <?php endif; ?>
            </div>

            <?php if ($mostra_download): ?>
                <div class="annfu-epigrafe-download">
                    <a href="<?php echo esc_url($url_epigrafe); ?>" download class="annfu-epigrafe-download-link">
                        <?php echo esc_html($testo_download); ?>
                    </a>
                </div>
            <?php endif; ?>
        </div>
        <?php
    }
}
