<?php

namespace YSquare\GiftCert\Model;

use OxidEsales\Eshop\Core\Registry;

class PaymentList extends PaymentList_parent
{

    /**
     * Returns the payment methods that can be used in the special situation
     *
     * 1) If vouchers are in basket we limit the payment method to the configured ones
     * 2) If Products can be payed completely by pool money, we only give the user an "empty" payment method
     *
     * @param $sShipSetId
     * @param $dPrice
     * @param null $oUser
     * @return mixed
     */
    public function getPaymentList($sShipSetId, $dPrice, $oUser = null)
    {
        $aPayments = parent::getPaymentList($sShipSetId, $dPrice, $oUser);

        //check module settings for allowed payments
        $aAllowedPayments_online = Registry::getConfig()->getConfigParam('aYsGiftcertAllowedPaymenttypes') ?? [];
        $aAllowedPayments_offline = Registry::getConfig()->getConfigParam('aYsGiftcertAllowedPaymenttypesOffline') ?? [];
        $aAllowedPayments_business = Registry::getConfig()->getConfigParam('aYsGiftcertAllowedPaymenttypesBusiness') ?? [];
        $PaymentTypeForPoolPayment = Registry::getConfig()->getConfigParam('sYsGiftcertPaymentForPool');

        $oBasket = $this->getSession()->getBasket();
        if ($oBasket) {

            // Case 1: Many ifs:
            // If the money pool is active AND
            // if the user wants to pay with the pool AND
            // the pool balance is enough to fully pay the basket
            // OR
            // the basket itself has a price of 0 (maybe cause of a giftcert)
            // -> then we use ONLY a "fake" payment method that is configured in the module settings
            if ((Registry::getConfig()->getConfigParam('blYsGiftcertRedeemToPool')
                    && $oBasket->getGiftCertUsedPoolAmount()->getPrice() > 0
                    && $oBasket->getPrice()->getPrice() <= 0)
                || $oBasket->getPrice()->getPrice() <= 0
            ) {
                $viewNameGenerator = Registry::get(\OxidEsales\Eshop\Core\TableViewNameGenerator::class);
                $paymentsView = $viewNameGenerator->getViewName("oxpayments");
                $this->selectString("SELECT * FROM {$paymentsView} WHERE oxid='{$PaymentTypeForPoolPayment}' AND oxactive='1'");
                return $this->_aArray;
            } else {
                // Actually this payment type should ONLY be shown in that case,
                // so we remove it from the list, if we are in "normal" mode
                unset($aPayments[$PaymentTypeForPoolPayment]);
            }

            // Case 2: Case 1 does not match BUT
            // there is a giftcert in the basket we only allow payments that are suitable for gift certs
            $blGiftcert = false;
            $blGiftcert_online = false;
            $blGiftcert_offline = false;
            $blGiftcert_business = false;
            $aContents = $oBasket->getContents(true);

            //check if we have a gift cert in basket
            foreach ($aContents as $oItem) {
                $oArticle = $oItem->getArticle();
                if ($oArticle->isGiftCertArticle() && !$oArticle->isOfflineGiftCert() && !$oArticle->isBusinessGiftCert()) {
                    $blGiftcert_online = true;
                }
                if ($oArticle->isOfflineGiftCert()) {
                    $blGiftcert_offline = true;
                }
                if ($oArticle->isBusinessGiftCert()) {
                    $blGiftcert_business = true;
                }
            }

            $allowedPayments = null;
            if($blGiftcert_online) {
                $allowedPayments = $allowedPayments ? array_intersect($allowedPayments, $aAllowedPayments_online): $aAllowedPayments_online;
            }
            if($blGiftcert_offline) {
                $allowedPayments = $allowedPayments ? array_intersect($allowedPayments, $aAllowedPayments_offline): $aAllowedPayments_offline;
            }
            if($blGiftcert_business) {
                $allowedPayments = $allowedPayments ? array_intersect($allowedPayments, $aAllowedPayments_business): $aAllowedPayments_business;
            }


            // TODO differentiate between the different gift certs
            if (($blGiftcert_online || $blGiftcert_offline || $blGiftcert_business) && is_array($allowedPayments) && count($allowedPayments) > 0) {
                //remove disallowed payments from selection
                foreach ($aPayments as $sKey => $oPayments) {
                    if (!in_array($sKey, $allowedPayments)) {
                        unset($aPayments[$sKey]);
                    }
                }
            }
        }

        return $aPayments;
    }
}