<?php

/**
 * @package        Joomla
 * @subpackage     Membership Pro
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2012 - 2019 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */
defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Component\ComponentHelper;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;

class os_userpoints extends MPFPayment {

    /**
     * Process payment
     *
     * @param   OSMembershipTableSubscriber  $row
     * @param   array                        $data
     *
     */
    public function processPayment($row, $data) {
        $app = Factory::getApplication();
        $Itemid = $app->input->getInt('Itemid');
        // $subscriptionStatus = $this->params->get('subscription_status');

        if (class_exists(UserPoints::class)) {
            Factory::getLanguage()->load('com_userpoints', JPATH_SITE);
            $params = ComponentHelper::getParams('com_userpoints');
            if (!UserPoints::checkRuleEnabled('up_membership_pro')) {
                $this->setPaymentErrorMessage(Text::_('AUP_SERVICE_NOT_ACTIVATED'));
                $app->redirect($this->getPaymentFailureUrl($row, $Itemid));
            }
            elseif (UserPoints::canAfford('up_membership_pro', '', '', $data['amount'] * $params->get('Points_per_unit', 1))) {
                UserPoints::newpoints('up_membership_pro', '', '', $data['item_name'], -$data['amount'] * $params->get('Points_per_unit', 1));
                $this->onPaymentSuccess($row, $row->transaction_id);
                $app->redirect($this->getPaymentCompleteUrl($row, $Itemid));
            } else {
                $this->setPaymentErrorMessage(Text::_('AUP_YOUDONOTHAVEENOUGHPOINTSTOPERFORMTHISOPERATION'));
                $app->redirect($this->getPaymentFailureUrl($row, $Itemid));
            }
        }
        else {
                $this->setPaymentErrorMessage('UserPoints is not installed');
                $app->redirect($this->getPaymentFailureUrl($row, $Itemid));
        }
    }

    /**
     * Get SEF return URL after processing payment
     *
     * @param   OSMembershipTableSubscriber  $row
     * @param   int                          $Itemid
     * @param   bool                         $absolute
     *
     * @return string
     */
    protected function getPaymentCompleteUrl($row, $Itemid, $absolute = false) {
        $langLink = OSMembershipHelper::getLangLink();

        if ($row->process_payment_for_subscription) {
            $Itemid = OSMembershipHelperRoute::getViewRoute('payment', $Itemid);

            $url = 'index.php?option=com_osmembership&view=payment&layout=complete&subscription_code=' . $row->subscription_code . '&Itemid=' . $Itemid . $langLink;
        } else {
            $url = OSMembershipHelperRoute::getViewRoute('complete', $Itemid) . '&subscription_code=' . $row->subscription_code . $langLink;
        }

        return Route::_($url, false, 0, $absolute);
    }

    /**
     * Get payment failure URL
     *
     * @param   OSMembershipTableSubscriber  $row
     * @param   int                          $Itemid
     * @param   bool                         $absolute
     *
     * @return string
     */
    protected function getPaymentFailureUrl($row, $Itemid, $absolute = false): string {
        $url = 'index.php?option=com_osmembership&view=failure&Itemid=' . $Itemid;

        return Route::_($url, false, 0, $absolute);
    }
}
