<?php

namespace Discountify\Core\Admin;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Discountify\Utils\Singleton;

/**
 * Class Menu
 */
class Menus {


	use Singleton;

	private $capability = 'read';

	/**
	 * Initialize
	 *
	 * @return void
	 */
	public function init() {
		add_action( 'admin_menu', array( $this, 'register_admin_menu' ) );
	}


	/**
	 * Register admin menu
	 *
	 * @return void
	 */
	public function register_admin_menu() {
		$slug = 'discountify';
		// Add main page
		if ( empty( $GLOBALS['admin_page_hooks'][ $slug ] ) ) {
			$logo = \Discountify::assets_url() . 'images/logo.svg';

			add_menu_page(
				esc_html__( 'Discountify', 'discountify' ),
				esc_html__( 'Discountify', 'discountify' ),
				$this->capability,
				$slug,
				array( $this, 'discountify_view' ),
				$logo,
				10
			);
		}

		// Add submenu pages
		if ( count( $this->sub_menu_pages() ) > 0 ) {
			foreach ( $this->sub_menu_pages() as $key => $value ) {
				add_submenu_page(
					$value['parent_slug'],
					$value['page_title'],
					$value['menu_title'],
					$value['capability'],
					$value['menu_slug'],
					$value['cb_function'],
					$value['position']
				);
			}
		}

		if ( ! empty( $GLOBALS['admin_page_hooks'][ $slug ] ) ) {
			unset( $GLOBALS['submenu']['discountify'][0] );
		}
	}

	/**
	 * Create menu page
	 *
	 * @param [type] $cb_function
	 */
	public function sub_menu_pages() {
		$sub_pages = array(
			array(
				'parent_slug' => 'discountify',
				'page_title'  => esc_html__( 'Overview', 'discountify' ),
				'menu_title'  => esc_html__( 'Overview', 'discountify' ),
				'capability'  => $this->capability,
				'menu_slug'   => 'discountify_overview',
				'cb_function' => array( $this, 'over_view' ),
				'position'    => 11,
			),
			array(
				'parent_slug' => 'discountify',
				'page_title'  => esc_html__( 'Settings', 'discountify' ),
				'menu_title'  => esc_html__( 'Settings', 'discountify' ),
				'capability'  => $this->capability,
				'menu_slug'   => 'discountify',
				'cb_function' => array( $this, 'discountify_view' ),
				'position'    => 12,
			),
			array(
				'parent_slug' => 'discountify',
				'page_title'  => esc_html__( 'Discount Rules', 'discountify' ),
				'menu_title'  => esc_html__( 'Discount Rules', 'discountify' ),
				'capability'  => $this->capability,
				'menu_slug'   => 'discountify-rules',
				'cb_function' => array( $this, 'discountify_rules' ),
				'position'    => 12,
			),
		);

		if ( ! class_exists( 'DiscountifyPro' ) ) {
			$premium_link = array(
				'parent_slug' => 'discountify',
				'page_title'  => '',
				'menu_title'  => esc_html__( 'Upgrade To Premium', 'discountify' ),
				'capability'  => $this->capability,
				'menu_slug'   => 'https://wpbens.com/discountify/',
				'cb_function' => null,
				'position'    => 12,
			);

			array_push( $sub_pages, $premium_link );
		}

		return $sub_pages;
	}

	/**
	 * Discount Rules
	 *
	 * @return void
	 */
	public function discountify_rules() {
		if ( file_exists( \Discountify::core_dir() . 'admin/header.php' ) ) {
			require_once \Discountify::core_dir() . 'admin/header.php';
		}

		$url_part = 'admin/discount-rules/discount-rules.php';

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Navigation parameter for display
		$rule = ( isset( $_GET['rule'] ) ) ? sanitize_text_field( wp_unslash( $_GET['rule'] ) ) : '';
		if ( $rule === 'new-rule' || $rule === 'update-rule' ) {
			$url_part = 'admin/discount-rules/add-rules.php';
		}
		?>
		<div class="wrap">
			<?php
			if ( file_exists( \Discountify::core_dir() . 'admin/discount-rules/discount-rules.php' ) ) {
				include_once \Discountify::core_dir() . $url_part;
			}
			?>
					</div>
		<?php
	}

	/**
	 * Admin view
	 *
	 * @return void
	 */
	public function discountify_view() {
		if ( file_exists( \Discountify::core_dir() . 'admin/header.php' ) ) {
			require_once \Discountify::core_dir() . 'admin/header.php';
		}
		?>
		<div class="wrap">
			<?php include_once \Discountify::core_dir() . 'admin/settings/form.php'; ?> 
		</div>
		<?php
	}

	/**
	 * OverView
	 *
	 * @return void
	 */
	public function over_view() {
		if ( file_exists( \Discountify::core_dir() . 'admin/header.php' ) ) {
			require_once \Discountify::core_dir() . 'admin/header.php';
		}
		if ( file_exists( \Discountify::core_dir() . 'admin/overview.php' ) ) {
			include_once \Discountify::core_dir() . 'admin/overview.php';
		}
	}
}
