<?php

namespace LinkFiliate\Admin;

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

use LinkFiliate\App\Traits\Singleton;

/**
 * Class Menu
 *
 * Represents the admin menu management for the linkfiliate plugin.
 *
 * @package LinkFiliate\Admin
 */
class Menu
{

	use Singleton;

	/**
	 * Parent slug for the menu.
	 *
	 * @var string
	 */
	private $parent_slug = 'linkfiliate';

	/**
	 * Initializes the admin menu.
	 *
	 * @return void
	 */
	public function init()
	{
		// Hook the function to the admin menu.
		add_action('admin_menu', array($this, 'menu'));
	}

	/**
	 * Adds a menu to the WordPress admin dashboard.
	 *
	 * @return void
	 */
	public function menu()
	{

		add_menu_page(
			__('LinkFiliate', 'linkfiliate'),
			__('LinkFiliate', 'linkfiliate'),
			'manage_options',
			$this->parent_slug,
			array($this, 'admin_page'),
			LINKFILIATE_ASSETS_URL . '/icons/icon-16x16.png',
			3
		);

		$plugin_url = admin_url('/admin.php?page=' . $this->parent_slug);

		$current_page = get_admin_page_parent();

		if ($current_page === $this->parent_slug) {
			$plugin_url = '';
		}

		$submenu_pages = array(
			array(
				'parent_slug' => $this->parent_slug,
				'page_title'  => __('Dashboard', 'linkfiliate'),
				'menu_title'  => __('Dashboard', 'linkfiliate'),
				'capability'  => 'manage_options',
				'menu_slug'   => $this->parent_slug,
				'function'    => array($this, 'admin_page'), // Uses the same callback function as parent menu.
			),
			array(
				'parent_slug' => $this->parent_slug,
				'page_title'  => __('Campaign Links', 'linkfiliate'),
				'menu_title'  => __('Campaign Links', 'linkfiliate'),
				'capability'  => 'manage_options',
				'menu_slug'   => $plugin_url . '/#/campaign',
				'function'    => null,
			),
			array(
				'parent_slug' => $this->parent_slug,
				'page_title'  => __('Categories', 'linkfiliate'),
				'menu_title'  => __('Categories', 'linkfiliate'),
				'capability'  => 'manage_options',
				'menu_slug'   => $plugin_url . '/#/category',
				'function'    => null,
			),
			array(
				'parent_slug' => $this->parent_slug,
				'page_title'  => __('Clicklog', 'linkfiliate'),
				'menu_title'  => __('Clicklog', 'linkfiliate'),
				'capability'  => 'manage_options',
				'menu_slug'   => $plugin_url . '/#/clicklog',
				'function'    => null,
			),

			array(
				'parent_slug' => $this->parent_slug,
				'page_title'  => __('Import/Export', 'linkfiliate'),
				'menu_title'  => __('Import/Export', 'linkfiliate'),
				'capability'  => 'manage_options',
				'menu_slug'   => $plugin_url . '/#/import',
				'function'    => null,
			),

			array(
				'parent_slug' => $this->parent_slug,
				'page_title'  => __('Settings', 'linkfiliate'),
				'menu_title'  => __('Settings', 'linkfiliate'),
				'capability'  => 'manage_options',
				'menu_slug'   => $plugin_url . '/#/settings',
				'function'    => null, // Uses the same callback function as parent menu.
			),
		);

		$plugin_submenu_pages = apply_filters('linkfiliate_submenu_pages', $submenu_pages);

		foreach ($plugin_submenu_pages as $submenu) {

			add_submenu_page(
				$submenu['parent_slug'],
				$submenu['page_title'],
				$submenu['menu_title'],
				$submenu['capability'],
				$submenu['menu_slug'],
				$submenu['function']
			);
		}
	}

	/**
	 * Callback function for the main "linkfiliate" menu page.
	 *
	 * @return void
	 */
	public function admin_page()
	{

		echo '<div id="linkfiliate-admin" class="linkfiliate-app"></div>';
	}
}
