<?php

namespace YSquare\GiftCert\Model;

use OxidEsales\Eshop\Core\Field;
use OxidEsales\Eshop\Core\Price;

class User extends User_parent {
    /**
     * Adds money to the giftcert pool
     * @param $value
     */
    public function addYSGiftcertPoolAmount($value) {
        $currentAmount = $this->oxuser__ys_giftcert_pool_amount->value;
        $newAmount = $currentAmount + $value;
        $this->oxuser__ys_giftcert_pool_amount = new Field($newAmount);
        $this->save();
    }

    /**
     * Subtracts money from the gifcert pool
     * @param $value
     */
    public function subtractYSGiftcertPoolAmount($value) {
        $this->addYSGiftcertPoolAmount($value * -1);
    }

    /**
     * Checks if there is any money in the pool of that user
     *
     * @return bool
     */
    public function hasYSGiftcertPoolAmount() {
        if($this->oxuser__ys_giftcert_pool_amount->value && $this->oxuser__ys_giftcert_pool_amount->value > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Returns the actual money value in the pool
     * @return mixed|Price
     */
    public function getYSGiftcertPoolAmount(){
        $amount = 0;
        if($this->oxuser__ys_giftcert_pool_amount->value) {
            $amount = $this->oxuser__ys_giftcert_pool_amount->value;
        }

        $myPrice = oxNew(Price::class);
        $myPrice->setPrice($amount);
        $this->_applyCurrency($myPrice);
        return $myPrice;
    }

    protected function _applyCurrency(\OxidEsales\Eshop\Core\Price $oPrice, $oCur = null) // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
    {
        if (!$oCur) {
            $oCur = $this->getConfig()->getActShopCurrencyObject();
        }

        $oPrice->multiply($oCur->rate);
    }


}