<?php

namespace Devnet\PingVid\Admin;


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


class AdminSide
{

	private $plugin_name;
	private $version;

	public function __construct($plugin_name, $version)
	{

		$this->plugin_name = $plugin_name;
		$this->version     = $version;

		add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']);
		add_action('admin_menu', [$this, 'admin_menu'], 100);
		add_filter('plugin_action_links_' . DEVNET_PINGVID_PATH_FILE, [$this, 'plugin_action_links']);
	}

	public function enqueue_assets($hook)
	{

		if ($hook === 'toplevel_page_pingvid') {
			$base_url = plugin_dir_url(__DIR__) . 'assets/build/';

			wp_enqueue_style('wp-color-picker');
			wp_enqueue_style($this->plugin_name, $base_url . 'admin.css', [], $this->version, 'all');

			wp_enqueue_media();
			wp_enqueue_script('wp-color-picker-alpha', plugin_dir_url(__DIR__) . 'assets/color-picker/wp-color-picker-alpha.min.js', ['wp-color-picker'], $this->version, true);

			$info = file_exists($base_url . 'admin.asset.php') ? include $base_url . 'admin.asset.php' : ['dependencies' => ['jquery'], 'version' => $this->version];

			wp_enqueue_script($this->plugin_name, $base_url . 'admin.js', $info['dependencies'], $info['version'], true);

			wp_localize_script($this->plugin_name, 'devnet_pingvid_script_data', [
				'ajaxurl' => admin_url('admin-ajax.php'),
				'nonce'   => wp_create_nonce('wp_rest'),
			]);
		}
	}

	public function admin_menu()
	{

		$plugin_settings = new Settings();

		$menu_slug = 'pingvid';

		add_menu_page(
			esc_html__('PingVid', 'pingvid'),
			esc_html__('PingVid', 'pingvid'),
			'edit_posts',
			$menu_slug,
			[$plugin_settings, 'settings_page'],
			'dashicons-format-video',
			56
		);

		add_submenu_page(
			$menu_slug,
			esc_html__('Settings', 'pingvid'),
			esc_html__('Settings', 'pingvid'),
			'edit_posts',
			$menu_slug
		);
	}

	public function plugin_action_links($links)
	{

		$custom_links   = [];
		$custom_links[] = '<a href="' . esc_url(get_admin_url(null, 'admin.php?page=pingvid')) . '">' . esc_html__('Settings', 'pingvid') . '</a>';

		return array_merge($custom_links, $links);
	}
}
