<?php

namespace YSquare\GiftCert\Controller;

use OxidEsales\Eshop\Application\Controller\AccountController;
use OxidEsales\Eshop\Core\Registry;

class AccountGiftcertController extends AccountController {
    /**
     * Current class template name.
     *
     * @var string
     */
    protected $_sThisTemplate = 'page/account/giftcert.tpl';

    /**
     * Status of redemption
     * 0 = no redemption at all
     * 1 = error
     * 2 = success
     *
     * @var int
     */
    protected $redeemStatus = 0;

    /**
     * If user is not logged in - returns name of template AccountOrderController::_sThisLoginTemplate, or if user is
     * already logged in - returns name of template AccountOrderController::_sThisTemplate
     *
     * @return string $_sThisTemplate current template file name
     */
    public function render()
    {
        parent::render();

        // is logged in ?
        $oUser = $this->getUser();
        if (!$oUser) {
            return $this->_sThisTemplate = $this->_sThisLoginTemplate;
        }
        if($this->redeemStatus > 0) {
            $this->_aViewData['redeemStatus'] = $this->redeemStatus;
        }
        return $this->_sThisTemplate;
    }

    public function redeemGiftcert(){
        $giftcertID = Registry::getRequest()->getRequestEscapedParameter("giftcertCode");
        $oGiftcert = oxNew(\YSquare\GiftCert\Model\GiftCert::class);

        try {
            $oGiftcert->getGiftcertByNr($giftcertID, true);
            $oGiftcert->isNotReserved();
        } catch (\OxidEsales\Eshop\Core\Exception\VoucherException $oEx) {
            \OxidEsales\Eshop\Core\Registry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
            $this->redeemStatus = 1;
            return;
        }

        $restvalue = $oGiftcert->ysgiftcert__ysrestdiscount->value;
        $user = $this->getUser();
        $oGiftcert->markAsUsed("-", $user->oxuser__oxid->value,$restvalue);
        $user->addYSGiftcertPoolAmount($restvalue);

        $this->redeemStatus = 2;
    }

    /**
     * Returns Bread Crumb - you are here page1/page2/page3...
     *
     * @return array
     */
    public function getBreadCrumb()
    {
        $aPaths = [];
        $aPath = [];
        $iBaseLanguage = Registry::getLang()->getBaseLanguage();
        $sSelfLink = $this->getViewConfig()->getSelfLink();

        $aPath['title'] = Registry::getLang()->translateString('MY_ACCOUNT', $iBaseLanguage, false);
        $aPath['link'] = Registry::getSeoEncoder()->getStaticUrl($sSelfLink . 'cl=account');
        $aPaths[] = $aPath;

        $aPath['title'] = Registry::getLang()->translateString('YS_GIFTCERT_ACCOUNT_BREADCRUMB', $iBaseLanguage, false);
        $aPath['link'] = $this->getLink();
        $aPaths[] = $aPath;

        return $aPaths;
    }
    
    /**
     * Returns full page title
     *
     * @return string
     */
    public function getPageTitle()
    {
        $seoTitle = Registry::getLang()->translateString("YS_SEO_TITLE_ACCOUNTGIFTCERT");
        if(!empty($seoTitle)){
            return $seoTitle;
        }else{
            return parent::getPageTitle();
        }
    }
}