<?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: OpenApi
Slug: openapi
Category: development
Url: https://www.vvveb.com
Description: View and interact with REST Api.
Author: givanz
Version: 0.1
Thumb: openapi.png
Author url: https://www.vvveb.com
Settings: /admin/index.php?module=plugins/openapi/settings
*/

if (! defined('V_VERSION')) {
	die('Invalid request!');
}
use Vvveb\System\Event;

class OpenApiPlugin {
	function admin() {
		//add admin menu item
		$admin_path = \Vvveb\adminPath();
		Event::on('Vvveb\Controller\Base', 'init-menu', __CLASS__, function ($menu) use ($admin_path) {
			$menuItem = [
				'name'     => 'OpenAPI',
				'url'      => $admin_path . 'index.php?module=plugins/openapi/settings',
				'module'   => 'plugins/openapi/settings',
				//'icon-img' => PUBLIC_PATH . 'plugins/seo/seo.svg',
				'icon'   => 'icon-server-outline',
				'items'  => [
					'info' => [
						'name'     => 'Stoplight',
						'icon'     => 'icon-server-outline',
						'url'      => $admin_path . 'index.php?module=plugins/openapi/settings',
						'module'   => 'plugins/openapi/settings',
						'action'   => 'index',
					],
					'error-log' => [
						'name'     => 'Swagger',
						'url'      => $admin_path . 'index.php?module=plugins/openapi/swagger',
						'module'   => 'plugins/openapi/swagger',
						'action'   => 'index',
						'icon'     => 'icon-server-outline',
					],
				],
			];

			$menu['plugins']['items']['openapi-plugin'] = $menuItem;
			$menu['openapi-plugin'] = $menuItem;

			return [$menu];
		});
	}

	function app() {
		/*
		Event::on('Vvveb\Controller\Product\Product', 'index', __CLASS__, function ($content, $language, $slug) {
			if ($content[$language]['type'] == 'product') {
				$content[$language]['template'] = 'product/my-template.html';
			}

			return [$content, $language, $slug];
		});*/
	}

	function __construct() {
		if (APP == 'admin') {
			$this->admin();
		} else {
			if (APP == 'app') {
				$this->app();
			}
		}
	}
}

$openApiPlugin = new OpenApiPlugin();
