<?php

namespace auctionforge\inc\shortcodes;

use DateTime;
use auctionforge\inc\helpers\LotHelper;
use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\helpers\View;
use auctionforge\inc\model\Auction;

class PastSales
{
    public function init() {
        add_shortcode('bp_past_sales', array($this, 'past_sales'));
    }

    public function past_sales($atts)
    {
        $atts = shortcode_atts( array(
            'title' => '',
            'limit' => 10
        ), $atts );
        extract($atts);

        $title = $atts['title'];
        $auctions = Auction::getArchiveAuction();
        $auctions = Auction::filterByEndedDays($auctions);
        usort($auctions, "sortAuctionDESC");

        if (!empty($auctions)) {
            $auctions = array_slice($auctions, 0, $limit);
            $html = "<div class=\"bp-row\">";
            if ($title != "") {
                $html .= "<div class=\"bp-d-flex bp-f-center\"><div class=\"bt-main__title\">".$title."</div></div>";
            }
            $html .= "<div class=\"bp-col bp-lots bp-auctions bp-d-flex bp-f-wrap bp-f-center bp-no-margin\">";
            foreach ($auctions as $auction) {
                $auctionDays = SettingsHelper::getTermOption($auction->term_id, 'auction_days');
                $auctionDays = apply_filters('auctionforge_auction_days_filter', $auctionDays, false);
                $auctionDays = \auctionforge\inc\controllers\CoverImageOverride::applyToDays($auctionDays, $auction->term_id);

                if (!is_array($auctionDays)) {
                    continue;
                }

                $auctionDays = array_reverse($auctionDays);
                $countDays = count($auctionDays);

                $auctionState = SettingsHelper::getTermOption($auction->term_id, 'auction_state');
                $mainLinkToAuction = $linkToAuction = get_category_link($auction->term_id);
                $isArchived = Auction::isAuctionArchived($auctionState);
                foreach ($auctionDays as $index => $auctionDay) {
                    if (!empty($auctionDay->image)) {
                        $featureImage = $auctionDay->image;
                    } else {
                        $featureImage = LotHelper::getPlaceholderImage();
                    }

                    $activeDayCounter = 0;

                    if ($countDays > 1) {
                        $uri = parse_url($linkToAuction);

                        if (!empty($uri['query'])) {
                            $uriParams = array();
                            parse_str($uri['query'], $uriParams);
                        }

                        $uriParams['part'] = $countDays - $index;
                        $mainLinkToAuction = $uri['path'] . "part/" . $uriParams['part'] . "/";
                    }

                    $timeZone = "+00:00";
                    if (!empty($auctionDay->unknownExactDate)) {
                        $dateTime = date("Y-m-d H:i:s", strtotime($auctionDay->date . $timeZone));
                    } else {
                        $dateTime = date("Y-m-d H:i:s", strtotime($auctionDay->date . 'T' . $auctionDay->time . $timeZone));
                    }

                    $auction->name = apply_filters('auctionforge_auction_name_build', $auctionDay->name, $auction);

                    if (!empty($uriParams['part'])) {
                        $auctionTitle = $auction->name . ",&nbsp;" . __("part", "auctionforge") . "&nbsp;" .
                            $uriParams['part'];
                    } else {
                        $auctionTitle = $auction->name;
                    }

                    if (isset($auctionDay->ended) && $auctionDay->ended == true) {
                        $activeDayCounter++;
                    }

                    $auctionParams = [
                        'index' => $index,
                        'auction' => $auction,
                        'auctionDay' => $auctionDay,
                        'featureImage' => $featureImage,
                        'auctionState' => $auctionState,
                        'dateTime' => $dateTime,
                        'linkToAuction' => $mainLinkToAuction,
                        'auctionTitle' => $auctionTitle,
                        'isArchive' => $isArchived,
                        'target' => $linkToAuction,
                        'activeDayCounter' => $activeDayCounter
                    ];
                    $html .= View::render('templates/shortcodes/past-auction', $auctionParams);
                    $uriParams = null;
                }
            }
            $html .= "</div></div>";

            return $html;
        }
    }
}
$pastSales = new PastSales();
$pastSales->init();