<?php

namespace twa\omnipay\controllers;

use twa\omnipay\models\EcomPurchasesAttemptModel;

class SimplifyController
{
    private $data;

    private $provider;

    public function __construct($data, $provider)
    {

        $this->provider = $provider;
        $required_data = ['private_key', 'public_key'];
        \twa\omnipay\controllers\GeneralFunctions::checkRequiredData($data, $required_data);
        $this->data = collect($data);
    }

    public static function updateGatewayInit($id,$reference){
        $atttempt = EcomPurchasesAttemptModel::find($id);
        $atttempt->gateway_init = $reference;
        $atttempt->save();

        return true;
    }

    public function createPayment($id, $value, $currency, $customer_name = null, $customer_email = null, $url_success = null, $url_cancel = null)
    {
        $token = md5($id.$this->provider.$value.$currency.env('APP_KEY'));

        return collect(['redirect' => ['link' => env('APP_URL').'/omnipay/redirect/simplify/'.$id.'/'.$this->provider.'/'.$value.'/'.$currency.'/'.$token]]);
    }

    public function getSession($data)
    {
        return false;
    }

    public function checkPayment($id)
    {

        $attempt = EcomPurchasesAttemptModel::where('id', $id)->first();
        \twa\omnipay\repositories\Simplify::$publicKey = $this->data['public_key'];
        \twa\omnipay\repositories\Simplify::$privateKey = $this->data['private_key'];

        $payment = \twa\omnipay\repositories\Simplify\Simplify_Payment::findPayment($attempt['gateway_init']);
        $payment = collect($payment);

        if (isset($payment['paymentStatus']) && $payment['paymentStatus'] == 'APPROVED') {
            return response(['status' => 'success', 'response' => $payment], 200);
        } else {

            return response(['status' => 'fail', 'response' => $payment], 400);
        }
    }

    public function chargeCard($id, $provider, $amount, $currency, $token)
    {
        \twa\omnipay\repositories\Simplify::$publicKey = $this->data['public_key'];
        \twa\omnipay\repositories\Simplify::$privateKey = $this->data['private_key'];

        $payment = \twa\omnipay\repositories\Simplify\Simplify_Payment::createPayment([
            'amount' => $amount*100,
            'token' => request()->input('simplifyToken'),
            'description' => "Your purchase from ".env('APP_URL'),
            'currency' => $currency,
        ]);
   
        $payment = collect($payment);
        if (! isset($payment['paymentStatus']) || $payment['paymentStatus'] != 'APPROVED') {

            echo '<script language="javascript">window.location="'.request()->fullUrl().'?notification=Your payment was not accepted";</script>';
            exit();
        } else {

            //we try to update the payment attempt
            $attempt = EcomPurchasesAttemptModel::where('id', $id)->first();
            $attempt->gateway_init = $payment['id'];
            $attempt->save();
            echo '<script language="javascript">window.location="'.env('APP_URL').'/omnipay/response/success/'.$id.'";</script>';
            exit();
        }
    }
}
