<?php

/**
 * Vvveb
 *
 * Copyright (C) 2022  Ziadin Givan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

/*
Name: Omnipay 3 payment gateways
Slug: omnipay
Category: payment
Url: https://www.vvveb.com
Description: Supported gateways: Paypal Express Checkout, Paypal Pro Checkout, Paypal Rest, Mollie, Dummy.
Author: givanz
Version: 0.1
Thumb: omnipay.svg
Author url: https://www.vvveb.com
Settings: /admin/index.php?module=plugins/omnipay/settings
*/

use function Vvveb\getSetting;
use Vvveb\Plugins\Omnipay\Payment as PaymentMethod;
use function Vvveb\slugify;
use Vvveb\System\Core\Request;
use Vvveb\System\Event;
use Vvveb\System\Locale;
use Vvveb\System\Payment as PaymentApi;
use Vvveb\System\Routes;

if (! defined('V_VERSION')) {
	die('Invalid request!');
}

include __DIR__ . '/vendor/autoload.php';

class OmnipayPlugin {
	private $namespace = 'omnipay';

	public function init() {
		$settings = getSetting($this->namespace, null, []);
		Routes::addRoute('/omnipay',  ['module' => 'plugins/omnipay/index/index']);
		Routes::addRoute('/omnipay-notify/{key}/{gateway}/#index#/#order_id#',  ['module' => 'plugins/omnipay/index/notify']);
		Routes::addRoute('/omnipay-success/{key}/{gateway}/#index#/#order_id#',  ['module' => 'plugins/omnipay/index/success']);
		Routes::addRoute('/omnipay-cancel/{key}/{gateway}/#index#/#order_id#',  ['module' => 'plugins/omnipay/index/cancel']);

		$payment = PaymentApi::getInstance();

		foreach ($settings as $index => $method) {
			$lang = $method['lang'][Locale::getLanguageId()] ?? [];

			if ($lang) {
				unset($method['lang']);
				$method += $lang;
			}

			$method['index'] = $index;
			$name            = $method['gateway'] . '-' . $index;
			$method['index'] = $index;
			$payment->registerMethod($name, PaymentMethod::class, $method);
		}
	}

	public function admin() {
		$request = Request::getInstance();
		$module  = $request->get['module'] ?? '';

		if ($module == 'order/order' || $module == 'settings/site') {
			$this->init();
		}
	}

	public function app() {
		$this->init();

		Event::on('Vvveb\Controller\Base', 'init', __CLASS__, function ($site) {
			$request = Request::getInstance();
			$route = $request->get['route'] ?? '';
			$module = $request->get['module'] ?? '';

			if ($route == '/checkout' || $module == 'checkout/checkout/index' || $route == '/cart/cart') {
				$this->init();
			}

			return [$site];
		});
	}

	public function __construct() {
		if (\Vvveb\isEditor()) {
			return;
		}

		if (APP == 'admin') {
			$this->admin();
		} elseif (APP == 'app') {
			$this->app();
		}
	}
}

$omnipay = new OmnipayPlugin();
