<?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 LknwpCrowdfundingTemplateFundingGoal 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-funding-goal';
    }

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

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

    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 "Objetivo de Arrecadação"
        $this->add_control(
            'funding_goal_prefix',
            [
                'label' => __('Prefix', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::TEXT,
                'dynamic' => [
                    'active' => true,
                ],
                'default' => __('Funding Goal:', 'link-nacional-wp-crowdfunding-extension'),
                'placeholder' => __('Enter prefix (e.g., Funding Goal)', '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 do objetivo de arrecadação
        $this->add_control(
            'html_tag_funding_goal',
            [
                'label' => __('HTML Tag for Funding Goal', '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', // Cor padrão (preto)
                'selectors' => [
                    '{{WRAPPER}} .funding-goal-prefix' => 'color: {{VALUE}};',
                ],
            ]
        );

        // Cor do texto para o valor do objetivo de arrecadação
        $this->add_control(
            'funding_goal_text_color',
            [
                'label' => __('Funding Goal Text Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#00A859', // Cor padrão (verde)
                'selectors' => [
                    '{{WRAPPER}} .funding-goal-value' => 'color: {{VALUE}};',
                ],
            ]
        );

        // Tipografia para o prefixo e o valor do objetivo de arrecadação
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'funding_goal_typography',
                'label' => __('Typography', 'link-nacional-wp-crowdfunding-extension'),
                'selector' => '{{WRAPPER}} .funding-goal-prefix, {{WRAPPER}} .funding-goal-value',
            ]
        );

        $this->end_controls_section();
    }

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

        $prefix = sanitize_text_field($settings['funding_goal_prefix']);

        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
            $funding_goal = 0;
        } else {
            $funding_goal = $this->lknwpHelper->get_total_goal($post->ID);
        }

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

        // Exibir o HTML diretamente
        echo '<' . esc_html($html_tag_prefix) . ' class="funding-goal-prefix">';
        echo esc_html($prefix) . ' ';
        echo '</' . esc_html($html_tag_prefix) . '>';

        echo '<' . esc_html($html_tag_funding_goal) . ' class="funding-goal-value">';
        echo wp_kses_post(wc_price($funding_goal));
        echo '</' . esc_html($html_tag_funding_goal) . '>';
    }
}
