<?php

namespace Lknwp\CrowdfundingExtension\Includes\elementorFields;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\helpers\LknwpCrowdfundingListingFunctions;

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

class LknwpCrowdfundingTemplateLocation extends Widget_Base
{
    private $lknwpHelper;

    public function __construct($data = [], $args = null)
    {
        parent::__construct($data, $args);

        $this->lknwpHelper = new LknwpCrowdfundingListingFunctions();
    }

    public function get_name()
    {
        return 'lkn-wp-crowdfunding-location';
    }

    public function get_title()
    {
        return __('Location', 'link-nacional-wp-crowdfunding-extension');
    }

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

    public function get_categories()
    {
        return ['lkn-wp-crowdfunding-extension'];
    }

    protected function _register_controls()
    {
        // Seção de conteúdo (Prefixo e Tags HTML)
        $this->start_controls_section(
            'content_section',
            [
                'label' => __('Content', 'link-nacional-wp-crowdfunding-extension'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        // Campo para prefixo antes do texto de "Localização da Campanha"
        $this->add_control(
            'campaign_location_prefix',
            [
                'label' => __('Prefix', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::TEXT,
                'dynamic' => [
                    'active' => true,
                ],
                'default' => 'Localização:',
                'placeholder' => __('Enter prefix (e.g., Location)', 'link-nacional-wp-crowdfunding-extension'),
            ]
        );

        // Tag HTML para personalizar o estilo do prefixo
        $this->add_control(
            'html_tag_prefix',
            [
                'label' => __('HTML Tag for Prefix', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'span' => 'Span',
                    'p' => 'Paragraph',
                    'div' => 'Div',
                    'strong' => 'Strong',
                    'em' => 'Emphasize',
                ],
                'default' => 'span',
            ]
        );

        // Tag HTML para o valor da localização
        $this->add_control(
            'html_tag_location',
            [
                'label' => __('HTML Tag for Location Value', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'span' => 'Span',
                    'p' => 'Paragraph',
                    'div' => 'Div',
                    'strong' => 'Strong',
                    'em' => 'Emphasize',
                ],
                'default' => 'span',
            ]
        );

        $this->end_controls_section();

        // Seção de Estilo (Cor e Tipografia)
        $this->start_controls_section(
            'style_section',
            [
                'label' => __('Style', 'link-nacional-wp-crowdfunding-extension'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        // Cor do texto para o prefixo
        $this->add_control(
            'prefix_text_color',
            [
                'label' => __('Prefix Text Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#000000',
                'selectors' => [
                    '{{WRAPPER}} .campaign-location-prefix' => 'color: {{VALUE}};',
                ],
            ]
        );

        // Cor do texto para o valor da localização
        $this->add_control(
            'location_text_color',
            [
                'label' => __('Location Text Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#00A859',
                'selectors' => [
                    '{{WRAPPER}} .campaign-location-value' => 'color: {{VALUE}};',
                ],
            ]
        );

        // Tipografia para o prefixo e o valor da localização
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'campaign_location_typography',
                'label' => __('Typography', 'link-nacional-wp-crowdfunding-extension'),
                'selector' => '{{WRAPPER}} .campaign-location-prefix, {{WRAPPER}} .campaign-location-value',
            ]
        );

        $this->end_controls_section();
    }

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

        // Obtém a localização da campanha
        $location = $this->lknwpHelper->campaign_location();

        if (\Elementor\Plugin::$instance->editor->is_edit_mode() || is_preview() || get_post_type() !== 'product') {
            // Se estiver no editor ou no modo de preview, exibe apenas uma mensagem
            $html_tag_location = __('Unknown', 'link-nacional-wp-crowdfunding-extension');
        }

        if (empty($location)) {
            return '';
        }

        // Obter as tags HTML configuradas
        $html_tag_prefix = $settings['html_tag_prefix'];
        $html_tag_location = $settings['html_tag_location'];

        // Exibir o HTML diretamente com echo
        echo '<' . esc_html($html_tag_prefix) . ' class="campaign-location-prefix">';
        echo esc_html($settings['campaign_location_prefix']) . ' ';
        echo '</' . esc_html($html_tag_prefix) . '>';

        echo '<' . esc_html($html_tag_location) . ' class="campaign-location-value">';
        echo esc_html($location);
        echo '</' . esc_html($html_tag_location) . '>';
    }
}
