<?php

namespace Lknwp\CrowdfundingExtension\Includes\elementorFields;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\controls\LknwpCrowdfundingListingAuthorControl;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\controls\LknwpCrowdfundingListingButtonsControl;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\controls\LknwpCrowdfundingListingDaysControl;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\controls\LknwpCrowdfundingListingDescriptionControl;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\controls\LknwpCrowdfundingListingFundRaisedControl;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\controls\LknwpCrowdfundingListingGoalControl;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\controls\LknwpCrowdfundingListingImage;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\controls\LknwpCrowdfundingListingLocationControl;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\controls\LknwpCrowdfundingListingMainContent;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\controls\LknwpCrowdfundingListingProgressControl;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\controls\LknwpCrowdfundingListingTitleControl;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\database\LknwpCrowdfundingListingQuery;
use Lknwp\CrowdfundingExtension\Includes\elementorFields\helpers\LknwpCrowdfundingListingFunctions;

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

class LknwpCrowdfundingListingWidget 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-extension-listing';
    }

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

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

    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,
            ]
        );

        LknwpCrowdfundingListingMainContent::getControl($this);

        $this->end_controls_section();

        /////////////////////////////////////////////////////////////

        LknwpCrowdfundingListingImage::getControl($this);
        LknwpCrowdfundingListingButtonsControl::getControl($this);
        LknwpCrowdfundingListingTitleControl::getControl($this);
        LknwpCrowdfundingListingAuthorControl::getControl($this);
        LknwpCrowdfundingListingDescriptionControl::getControl($this);
        LknwpCrowdfundingListingLocationControl::getControl($this);
        LknwpCrowdfundingListingProgressControl::getControl($this);
        LknwpCrowdfundingListingGoalControl::getControl($this);
        LknwpCrowdfundingListingFundRaisedControl::getControl($this);
        LknwpCrowdfundingListingDaysControl::getControl($this);
    }

    public function render()
    {
        $settings = $this->get_settings_for_display();
        $thumbnail = '';

        $mainBackgroundColor = 'background-color: #FFFFFF;';
        if (!empty($settings['main_background_color'])) {
            $mainBackgroundColor = 'background-color: ' . $settings['main_background_color'] . ';';
        }

        $buttonsSettings = LknwpCrowdfundingListingButtonsControl::getFields($settings);
        $titleSettings = LknwpCrowdfundingListingTitleControl::getFields($settings);
        $authorSettings = LknwpCrowdfundingListingAuthorControl::getFields($settings);
        $descriptionSettings = LknwpCrowdfundingListingDescriptionControl::getFields($settings);
        $locationSettings = LknwpCrowdfundingListingLocationControl::getFields($settings);
        $progressSettings = LknwpCrowdfundingListingProgressControl::getFields($settings);
        $goalSettings = LknwpCrowdfundingListingGoalControl::getFields($settings);
        $fundRaisedSettings = LknwpCrowdfundingListingFundRaisedControl::getFields($settings);
        $daysSettings = LknwpCrowdfundingListingDaysControl::getFields($settings);

        $campaign_query = LknwpCrowdfundingListingQuery::getQuery($settings);

        // Define as campanhas e os dados necessários
        $campaigns = [];

        if ($campaign_query->have_posts()) {
            while ($campaign_query->have_posts()) {
                $campaign_query->the_post();

                $thumbnail_id = get_post_thumbnail_id(get_the_ID());
                if ($thumbnail_id) {
                    $thumbnail = get_the_post_thumbnail(get_the_ID(), $settings['thumbnail_size']);
                    $thumbnail = str_replace('class="', 'class="lknwp-image ', $thumbnail);
                } else {
                    $placeholder_id = get_option('woocommerce_placeholder_image');
                    $thumbnail = wp_get_attachment_image($placeholder_id, $settings['thumbnail_size']);
                    $thumbnail = str_replace('class="', 'class="lknwp-image ', $thumbnail);
                }

                $end_method = get_post_meta(get_the_ID(), 'wpneo_campaign_end_method', true);

                $campaigns[] = [
                    'id'             => get_the_ID(),
                    'title'          => get_the_title(),
                    'permalink'      => get_permalink(),
                    'thumbnail'      => $thumbnail,
                    'excerpt'        => wp_kses(htmlspecialchars_decode(get_the_excerpt()), []),
                    'location'       => $this->lknwpHelper->campaign_location(),
                    'author_name'    => $this->lknwpHelper->get_author_name(),
                    'author_url'     => $this->lknwpHelper->get_author_url(get_the_author_meta('user_login')),
                    'raised_percent' => $this->lknwpHelper->get_raised_percent(get_the_ID()),
                    'funding_goal'   => $this->lknwpHelper->get_total_goal(get_the_ID()),
                    'fund_raised'    => $this->lknwpHelper->get_total_fund(get_the_ID()),
                    'days_remaining' => $this->lknwpHelper->get_date_remaining(get_the_ID()),
                    'end_method'     => $end_method,
                ];
            }
        }

        // Inclui o template e passa os dados
        require LKNWP_CROWDFUNDING_EXTENSION_DIR . 'Includes/elementorFields/views/LknwpCrowdfundingListingCard.php';

        wp_reset_postdata();
    }

    public static function lknwp_get_settings()
    {
        return $this->get_settings_for_display();
    }
}
