<?php
use Proxim\Application;
use Proxim\Configuration;
use Proxim\Module\Module;
use Proxim\Module\PaymentModule;
use Proxim\Site\Site;
use Proxim\Util\ArrayUtils;
use Proxim\Validate;

class PayPal extends PaymentModule
{
    /**
    * Stripe Prestashop configuration
    */
    const PAYPAL_ENABLED = 'PAYPAL_ENABLED';
    const PAYPAL_MODE = 'PAYPAL_MODE';
    const PAYPAL_CLIENT_ID = 'PAYPAL_CLIENT_ID';
    const PAYPAL_SECRET_KEY = 'PAYPAL_SECRET_KEY';

    public function __construct()
    {
        $this->name = 'paypal';
        $this->icon = 'fab fa-paypal';
        $this->version = '1.0.0';
        $this->prox_versions_compliancy = array('min' => '1.0.0', 'max' => Application::VERSION);
        $this->author = 'Davison Pro';

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

        $this->displayName = 'PayPal';
        $this->description = 'Accepts payments by PayPal';
    }

    public function isActive() {
        return Configuration::get(self::PAYPAL_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;

        $smarty->assign([
            'paypal_mode' => Configuration::get( self::PAYPAL_MODE ),
            'paypal_client' => Configuration::get( self::PAYPAL_CLIENT_ID ),
            'paypal_secret' => Configuration::get( self::PAYPAL_SECRET_KEY ),
            'payee_email' => $user->email
        ]);
        
        return $this->getTemplateContent('paypal.order');
    }

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

        $smarty->assign([
            'paypal_mode' => Configuration::get( self::PAYPAL_MODE ),
            'paypal_client' => Configuration::get( self::PAYPAL_CLIENT_ID ),
            'paypal_secret' => Configuration::get( self::PAYPAL_SECRET_KEY ),
            'payee_email' => $user->email
        ]);
        
        return $this->getTemplateContent('paypal.add-payment');
    }

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

        $smarty->assign([
            'paypal_mode' => Configuration::get( self::PAYPAL_MODE ),
            'paypal_client' => Configuration::get( self::PAYPAL_CLIENT_ID ),
            'paypal_secret' => Configuration::get( self::PAYPAL_SECRET_KEY ),
            'payee_email' => $user->email
        ]);
        
        return $this->getTemplateContent('paypal.wallet');
    }

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

        $smarty->assign([
            'paypal_mode' => Configuration::get( self::PAYPAL_MODE ),
            'paypal_client' => Configuration::get( self::PAYPAL_CLIENT_ID ),
            'paypal_secret' => Configuration::get( self::PAYPAL_SECRET_KEY ),
            'payee_email' => $user->email
        ]);
        
        return $this->getTemplateContent('paypal.tip');
    }

    /**
    * Hook action verify payment
    * @param $params array
    * @return void
    */
    public function verifyPayment($node_id, $handle, $payload) 
    {
        PaymentModule::processPayment( $node_id, $handle, $payload );
    }

}