<?php
use Proxim\Application;
use Proxim\Configuration;
use Proxim\Module\Module;
use Proxim\Module\PaymentModule;
use Proxim\Order\AdditionalService;
use Proxim\Order\Order;
use Proxim\Site\Site;
use Proxim\Tools;
use Proxim\User\Customer;
use Proxim\Util\ArrayUtils;
use Proxim\Validate;

class SafePay extends PaymentModule
{
    /**
    * Safepay configuration
    */
    const SAFEPAY_ENABLED = 'SAFEPAY_ENABLED';
    const SAFEPAY_MODE = 'SAFEPAY_MODE';
    const SAFEPAY_LIVE_KEY = 'SAFEPAY_LIVE_KEY';
    const SAFEPAY_SANBOX_KEY = 'SAFEPAY_SANBOX_KEY';
    const SAFEPAY_SECRET_KEY = 'SAFEPAY_SECRET_KEY';

    public function __construct()
    {
        $this->name = 'safepay';
        $this->icon = 'fa fa-credit-card';
        $this->version = '1.0.0';
        $this->prox_versions_compliancy = array('min' => '1.0.0', 'max' => Application::VERSION);
        $this->author = 'Davison Pro';
        $this->displayName = 'Safe Pay';
        $this->description = 'Set up Safe Pay as a payment method and enable users to pay without leaving your website';

        $this->bootstrap = true;
        parent::__construct();
    }

    public function isActive() {
        return Configuration::get(self::SAFEPAY_ENABLED);
    }

    /**
     * Echoes a template.
     *
     * @param string $templateName Template name
     */
    public function showTemplate($templateName)
    {
        echo $this->getTemplateContent($templateName);
    }

    /**
     * Return a template.
     *
     * @param string $templateName          Template name
     * @param array  $additionnalParameters Additionnal parameters to inject on the Twig template
     *
     * @return string Parsed template
     */
    private function getTemplateContent($templateName, $additionnalParameters = array())
    {
        $this->smarty->assign($additionnalParameters);
        return $this->fetch(__DIR__ . '/views/' . $templateName.'.tpl');
    }

    public function processOrderCheckout($order, $currency) {
        $app = $this->application;
        $smarty = $this->smarty;
        $user = $app->container->user;

    }

    public function processAddPaymentCheckout($addPayment, $currency) {
        $app = $this->application;
        $smarty = $this->smarty;
        $user = $app->container->user;

    }

    public function processWalletCheckout($amount, $currency) {
        $app = $this->application;
        $smarty = $this->smarty;
        $user = $app->container->user;

        
    }

    public function processTipCheckout($amount, $currency) {
        $app = $this->application;
        $smarty = $this->smarty;
        $user = $app->container->user;

        
    }

    /**su
     * Check Safepay webhook request is valid.
     * @param  string $tracker
     */
    public function validateSignature($tracker, $signature)
    {
        $secret = Configuration::get('SAFEPAY_SECRET_KEY');
        $signature_2 = hash_hmac('sha256', $tracker, $secret);

        if ($signature_2 === $signature) {
            return true;
        }

        return false;
    }

    /**
    * Hook action verify payment
    * @param $params array
    * @return void
    */
    public function verifyPayment($node_id, $handle, $payload) 
    {
        $app = $this->application;
        $tracker = ArrayUtils::get($payload, 'tracker');
        $sig = ArrayUtils::get($payload, 'sig');

        PaymentModule::processPayment( $node_id, $handle, $payload );
    }
}