<?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 LknwpCrowdfundingTemplateDaysRemaining 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-days-remaining';
    }

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

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

    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(
            'days_remaining_prefix',
            [
                'label' => __('Prefix', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::TEXT,
                'dynamic' => [
                    'active' => true,
                ],
                'default' => __('Days Remaining:', 'link-nacional-wp-crowdfunding-extension'),
                'placeholder' => __('Enter prefix (e.g., Days Remaining)', '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_days_remaining',
            [
                'label' => __('HTML Tag for Days Remaining', '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}} .days-remaining-prefix' => 'color: {{VALUE}};',
                ],
            ]
        );

        // Cor do texto para o valor dos fundos arrecadados
        $this->add_control(
            'days_remaining_text_color',
            [
                'label' => __('Days Remaining Text Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#00A859', // Cor padrão (verde)
                'selectors' => [
                    '{{WRAPPER}} .days-remaining-value' => 'color: {{VALUE}};',
                ],
            ]
        );

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

        $this->end_controls_section();
    }

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

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

        $days_remaining = $this->lknwpHelper->get_date_remaining($post->ID);
        $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
            $days_remaining = '999';
        }

        if ($end_method === "never_end") {
            $days_remaining = __('Never end', 'link-nacional-wp-crowdfunding-extension');
        } elseif ($days_remaining == '0') {
            $days_remaining = __('Expired', 'link-nacional-wp-crowdfunding-extension');
        }

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

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

        echo '<' . esc_html($html_tag_days_remaining) . ' class="days-remaining-value">';
        echo esc_html($days_remaining);
        echo '</' . esc_html($html_tag_days_remaining) . '>';
    }
}
