<?php

namespace Lknwp\CrowdfundingExtension\Includes\elementorFields;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;

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

class LknwpCrowdfundingTemplateName extends Widget_Base
{
    public function get_name()
    {
        return 'lkn-wp-crowdfunding-product-name';
    }

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

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

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

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

        // Torna o campo dinâmico
        $this->add_control(
            'product_name',
            [
                'label' => __('Campaign Name (Optional)', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::TEXT,
                'dynamic' => [
                    'active' => true,
                ],
                'default' => '',
                'placeholder' => __('Enter Campaign Name or leave empty for current product', 'link-nacional-wp-crowdfunding-extension'),
            ]
        );

        // Tag HTML para exibir o nome do produto
        $this->add_control(
            'html_tag',
            [
                'label' => __('HTML Tag', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'h1' => 'H1',
                    'h2' => 'H2',
                    'h3' => 'H3',
                    'p' => 'Paragraph',
                    'div' => 'Div',
                ],
                'default' => 'h2',
            ]
        );

        $this->end_controls_section();

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

        // Cor do texto
        $this->add_control(
            'text_color',
            [
                'label' => __('Text Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#000000',
                'selectors' => [
                    '{{WRAPPER}} .lkn-product-name' => 'color: {{VALUE}};',
                ],
            ]
        );

        // Tipografia
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'typography',
                'label' => __('Typography', 'link-nacional-wp-crowdfunding-extension'),
                'selector' => '{{WRAPPER}} .lkn-product-name',
            ]
        );

        $this->end_controls_section();
    }

    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $product_name = sanitize_text_field($settings['product_name']);
        $html_tag = esc_html($settings['html_tag']);

        // Verifica se está no editor do Elementor ou no modo de visualização
        if (\Elementor\Plugin::$instance->editor->is_edit_mode() || is_preview() || get_post_type() !== 'product') {
            echo '<' . esc_html($html_tag) . ' class="lkn-product-name">' . esc_html__('Sample Campaign Name', 'link-nacional-wp-crowdfunding-extension') . '</' . esc_html($html_tag) . '>';
            return;
        }

        // Se o campo estiver vazio, tenta obter o produto pelo contexto
        if (empty($product_name)) {
            if (is_singular('product')) {
                global $post;
                $product_id = $post->ID;
            } else {
                return '';
            }
        } else {
            // Busca o produto por SKU ou nome
            $product_id = wc_get_product_id_by_sku($product_name) ?: wc_get_product_id_by_name($product_name);
        }

        // Carrega o produto e exibe o título
        if ($product_id) {
            $product = wc_get_product($product_id);
            if ($product) {
                echo '<' . esc_html($html_tag) . ' class="lkn-product-name">' . esc_html($product->get_title()) . '</' . esc_html($html_tag) . '>';
            } else {
                echo '';
            }
        } else {
            echo '';
        }
    }
}
