<?php

namespace Devnet\EBC\Admin;

use Devnet\EBC\Includes\Helper;

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;

		$page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : null;

		add_action('admin_enqueue_scripts', [$this, 'enqueue_styles']);
		add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
		add_action('admin_menu', [$this, 'admin_menu'], 100);

		if ($page === 'easy-booking-calendar-settings') {
			add_filter('admin_footer_text', [$this, 'admin_credits']);
		}

		add_filter('plugin_action_links_' . DEVNET_EBC_PATH, [$this, 'plugin_action_links']);

		add_action('before_woocommerce_init', [$this, 'hpos_compatible']);
	}

	public function enqueue_styles($hook)
	{
		if ($hook !== 'product_page_easy-booking-calendar-settings') {
			return;
		}

		wp_enqueue_style('wp-color-picker');
		wp_enqueue_style($this->plugin_name, plugin_dir_url(__DIR__) . 'assets/build/admin.css', [], $this->version, 'all');
	}

	public function enqueue_scripts($hook)
	{
		if ($hook !== 'product_page_easy-booking-calendar-settings') {
			return;
		}

		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);

		$script_asset_path = plugin_dir_url(__DIR__) . 'assets/build/admin.asset.php';
		$script_info       = file_exists($script_asset_path)
			? include $script_asset_path
			: ['dependencies' => ['jquery'], 'version' => $this->version];

		wp_enqueue_script(
			$this->plugin_name,
			plugin_dir_url(__DIR__) . 'assets/build/admin.js',
			$script_info['dependencies'],
			$script_info['version'],
			true
		);

		// wp_localize_script($this->plugin_name, 'devnet_ebc_script_data', [
		//     'ajaxurl'      => admin_url('admin-ajax.php'),
		//     'nonce'        => wp_create_nonce('easy-booking-calendar-nonce'),
		// ]);
	}

	public function admin_menu()
	{

		$plugin_settings = new Settings();

		add_submenu_page(
			'edit.php?post_type=product',
			esc_html__('Booking Calendar', 'easy-booking-calendar'),
			esc_html__('Booking Calendar', 'easy-booking-calendar'),
			'edit_posts',
			'easy-booking-calendar-settings',
			[$plugin_settings, 'settings_page'],
		);
	}


	public function admin_credits($footer_text)
	{
		$footer_text  = '';
		$footer_text .= '<div class="ecb-admin-footer">';
		$footer_text .= '<div class="ecb-footer-message">';
		$footer_text .= 'Please rate <strong>Easy Booking Calendar</strong> <a href="https://wordpress.org/support/plugin/easy-booking-calendar/reviews/?rate=5#new-post" target="_blank">★★★★★</a> on <a href="https://wordpress.org/support/plugin/easy-booking-calendar/reviews/?rate=5#new-post" target="_blank">WordPress.org</a> to help us spread the word. Thank you from the <a href="https://devnet.hr/" target="_blank">Devnet</a> team!';
		$footer_text .= '</div>';
		$footer_text .= '</div>';
		$footer_text .= '</div>';

		return $footer_text;
	}

	public function plugin_action_links($links)
	{

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

		return array_merge($custom_links, $links);
	}

	/**
	 * Declare that plugin is hpos compatible.
	 *
	 */
	public function hpos_compatible()
	{
		if (class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil')) {

			$plugin_file = plugin_dir_path(dirname(__FILE__)) . 'easy-booking-calendar.php';

			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', $plugin_file, true);
		}
	}
}
