<?php

/**
 * @version		3.5.7
 * @package		Joomla
 * @subpackage	EShop
 * @author  	Giang Dinh Truong
 * @copyright	Copyright (C) 2012 Ossolution Team
 * @license		GNU/GPL, see LICENSE.php
 */
// no direct access
defined('_JEXEC') or die();

use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Uri\Uri;
use Joomla\Registry\Registry;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Component\ComponentHelper;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;

class os_userpoints extends os_payment {

    /**
     * Process Payment
     *
     * @param   DMSTableOrder  $row
     * @param   array          $data
     */
    public function processPayment($data) {
        $app = Factory::getApplication();
        $Itemid = $app->input->getInt('Itemid', 0);
        $completeUrl = Route::_('index.php?option=com_eshop&view=checkout&layout=complete&Itemid=' . $Itemid, false, 0, true);
        $failureUrl = Route::_('index.php?option=com_eshop&view=checkout&layout=failure&Itemid=' . $Itemid, false, 0, true);
        // $subscriptionStatus = $this->params->get('subscription_status');

        if (class_exists(UserPoints::class)) {
            Factory::getLanguage()->load('com_userpoints', JPATH_SITE);
            $params = ComponentHelper::getParams('com_userpoints');
            $total = $data['totals'][1]['value'];
            if (!UserPoints::checkRuleEnabled('up_eshop_payment')) {
                $this->errorReport(Text::_('AUP_SERVICE_NOT_ACTIVATED'));
            } elseif (UserPoints::canAfford('up_eshop_payment', '', '', $total * $params->get('Points_per_unit', 1))) {
                UserPoints::newpoints('up_eshop_payment', '', '', $data['order_number'] . ' - ' . $data['invoice_number'], -$total * $params->get('Points_per_unit', 1));
                $this->paymentMade($data);
            } else {
                $this->errorReport(Text::_('AUP_YOUDONOTHAVEENOUGHPOINTSTOPERFORMTHISOPERATION'));
             }
        } else {
            $this->errorReport('UserPoints is not installed');
        }
    }

    private function errorReport($message) {
        Factory::getSession()->set('omnipay_payment_error_reason', $message);
        Factory::getApplication()->redirect(Route::_(EshopRoute::getViewRoute('checkout').'&layout=failure'));
    }

    private function paymentMade($data) {
        $row = Table::getInstance('Eshop', 'Order');
        $id = $data['order_id'];
        $row->load($id);
        // Change order status based on the Order Status setting
        $row->order_status_id = $this->params->get('order_status', EshopHelper::getConfigValue('order_status_id'));
        $row->store();

        EshopHelper::completeOrder($row);
        PluginHelper::importPlugin('eshop');
        Factory::getApplication()->triggerEvent('onAfterCompleteOrder', array($row));
        //Send confirmation email here
        if (EshopHelper::getConfigValue('order_alert_mail')) {
            EshopHelper::sendEmails($row);
        }
        Factory::getApplication()->redirect(Route::_(EshopRoute::getViewRoute('checkout') . '&layout=complete'));
    }
}
