<?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 LknwpCrowdfundingTemplateEndMethod 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-end-method';
    }

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

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

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

    protected function _register_controls()
    {
        $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 "Método de término"
        $this->add_control(
            'end_method_prefix',
            [
                'label' => __('Prefix', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::TEXT,
                'dynamic' => [
                    'active' => true,
                ],
                'default' => __('End Method:', 'link-nacional-wp-crowdfunding-extension'),
                'placeholder' => __('Enter prefix (e.g., End Method)', '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 método de término
        $this->add_control(
            'html_tag_end_method',
            [
                'label' => __('HTML Tag for End Method', '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}} .end-method-prefix' => 'color: {{VALUE}};',
                ],
            ]
        );

        // Cor do texto para o método de término
        $this->add_control(
            'end_method_text_color',
            [
                'label' => __('End Method Text Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#00A859', // Cor padrão (verde)
                'selectors' => [
                    '{{WRAPPER}} .end-method-value' => 'color: {{VALUE}};',
                ],
            ]
        );

        // Tipografia para o prefixo e o método de término
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'end_method_typography',
                'label' => __('Typography', 'link-nacional-wp-crowdfunding-extension'),
                'selector' => '{{WRAPPER}} .end-method-prefix, {{WRAPPER}} .end-method-value',
            ]
        );

        $this->end_controls_section();
    }

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

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

        // Obtém o valor de end_method
        $end_method = get_post_meta($post->ID, 'wpneo_campaign_end_method', true);

        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
            $end_method = 'Method';
        }

        // Verifica se o método de término é um dos esperados
        if (empty($end_method)) {
            return '';
        }

        // Inicia a variável para o conteúdo
        $output = '';

        // Concatena o prefixo do método de término com a tag personalizada
        $prefix_tag = $settings['html_tag_prefix'];
        $output .= "<{$prefix_tag} class='end-method-prefix'>";
        $output .= esc_html($settings['end_method_prefix']) . ' ';
        $output .= "</{$prefix_tag}>";

        // Concatena o valor do método de término com a tag personalizada
        $end_method_tag = $settings['html_tag_end_method'];
        $output .= "<{$end_method_tag} class='end-method-value'>";
        switch ($end_method) {
            case 'target_goal':
                $output .= __('Target goal', 'link-nacional-wp-crowdfunding-extension');
                break;
            case 'target_date':
                $output .= __('Target date', 'link-nacional-wp-crowdfunding-extension');
                break;
            case 'target_goal_and_date':
                $output .= __('Target goal and date', 'link-nacional-wp-crowdfunding-extension');
                break;
            case 'never_end':
                $output .= __('Never end', 'link-nacional-wp-crowdfunding-extension');
                break;
            default:
                $output .= __('Unknown', 'link-nacional-wp-crowdfunding-extension');
                break;
        }
        $output .= "</{$end_method_tag}>";

        // Exibe o HTML gerado
        echo wp_kses_post($output);
    }
}
