<?php

namespace Lknwp\CrowdfundingExtension\Includes\elementorFields;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;

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

class LknwpCrowdfundingTemplateFavoriteCampaign extends Widget_Base
{
    private $uniqId;

    public function __construct($data = [], $args = null)
    {
        parent::__construct($data, $args);

        $this->uniqId = uniqid();
    }

    public function get_name()
    {
        return 'lkn-wp-crowdfunding-favorite-campaign';
    }

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

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

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

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

        $this->add_control(
            'lkn_active_color',
            [
                'label' => __('Active Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#e52e44',
            ]
        );

        $this->add_control(
            'lkn_active_shadow_color_1',
            [
                'label' => __('Active Shadow Color 1', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#fa4654',
            ]
        );

        $this->add_control(
            'lkn_active_shadow_color_2',
            [
                'label' => __('Active Shadow Color 2', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#fb626e',
            ]
        );

        $this->add_control(
            'lkn_inactive_color',
            [
                'label' => __('Inactive Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#ffffff',
            ]
        );

        $this->add_control(
            'lkn_background_color',
            [
                'label' => __('Background Color', 'link-nacional-wp-crowdfunding-extension'),
                'type' => Controls_Manager::COLOR,
                'default' => '#00A859',
            ]
        );

        $this->end_controls_section();
    }

    protected function render()
    {
        $user_id = get_current_user_id();
        if (!$user_id) {
            return;
        }

        $settings = $this->get_settings_for_display();
        $edit_mode = false;

        $active_color = $settings['lkn_active_color'];
        $active_shadow_color_1 = $settings['lkn_active_shadow_color_1'];
        $active_shadow_color_2 = $settings['lkn_active_shadow_color_2'];
        $inactive_color = $settings['lkn_inactive_color'];

        $user_id = get_current_user_id(); // Obtém o ID do usuário logado
        $product_id = get_the_ID(); // Obtém o ID do produto atual
        $author_id = get_post_field('post_author', $product_id);
        $loved_campaign_ids = get_user_meta($user_id, 'loved_campaign_ids', true); // Recupera os IDs favoritos do usuário
        $loved_campaign_ids = !empty($loved_campaign_ids) ? json_decode($loved_campaign_ids, true) : [];

        // Verifica se o produto está nos favoritos do usuário
        $is_favorited = in_array($product_id, $loved_campaign_ids);

        // Define as cores do SVG com base no estado
        $cls_2_fill = $is_favorited ? $active_color : $inactive_color;
        $cls_3_fill = $is_favorited ? $active_shadow_color_1 : $inactive_color;
        $cls_4_fill = $is_favorited ? $active_shadow_color_2 : $inactive_color;

        // Gera o valor para o input hidden
        $colors_value = implode(',', [
            "$active_color",
            "$active_shadow_color_1",
            "$active_shadow_color_2",
            "$inactive_color"
        ]);

        if (\Elementor\Plugin::$instance->editor->is_edit_mode() || is_preview()) {
            $edit_mode = true;
        }

        if (get_post_type() === 'product' || \Elementor\Plugin::$instance->editor->is_edit_mode() || is_preview()) {
            echo '<div class="lkn-heart-container">';
            echo '<input type="hidden" class="lkn-edit-mode" name="lkn-edit-mode" value="' . ($edit_mode ? 'active' : 'inactive') . '">';
            echo '<input type="hidden" class="lkn-edit-mode" name="lkn-user-id" value="' . esc_attr($user_id) . '">';
            echo '<input type="hidden" class="lkn-edit-mode" name="lkn-user-product-id" value="' . esc_attr($author_id) . '">';
            echo '<input type="hidden" class="lkn-heart-status" name="lkn-heart-status" value="' . ($is_favorited ? 'active' : 'inactive') . '">';
            echo '<input type="hidden" class="lkn-heart-colors" name="lkn-heart-colors" value="' . esc_attr($colors_value) . '">';
            echo '<button class="lkn-heart-btn" data-product-id="' . esc_attr($product_id) . '" data-user-id="' . esc_attr($user_id) . '">
                <svg class="lkn-heart" data-name="50" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
                    <title>Heart icon</title>
                    <circle class="cls-1 cls-1-' . esc_attr($this->uniqId) . '" cx="256" cy="256" r="256" fill="' . esc_attr($settings['lkn_background_color']) . '"/>
                    <path class="cls-2 cls-2-' . esc_attr($this->uniqId) . '" d="M409.05,283.21,273.89,418.36a25.28,25.28,0,0,1-35.78,0L126,306.21l-23-23A92,92,0,1,1,233,153.15l23,23,23-23A92,92,0,0,1,409.05,283.21Z" fill="' . esc_attr($cls_2_fill) . '"/>
                    <path class="cls-3 cls-3-' . esc_attr($this->uniqId) . '" d="M409.05,283.21,299.56,392.7a25.29,25.29,0,0,1-35.79,0L151.61,280.54l-23-23a92,92,0,0,1-11.56-116,92,92,0,0,1,116,11.57l23,23,23-23A92,92,0,0,1,409.05,283.21Z" fill="' . esc_attr($cls_3_fill) . '"/>
                    <ellipse class="cls-4 cls-4-' . esc_attr($this->uniqId) . '" cx="373.08" cy="169.49" rx="23.33" ry="38.57" transform="translate(14.73 368.23) rotate(-53.45)" fill="' . esc_attr($cls_4_fill) . '"/>
                    <ellipse class="cls-4 cls-4-' . esc_attr($this->uniqId) . '" cx="409.8" cy="225.81" rx="14.74" ry="24.37" transform="translate(-28.72 62.63) rotate(-8.45)" fill="' . esc_attr($cls_4_fill) . '"/>
                </svg>
            </button>';
            echo '</div>';
        }
    }
}
