<?php

namespace auctionforge\inc\import;

use Exception;
use auctionforge\inc\controllers\PolylangSupport;
use auctionforge\inc\Debug;
use auctionforge\inc\helpers\LotHelper;
use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\ImageUploader;
use auctionforge\inc\model\Auction;
use auctionforge\inc\traits\LoggerInfo;
use WP_Post;
use WP_Term;

class AuctionErrors
{
    use LoggerInfo;

    private $errors;
    private $auctionSlug;
    private $auctionId;

    /**
     * AuctionErrors constructor.
     * @param $errors
     */
    public function __construct($auctionSlug, $errors)
    {
        $this->setAuctionSlug($auctionSlug);
        $this->setAuctionId($auctionSlug);
        $this->setErrors($errors);
    }

    public function run()
    {
        $trigger = apply_filters('auctionforge_auction_errors_trigger', true, $this->getAuctionId());
        if ($trigger && !empty($this->getErrors())) {
            foreach ($this->getErrors() as $error) {
                try {
                    $error = json_decode($error);
                    if (!empty($error->errorCode) && $error->errorCode == "NOT_FOUND" || $error->errorCode == "BAD_PARAM_VALUE") {
                        if (function_exists('pll_get_term_translations') && PolylangSupport::isFullSupport()) {
                            $termTranslate = pll_get_term_translations($this->getAuctionId());
                            if (!empty($termTranslate)) {
                                foreach ($termTranslate as $key => $translate) {
                                    $this->deleteAuction($translate);
                                }
                            }
                        } else {
                            $this->deleteAuction($this->getAuctionId());
                        }
                    }
                } catch (Exception $e) {
                    return false;
                }
            }
        }
    }

    private function deleteAuction($termId)
    {
        $postIds = Auction::getAllAuctionPostsBS($termId);

        LotHelper::movedToTrashPosts($postIds);

        $res = wp_delete_term($termId, 'auction_number', ['default' => 0, 'force_default' => true]);

        if ($res == true) {
            $this->setLoggedInfo(array(
                "auction_id_" . $termId . "_error" => array(
                    "was removed" => $termId
                )
            ));
        } elseif ($res instanceof \WP_Error) {
            $this->setLoggedInfo(array(
                "auction_id_" . $termId . "_error" => array(
                    "error removed" => $res->get_error_message()
                )
            ));
        } else {
            $this->setLoggedInfo(array(
                "auction_id_" . $termId . "_error" => array(
                    "error" => "not removed"
                )
            ));
        }
    }

    /**
     * @return mixed
     */
    public function getErrors()
    {
        return $this->errors;
    }

    /**
     * @param mixed $errors
     */
    public function setErrors($errors)
    {
        $this->errors = $errors;
    }

    /**
     * @return mixed
     */
    public function getAuctionSlug()
    {
        return $this->auctionSlug;
    }

    /**
     * @param mixed $auctionSlug
     */
    public function setAuctionSlug($auctionSlug)
    {
        $this->auctionSlug = $auctionSlug;
    }

    /**
     * @return mixed
     */
    public function getAuctionId()
    {
        return $this->auctionId;
    }

    /**
     * @param $auctionSlug
     */
    public function setAuctionId($auctionSlug)
    {
        /** @var WP_Term $auction */
        $auction = Auction::findAuctionById($auctionSlug);
        $this->auctionId = $auction->term_id;
    }
}
