<?php
/**
 * Widget Elementor: Streaming
 *
 * Mostra il pulsante per lo streaming live del funerale
 *
 * @package AnnunciFunebri
 * @since 4.8.0
 */

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

class ANNFU_Widget_Streaming extends ANNFU_Widget_Base {

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

    public function get_title() {
        return __('Streaming Live', 'annuncifunebri-onoranza');
    }

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

    protected function ha_dati_widget() {
        $annuncio = $this->ottieni_dati_annuncio();
        return !empty($annuncio['streaming']) &&
               (isset($annuncio['streaming']['in_corso']) || isset($annuncio['streaming']['prossimo']));
    }

    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(
            'testo_in_corso',
            [
                'label' => __('Testo Streaming In Corso', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => '🔴 DIRETTA IN CORSO',
                'placeholder' => '🔴 DIRETTA IN CORSO',
            ]
        );

        $this->add_control(
            'testo_prossimo',
            [
                'label' => __('Testo Streaming Prossimo', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => '📺 Guarda la diretta',
                'placeholder' => '📺 Guarda la diretta',
            ]
        );

        $this->add_control(
            'apri_nuova_finestra',
            [
                'label' => __('Apri in Nuova Finestra', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Sì', 'annuncifunebri-onoranza'),
                'label_off' => __('No', 'annuncifunebri-onoranza'),
                'return_value' => 'yes',
                'default' => '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',
                'label' => __('Tipografia', 'annuncifunebri-onoranza'),
                'selector' => '{{WRAPPER}} .annfu-streaming-button',
            ]
        );

        $this->add_control(
            'colore_testo',
            [
                'label' => __('Colore Testo', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'default' => '#ffffff',
                'selectors' => [
                    '{{WRAPPER}} .annfu-streaming-button' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'colore_sfondo',
            [
                'label' => __('Colore Sfondo', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'default' => '#e74c3c',
                'selectors' => [
                    '{{WRAPPER}} .annfu-streaming-button' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'colore_sfondo_hover',
            [
                'label' => __('Colore Sfondo Hover', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'default' => '#c0392b',
                'selectors' => [
                    '{{WRAPPER}} .annfu-streaming-button:hover' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' => 'bordo',
                'label' => __('Bordo', 'annuncifunebri-onoranza'),
                'selector' => '{{WRAPPER}} .annfu-streaming-button',
            ]
        );

        $this->add_control(
            'border_radius',
            [
                'label' => __('Raggio Bordo', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'default' => [
                    'top' => 5,
                    'right' => 5,
                    'bottom' => 5,
                    'left' => 5,
                    'unit' => 'px',
                ],
                'selectors' => [
                    '{{WRAPPER}} .annfu-streaming-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'ombra',
                'label' => __('Ombra', 'annuncifunebri-onoranza'),
                'selector' => '{{WRAPPER}} .annfu-streaming-button',
            ]
        );

        $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-streaming-wrapper' => 'text-align: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'spaziatura',
            [
                'label' => __('Spaziatura Interna', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'default' => [
                    'top' => 15,
                    'right' => 30,
                    'bottom' => 15,
                    'left' => 30,
                    'unit' => 'px',
                ],
                'selectors' => [
                    '{{WRAPPER}} .annfu-streaming-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'margine',
            [
                'label' => __('Margine', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .annfu-streaming-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-video-camera', __('Streaming Live', 'annuncifunebri-onoranza'), __('Pulsante per guardare la diretta', 'annuncifunebri-onoranza'));
            return;
        }

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

        $streaming = $annuncio['streaming'];
        $in_corso = isset($streaming['in_corso']) && $streaming['in_corso'];
        $url = isset($streaming['url']) ? $streaming['url'] : '';

        if (empty($url)) {
            return;
        }

        $testo = $in_corso ? $settings['testo_in_corso'] : $settings['testo_prossimo'];
        $target = $settings['apri_nuova_finestra'] === 'yes' ? '_blank' : '_self';
        $rel = $settings['apri_nuova_finestra'] === 'yes' ? 'noopener noreferrer' : '';

        ?>
        <div class="annfu-streaming-wrapper">
            <a href="<?php echo esc_url($url); ?>"
               class="annfu-streaming-button <?php echo $in_corso ? 'annfu-streaming-live' : ''; ?>"
               target="<?php echo esc_attr($target); ?>"
               <?php if ($rel): ?>rel="<?php echo esc_attr($rel); ?>"<?php endif; ?>>
                <?php echo esc_html($testo); ?>
            </a>
        </div>
        <?php
    }
}
