<?php

use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\helpers\View;
use auctionforge\inc\model\Auction;
use auctionforge\inc\model\Lot;

/**
 * Register query vars part
 *
 * @param  array  $vars  The array of available query variables
 *
 * @link https://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
 */

class AuctionRoutes
{
    const PARAM_LOT = "lot";
    const PARAM_PART = "part";
    const PARAM_BIDSPIRIT = "bidspirit";

    public function init()
    {
        add_filter('query_vars', array($this, 'auctions_register_query_vars'));
        add_action('init', array($this, 'auctions_do_rewrite'));
        add_action('init', array($this, 'auctions_do_redirectos'));
        add_action('init', array($this, 'auctions_do_sync_status'));

        add_filter('template_include', array($this, 'auctions_do_rules'), 99, 1);
        add_filter('template_include', array($this, 'sync_status'), 99, 1);
    }

    function auctions_do_rewrite()
    {
        add_rewrite_rule('^([^/]*)/auction/([^/]*)/' . self::PARAM_PART . '/([^/]*)/page/([0-9]{1,})/?', 'index.php?lang=$matches[1]&auction_number=$matches[2]&' .
            self::PARAM_PART . '=$matches[3]&paged=$matches[4]','top');
        add_rewrite_rule('^auction/([^/]*)/' . self::PARAM_PART . '/([^/]*)/page/([0-9]{1,})/?', 'index.php?auction_number=$matches[1]&' . self::PARAM_PART . '=$matches[2]&paged=$matches[3]','top');
        add_rewrite_rule('^([^/]*)/auction/([^/]*)/' . self::PARAM_PART . '/([^/]*)/?', 'index.php?lang=$matches[1]&auction_number=$matches[2]&' .
            self::PARAM_PART . '=$matches[3]','top');
        add_rewrite_rule('^auction/([^/]*)/' . self::PARAM_PART . '/([^/]*)/?', 'index.php?auction_number=$matches[1]&' . self::PARAM_PART . '=$matches[2]','top');
    }

    public function auctions_do_redirectos()
    {
        add_rewrite_rule('^bidspirit/([^/]*)/lot/([^/]*)/?', 'index.php?' . self::PARAM_BIDSPIRIT . '=$matches[1]&' . self::PARAM_LOT . '=$matches[2]', 'top');
        add_rewrite_rule('^bidspirit/([^/]*)/' . self::PARAM_PART . '/([^/]*)/?', 'index.php?' . self::PARAM_BIDSPIRIT . '=$matches[1]&' . self::PARAM_PART . '=$matches[2]', 'top');
        add_rewrite_rule('^bidspirit/([^/]*)/?', 'index.php?' . self::PARAM_BIDSPIRIT . '=$matches[1]', 'top');
    }

    public function auctions_do_sync_status()
    {
        add_rewrite_rule('^bidspirit/([^/]*)/?', 'index.php?bidspirit=$matches[1]', 'top');
    }

    public function auctions_do_rules($template)
    {
        if (intval(get_query_var(self::PARAM_BIDSPIRIT)) && intval(get_query_var(self::PARAM_BIDSPIRIT)) > 0) {
            $auctionId = intval(get_query_var(self::PARAM_BIDSPIRIT));
            $auction = Auction::findAuctionById($auctionId);

            if ($auction instanceof WP_Term) {
                $auctionDays = SettingsHelper::getTermOption($auction->term_id, 'auction_days');
            }
            $link = null;
            $partUrl = null;

            $varPart = get_query_var(self::PARAM_PART);
            $lotIndex = get_query_var(self::PARAM_LOT);

            if (empty($varPart)) {
                $varPart = 1;
            }

            if (!empty($varPart) && !empty($auctionDays) && count($auctionDays) > 1) {
                $partUrl = self::PARAM_PART . "/" . $varPart . "/";
            } elseif ($lotIndex) {
                $partUrl = self::PARAM_LOT . "/" . $lotIndex . "/";
            }

            if (!empty($lotIndex) && $auction instanceof WP_Term) {
                $lot = Lot::findLotByLotIndex($lotIndex, $auction->term_id);
                if (!empty($lot[0]) && $lot[0] instanceof WP_Post) {
                    $link = get_permalink($lot[0]->ID);
                }
            } elseif (empty($lotIndex) && $auction instanceof WP_Term) {
                $link = get_category_link($auction);
                if ($partUrl) {
                    $link .= $partUrl;
                }
            }

            if ($link) {
                wp_safe_redirect($link);
            }
            die;
        }

        return $template;
    }

    public function sync_status($template)
    {
        if (get_query_var('bidspirit') == 'status') {
            do_action('bidspirit_sync_progress');
            die;
        }

        return $template;
    }

    public function auctions_register_query_vars($vars)
    {
        $vars[] = self::PARAM_PART;
        $vars[] = self::PARAM_LOT;
        $vars[] = self::PARAM_BIDSPIRIT;
        return $vars;
    }
}

$instance = new AuctionRoutes();
$instance->init();
