<?php

namespace Lknwp\CrowdfundingExtension\Includes\elementorFields;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\helpers\LknwpCrowdfundingListingFunctions;
use Elementor\Group_Control_Typography;

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

class LknwpCrowdfundingTemplateRaisedPercent 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-raised-percent';
    }

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

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

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

    protected function _register_controls()
    {
        $this->start_controls_section(
            'content_section',
            [
                'label' => __('Settings', 'link-nacional-wp-crowdfunding-extension'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        // Campo para tag HTML
        $this->add_control(
            'html_tag_message',
            [
                'label' => __('HTML Tag for Message', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'span' => 'Span',
                    'p' => 'Paragraph',
                    'div' => 'Div',
                    'h1' => 'H1',
                    'h2' => 'H2',
                    'h3' => 'H3',
                ],
                'default' => 'p',
            ]
        );

        $this->add_control(
            'html_tag_value',
            [
                'label' => __('HTML Tag for Percentage Value', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'span' => 'Span',
                    'p' => 'Paragraph',
                    'div' => 'Div',
                    'h1' => 'H1',
                    'h2' => 'H2',
                    'h3' => 'H3',
                    'strong' => 'Strong',
                ],
                'default' => 'strong',
            ]
        );

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

        $this->end_controls_section();

        $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_message',
            [
                'label' => __('Text Color for Message', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#000000',
                'selectors' => [
                    '{{WRAPPER}} .lkn-raised-message' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'text_color_value',
            [
                'label' => __('Text Color for Percentage Value', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#00A859', // Cor verde padrão
                'selectors' => [
                    '{{WRAPPER}} .lkn-raised-percent-value' => 'color: {{VALUE}};',
                ],
            ]
        );

        // Cor da barra de progresso
        $this->add_control(
            'progress_bar_color',
            [
                'label' => __('Progress Bar Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#00A859', // Cor verde padrão
                'selectors' => [
                    '{{WRAPPER}} .lkn-progress-bar' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

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

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

        // Calcula o percentual arrecadado
        $raised_percent = $this->lknwpHelper->get_raised_percent($post->ID);

        if ((\Elementor\Plugin::$instance->editor->is_edit_mode() || is_preview())) {
            $raised_percent = 10;
        } elseif (get_post_type() !== 'product') {
            $raised_percent = 0;
        }

        if ($raised_percent === null) {
            return '';
        }

        // Exibe a div com o percentual e a barra de progresso
        $html_tag_message = $settings['html_tag_message'];
        $html_tag_value = $settings['html_tag_value'];

        echo '<div class="lkn-raised-container">';

        // Cria a div para englobar a mensagem e o valor, com display flex
        echo '<div class="lkn-raised-percent-wrapper">';

        // Exibe a mensagem com a tag e cor definidas
        echo '<' . esc_html($html_tag_message) . ' class="lkn-raised-message">' . esc_html__('Raised Percentage:', 'link-nacional-wp-crowdfunding-extension') . '</' . esc_html($html_tag_message) . '>';

        // Exibe o valor percentual com a tag e cor definidas
        echo '<' . esc_html($html_tag_value) . ' class="lkn-raised-percent-value">' . esc_html($raised_percent) . '%</' . esc_html($html_tag_value) . '>';

        echo '</div>'; // Fecha a div com display flex

        // Exibe a barra de progresso
        echo '<div class="lkn-progress-container">';
        echo '<div class="lkn-progress-bar" style="width: ' . esc_attr($raised_percent) . '%;"></div>';
        echo '</div>';

        echo '</div>';
    }
}
