<?php

namespace Lknwp\CrowdfundingExtension\Includes\elementorFields;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

class LknwpCrowdfundingTemplateAverageRating extends Widget_Base
{
    public function get_name()
    {
        return 'lkn-wp-crowdfunding-average-rating';
    }

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

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

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

    protected function register_controls()
    {
        // Aba de Estilo
        $this->start_controls_section(
            'style_section',
            [
                'label' => __('Style', 'link-nacional-wp-crowdfunding-extension'),
                'tab'   => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'star_color',
            [
                'label'     => __('Star Color', 'link-nacional-wp-crowdfunding-extension'),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .lkn-star-rating span:before' => 'color: {{VALUE}}',
                ],
                'default'   => '#FFD700',
            ]
        );

        $this->end_controls_section();
    }

    protected function render()
    {
        // Obtém o ID do produto dinamicamente
        $product_id = get_the_ID();
        $product = wc_get_product($product_id);

        if ($product) {
            // Calcula a avaliação média
            $average_rating = $product->get_average_rating();
            $rating_count = $product->get_rating_count();
        } else {
            $average_rating = 0;
            $rating_count = 0;
        }

        $output = '';
        if (\Elementor\Plugin::$instance->editor->is_edit_mode() || is_preview()) {
            $average_rating = 3.5;
            $rating_count = 3.5;
            // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
            $output .= '<img src="' . LKNWP_CROWDFUNDING_EXTENSION_URL . 'Includes/assets/images/rating.png' . '" alt="Rating Image" />';
            $output .= '<p>' . esc_html(__('Example image', 'link-nacional-wp-crowdfunding-extension')) . '</p>';
        } else {
            if ($rating_count === 0) {
                return '';
            }
            if (get_post_type() !== 'product') {
                return '';
            }
            $output = wc_get_rating_html($average_rating, $rating_count);
        }



        // Renderiza a média de avaliações
        echo '<div class="lkn-average-rating-widget">';
        // translators: Rating component
        echo '<div class="lkn-star-rating" title="' . esc_attr(sprintf(__('Rated %s out of 5', 'link-nacional-wp-crowdfunding-extension'), $average_rating)) . '">';
        echo wp_kses_post($output);
        echo '</div>';
        echo '</div>';
    }
}
