<?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 LknwpCrowdfundingTemplateFundRaised 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-fund-raised';
    }

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

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

    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 "Fundos Arrecadados"
        $this->add_control(
            'fund_raised_prefix',
            [
                'label' => __('Prefix', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::TEXT,
                'dynamic' => [
                    'active' => true,
                ],
                'default' => 'Fund Raised:',
                'placeholder' => __('Enter prefix (e.g., Fund Raised)', '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 dos fundos arrecadados
        $this->add_control(
            'html_tag_fund_raised',
            [
                'label' => __('HTML Tag for Funds Raised', '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}} .funds-raised-prefix' => 'color: {{VALUE}};',
                ],
            ]
        );

        // Cor do texto para o valor dos fundos arrecadados
        $this->add_control(
            'fund_raised_text_color',
            [
                'label' => __('Funds Raised Text Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#00A859',
                'selectors' => [
                    '{{WRAPPER}} .funds-raised-value' => 'color: {{VALUE}};',
                ],
            ]
        );

        // Tipografia para o prefixo e o valor dos fundos arrecadados
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'fund_raised_typography',
                'label' => __('Typography', 'link-nacional-wp-crowdfunding-extension'),
                'selector' => '{{WRAPPER}} .funds-raised-prefix, {{WRAPPER}} .funds-raised-value',
            ]
        );

        $this->end_controls_section();
    }

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

        // Obtém o produto atual
        global $post;

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

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

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

        echo '<' . esc_html($html_tag_fund_raised) . ' class="funds-raised-value">';
        echo wp_kses_post(wc_price($fund_raised));
        echo '</' . esc_html($html_tag_fund_raised) . '>';
    }
}
