<?php

/*
 * @component UserPoints
 * @copyright Copyright (C) 2015-2016 - Bernard Gilly - Adrien Roussel
 * @license : GNU/GPL
 * @Website : https://remository.com
 */

namespace BlackSheepResearch\Component\UserPoints\Site\Viewers;

// no direct access
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;
use BlackSheepResearch\Component\UserPoints\Site\Problems\SQL;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;
use BlackSheepResearch\Component\UserPoints\Site\Problems\PointsDetails;

class PurchaseHTML extends UserView {

    protected $task = '';
    protected $payamount = 0.0;
    protected $fpoints = 0.0;
    protected $maximum_spend = 0;
    protected $currency = '';
    protected $show_currency = '';
    protected $csymbol = '';
    protected $points_per_unit = 0;
    protected $button_html = '';
    protected $howmany = 0;
    protected $token = '';

    public function _displaylist($task, $currency, $payamount, $itemname, $fpoints, $maximum_spend=0, $points_per_unit=0) {
        $this->task = $task;
        $this->payamount = $payamount;
        $this->itemname = $itemname;
        $this->fpoints = $fpoints;
        $this->maximum_spend = $maximum_spend;
        $this->currency = $currency;
        if ('GBP' == $currency) {
            $this->csymbol = '£';
            $this->show_currency = '';
        } elseif ('USD' == $currency) {
            $this->csymbol = '$';
            $this->show_currency = '';
        } else {
            $this->csymbol = '';
            $this->show_currency = $currency;
        }
        $this->points_per_unit = $points_per_unit;
        $this->howmany = $points_per_unit * $payamount;
        if ('payment' == $this->task) {
            $this->button_html = $this->getPayageButtons($payamount, $itemname);
            if (!$this->button_html) {
                return;
            }
        }
        $this->token = HTMLHelper::_('form.token');
        parent::display();
    }

    private function getPayageButtons ($gross_amount, $itemname) {
        $parmlist = [
            'app_name' => 'UserPoints',
            'item_name' => $itemname,
            'layout' => 4,
            'gross_amount' => 0.0
        ];
        if (!file_exists(JPATH_ADMINISTRATOR . '/components/com_payage/api.php')) {
            echo Text::_('COM_PAYAGE_TEST_NOT_INSTALLED');
            return;
        }
        require_once JPATH_ADMINISTRATOR . '/components/com_payage/api.php';
        $detail_id = $this->createDetail();
        if (!$detail_id) {
            return '';
        }
        $parmlist['currency'] = $this->currency;
        $parmlist['gross_amount'] = $gross_amount;
        $parmlist['app_transaction_details'] = [
            UserPoints::getReferreid(),
            $detail_id
            ];
        $parmlist['app_return_url'] = Uri::root(true).'/index.php?option=com_userpoints&view=purchase&task=complete';
        $parmlist['app_update_path'] = JPATH_ROOT.'/components/com_userpoints/src/Problems/PurchaseUpdate.php';
        $return_status = \PayageApi::get_buttons($parmlist, $html);
        if ($return_status) {
            Factory::getApplication()->enqueueMessage(Text::_('AUP_PURCHASE_BUTTON_ERROR_' . $return_status));
            return '';
        }
        return $html;
    }
    
    private function createDetail () {
        $detail = new PointsDetails();
        $detail->referreid = UserPoints::getReferreid();
        $detail->rule_id = 'sysplgup_buypoints';
        $detail->points = 0.0;
        $detail->insert_date = SQL::datetimeSQL();
        $detail->status = 1;
        $detail->approved = 0;
        $detail->keyreference = '';
        $detail->datareference = Text::_('AUP_PURCHASE_PENDING');
        $detail->enabled = 0;
        if ($detail->save()) {
            return $detail->id;
        }
    }

}
