<?php
namespace auctionforge\inc\helpers;

use auctionforge\inc\abs\ImageModel;
use auctionforge\inc\controllers\BidspiritLots;
use auctionforge\inc\model\Auction;
use auctionforge\inc\model\Lot;
use auctionforge\inc\model\LotImage;
use auctionforge\inc\model\LotMetaData;
use WP_Term;

class BS_API
{
    public static $taxonomy = Auction::TERM;
    public static $post_type = BidspiritLots::POST_TYPE;
    public static $date_format = Auction::DATE_FORMAT;

    const LABEL_ORIGINAL = ImageModel::LABEL_ORIGINAL;
    const LABEL_CUSTOM = ImageModel::LABEL_CUSTOM;
    const LABEL_MIN = ImageModel::LABEL_MIN;
    const LABEL_MEDIUM = ImageModel::LABEL_MEDIUM;
    const LABEL_ATTACH = ImageModel::LABEL_ATTACH;

    const IMAGE_HOST_CLONED = ImageModel::IMAGE_HOST_CLONED;
    const IMAGE_HOST = ImageModel::IMAGE_HOST;
    const HOST_FILES = ImageModel::HOST_FILES;

    const STATE_READY = Auction::STATE_READY;
    const STATE_PENDING = Auction::STATE_PENDING;
    const STATE_RUNNING = Auction::STATE_RUNNING;
    const STATE_ARCHIVED = Auction::STATE_ARCHIVED;
    const STATE_ENDING = Auction::STATE_ENDING;
    const DATE_FORMAT = Auction::DATE_FORMAT;
    const META_FIELD_TAGS = Auction::META_FIELD_TAGS;

    public static function getSettingsValue($field, $default)
    {
        return SettingsHelper::getOption($field, $default);
    }

    public static function getTermOption($termId, $key)
    {
        return SettingsHelper::getTermOption($termId, $key);
    }

    public static function getAuctionKeys($termIds, $key)
    {
        global $wpdb;
        $sql = "SELECT tm.meta_value FROM " . $wpdb->termmeta . " AS tm WHERE tm.term_id IN (" . implode(",", $termIds) . ") AND tm.meta_key = '%s'";
        $sql = $wpdb->prepare($sql, $key);
        return $wpdb->get_col($sql);
    }

    /**
     * @param $state
     * @return bool
     */
    public static function isAuctionArchived($state)
    {
        return Auction::isAuctionArchived($state);
    }

    /**
     * Get Active auctions
     * @return WP_Term[]
     */
    public static function getActiveAuction($isSyncQuery = true)
    {
        return Auction::getActiveAuction(true, $isSyncQuery);
    }

    public static function getArchiveAuction($isSyncQuery = true)
    {
        return Auction::getArchiveAuction(false, $isSyncQuery);
    }

    public static function findAuctionById($auctionId)
    {
        return Auction::findAuctionById($auctionId);
    }

    public static function findAuctionByIdWIthLang($auctionId, $langCode)
    {
        return Auction::findAuctionByIdWithLang($auctionId, $langCode);
    }

    /**
     * Нужно доработать, добавить параметры. New function!!!
     * @param $termIds
     * @param $count
     * @return int[]|\WP_Post[]
     */
    public static function getFeaturedLotsInAuction($termIds, $count)
    {
        return Lot::getFeaturedLotsInAuction($termIds, $count);
    }

    public static function getFeaturedLots($count)
    {
        return Lot::getLots($count);
    }

    public static function getLotHelper($postId)
    {
        return new LotHelper($postId);
    }

    public static function getFeatureImage($postId, $size)
    {
        $cdnUse = SettingsHelper::getOption('cdn_images', 'on');
        $imageList = '';
        $isCloned = 'false';
        $templateImageList = 'false';

        $lotHelper = new LotHelper($postId);

        $cdnUseCondition = apply_filters('auctionforge_cdn_use_conditions', ($cdnUse == 'on'), $cdnUse, $lotHelper);
        if ($cdnUseCondition) {
            $lotImages = new LotImage($postId);
            $images = $lotImages->getImageList();
            if (!empty($images[0])) {
                $featureImage = $images[0][$size];
            } else {
                $featureImage = LotHelper::getPlaceholderImage();
            }

        } else {
            $featureImage = get_the_post_thumbnail_url($postId, 'full');
            $images = get_attached_media('image');
        }

        return $featureImage;
    }

    public static function getLotImages($postId)
    {
        $lotImages = new LotImage($postId);
        return $lotImages->getImageList();
    }

    public static function getPreviousLots($lotIndex, $auctionId, $count, $lang = 'en')
    {
        $args = array(
            "numberposts" => $count,
            "post_status" => array(SettingsHelper::STATUS_PUBLISH),
            "post_type" => "lot",
            LotMetaData::BP_QUERY => array(
                "bp_index" => array(1),
                "term_id" => intval($auctionId),
                "lang" => $lang
            ),
            "bp_orderby" => "paddedIndex",
            "suppress_filters" => false
        );
        $args[LotMetaData::BP_QUERY]["bp_field"][] = array(
            "field" => "itemIndex",
            "value" => $lotIndex,
            "order" => "DESC",
            "compare" => '<'
        );
        return get_posts($args);
    }

    public static function getNextLots($lotIndex, $auctionId, $count, $lang = 'en')
    {
        $args = array(
            "numberposts" => $count,
            "post_status" => array(SettingsHelper::STATUS_PUBLISH),
            "post_type" => "lot",
            LotMetaData::BP_QUERY => array(
                "bp_index" => array(1),
                "term_id" => intval($auctionId),
                "lang" => $lang
            ),
            "bp_orderby" => "paddedIndex",
            "order" => "ASC",
            "suppress_filters" => false
        );
        $args[LotMetaData::BP_QUERY]["bp_field"][] = array(
            "field" => "itemIndex",
            "value" => $lotIndex,
            "compare" => '>'
        );
        return get_posts($args);
    }

    public static function dateTimeFormat($dateTime)
    {
        return SettingsHelper::dateTimeFormat($dateTime);
    }

    public static function getPlaceholderImage()
    {
        return LotHelper::getPlaceholderImage();
    }

    public static function isTaxTemplate()
    {
        return Conditions::isTaxTemplate();
    }

    public static function isSingleLotTemplate($post)
    {
        return Conditions::isSingleLotTemplate($post);
    }

    public static function isAuctionListTemplate($post, $pageAuctionListKey, $auctionPolylangId)
    {
        return Conditions::isAuctionListTemplate($post, $pageAuctionListKey, $auctionPolylangId);
    }

    public static function isManualAuctionListTemplate($post, $pageAuctionListKey, $auctionPolylangId)
    {
        return Conditions::isManualAuctionListTemplate($post, $pageAuctionListKey, $auctionPolylangId);
    }

    public static function isArchiveListTemplate($post, $pageArchiveListKey, $archivePolylangId)
    {
        return Conditions::isArchiveListTemplate($post, $pageArchiveListKey, $archivePolylangId);
    }

    public static function isProfileTemplate($post, $pageProfileKey, $profilePolylangId)
    {
        return Conditions::isProfileTemplate($post, $pageProfileKey, $profilePolylangId);
    }
}
