<?php

namespace auctionforge\inc\helpers;

use auctionforge\inc\controllers\BidspiritLots;
use auctionforge\inc\model\Lot;
use WP_Post;

class Conditions
{
    public static function isTaxTemplate()
    {
        return is_tax(BidspiritLots::TAX_AUCTION_TYPE) && is_archive();
    }

    /**
     * @param $post WP_Post
     * @return bool
     */
    public static function isSingleLotTemplate($post)
    {
        return is_single() && !empty($post) && $post->post_type == BidspiritLots::POST_TYPE;
    }

    /**
     * @param $post WP_Post
     * @param $pageAuctionListKey string
     * @param $auctionPolylangId string
     * @return bool
     */
    public static function isAuctionListTemplate($post, $pageAuctionListKey, $auctionPolylangId)
    {
        return self::isPageTemplate($post, $pageAuctionListKey, $auctionPolylangId);
    }

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

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

    /**
     * @param $post WP_Post
     * @param $pageProfileKey string
     * @param $profilePolylangId string
     * @return bool
     */
    public static function isProfileTemplate($post, $pageProfileKey, $profilePolylangId)
    {
        return self::isPageTemplate($post, $pageProfileKey, $profilePolylangId);
    }

    /**
     * @param $post WP_Post
     * @param $pageKey string
     * @param $polylangId string
     * @return bool
     */
    public static function isPageTemplate($post, $pageKey, $polylangId)
    {
        return is_page() && !empty($post) && ($post->ID == $pageKey || $post->ID == $polylangId);
    }

    /**
     * @param $lot Lot
     * @param $metaLoadImages string
     * @param $postId int
     * @param $lastUpdate int
     * @return bool
     */
    public static function isSyncImages($lot, $metaLoadImages, $postId, $lastUpdate)
    {
        $cdnUse = SettingsHelper::getOption('cdn_images', 'on');
        return ($cdnUse === 'off' && SettingsHelper::isChecked('sync_images') &&
            (!$metaLoadImages || !has_post_thumbnail($postId) || (int)$lastUpdate < (int)$lot->getLastUpdate())
        );
    }

    public static function isNotSyncTitle($post)
    {
        return self::isSyncOption('sync_title', $post);
    }

    public static function isNotSyncDesc($post)
    {
        return self::isSyncOption('sync_desc', $post);
    }

    private static function isSyncOption($key, $post)
    {
        return !SettingsHelper::isChecked($key) && !empty($post);
    }

    public static function isAuctionPage($query)
    {
        return !is_admin() && ($query->is_post_type_archive('lot') || $query->is_tax(BidspiritLots::TAX_AUCTION_TYPE));
    }

    public static function isMainQuery($query)
    {
        return !is_admin() && $query->is_main_query();
    }
}