<?php

/**
 * Registers the top-level NudgeSells admin menu and its submenus, the shared branded page header, and the one-time activation welcome notice.
 *
 * @link       https://nudgesells.com
 * @since      1.0.0
 *
 * @package    NudgeSells
 * @subpackage NudgeSells/admin
 */

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

/**
 * NudgeSells_Admin
 *
 * Registers the NudgeSells admin menu structure and renders the shared header, screen fixes, and welcome notice used across its pages.
 *
 * @since      1.0.0
 * @package    NudgeSells
 * @subpackage NudgeSells/admin
 * @author     NudgeSells <support@nudgesells.com>
 */
class NudgeSells_Admin
{

	const SLUG            = 'nudgesells';
	const SLUG_ANALYTICS  = 'nudgesells-analytics';
	const SLUG_SETTINGS   = 'nudgesells-settings';
	const SLUG_TOOLS      = 'nudgesells-tools';
	const CAPABILITY      = 'manage_woocommerce';

	private $page_hooks = array();

	/**
	 * Render welcome notice.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function render_welcome_notice()
	{
		if ('yes' !== get_option('nudgesells_show_welcome_notice')) {
			return;
		}
		delete_option('nudgesells_show_welcome_notice');

		if (! current_user_can(self::CAPABILITY)) {
			return;
		}
		?>
		<div class="notice notice-success is-dismissible nudgesells-welcome-notice">
			<div class="nudgesells-welcome-notice-inner">
				<span class="nudgesells-logo"><span class="dashicons dashicons-megaphone"></span></span>
				<div class="nudgesells-welcome-notice-text">
					<p><strong><?php esc_html_e('Welcome to NudgeSells!', 'nudgesells'); ?></strong></p>
					<p><?php esc_html_e('Add inline upsells directly above the add to cart button. Here\'s where to start:', 'nudgesells'); ?></p>
					<p class="nudgesells-welcome-notice-actions">
						<a href="<?php echo esc_url(admin_url('post-new.php?post_type=' . NudgeSells_CPT::POST_TYPE)); ?>" class="button button-primary"><?php esc_html_e('Create your first offer', 'nudgesells'); ?></a>
						<a href="<?php echo esc_url(admin_url('admin.php?page=' . self::SLUG)); ?>" class="button"><?php esc_html_e('View the dashboard', 'nudgesells'); ?></a>
						<a href="<?php echo esc_url(admin_url('admin.php?page=' . self::SLUG_SETTINGS)); ?>" class="button"><?php esc_html_e('Go to settings', 'nudgesells'); ?></a>
					</p>
				</div>
			</div>
		</div>
	<?php
	}

	/**
	 * Register menu.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function register_menu()
	{
		$this->page_hooks[] = add_menu_page(
			__('NudgeSells', 'nudgesells'),
			__('NudgeSells', 'nudgesells'),
			self::CAPABILITY,
			self::SLUG,
			array(NudgeSells_Admin_Dashboard::class, 'render'),
			'dashicons-nudgesells',
			58
		);

		$this->page_hooks[] = add_submenu_page(
			self::SLUG,
			__('Dashboard', 'nudgesells'),
			__('Dashboard', 'nudgesells'),
			self::CAPABILITY,
			self::SLUG,
			array(NudgeSells_Admin_Dashboard::class, 'render')
		);

		add_submenu_page(
			self::SLUG,
			__('Offers', 'nudgesells'),
			__('Offers', 'nudgesells'),
			self::CAPABILITY,
			'edit.php?post_type=' . NudgeSells_CPT::POST_TYPE
		);

		$this->page_hooks[] = add_submenu_page(
			self::SLUG,
			__('Analytics', 'nudgesells'),
			__('Analytics', 'nudgesells'),
			self::CAPABILITY,
			self::SLUG_ANALYTICS,
			array(
				NudgeSells_Pro_Loader::is_active() ? NudgeSells_Pro_Admin_Analytics::class : NudgeSells_Admin_Analytics::class,
				'render',
			)
		);

		if (NudgeSells_Pro_Loader::is_active()) {
			add_submenu_page(
				self::SLUG,
				__('A/B Tests', 'nudgesells'),
				__('A/B Tests', 'nudgesells'),
				self::CAPABILITY,
				'edit.php?post_type=nudgesells_ab_test'
			);
		}

		$this->page_hooks[] = add_submenu_page(
			self::SLUG,
			__('Settings', 'nudgesells'),
			__('Settings', 'nudgesells'),
			self::CAPABILITY,
			self::SLUG_SETTINGS,
			array(NudgeSells_Admin_Settings::class, 'render')
		);

		if (NudgeSells_Pro_Loader::is_active()) {
			$this->page_hooks[] = add_submenu_page(
				self::SLUG,
				__('Tools', 'nudgesells'),
				__('Tools', 'nudgesells'),
				self::CAPABILITY,
				self::SLUG_TOOLS,
				array(NudgeSells_Pro_Admin_Tools::class, 'render')
			);
		}
	}

	/**
	 * Is nudgesells cpt screen.
	 *
	 * @param mixed $screen
	 *
	 * @return mixed
	 * @since 1.0.0
	 */
	private function is_nudgesells_cpt_screen($screen)
	{
		return $screen
			&& in_array($screen->post_type, array(NudgeSells_CPT::POST_TYPE, 'nudgesells_ab_test'), true)
			&& in_array($screen->base, array('edit', 'post'), true);
	}

	/**
	 * Is our screen.
	 *
	 * @param mixed $hook
	 *
	 * @return mixed
	 * @since 1.0.0
	 */
	private function is_our_screen($hook)
	{
		if (in_array($hook, $this->page_hooks, true)) {
			return true;
		}
		return $this->is_nudgesells_cpt_screen(get_current_screen());
	}

	/**
	 * Enqueue assets.
	 *
	 * @param mixed $hook
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function enqueue_assets($hook)
	{
		$show_welcome_notice = 'yes' === get_option('nudgesells_show_welcome_notice');

		wp_enqueue_style('nudgesells-font', NUDGESELLS_PLUGIN_URL . 'admin/css/nudgesells-font.css', array(), NUDGESELLS_VERSION);
		if (! $this->is_our_screen($hook) && ! $show_welcome_notice) {
			return;
		}
		wp_enqueue_style('nudgesells-admin', NUDGESELLS_PLUGIN_URL . 'admin/css/nudgesells-admin.css', array(), NUDGESELLS_VERSION);

		if (! $this->is_our_screen($hook)) {
			return;
		}

		$is_settings_page = isset($_GET['page']) && self::SLUG_SETTINGS === $_GET['page'];
		if ($is_settings_page) {
			wp_enqueue_style('wp-color-picker');
		}

		$is_analytics_page = isset($_GET['page']) && self::SLUG_ANALYTICS === $_GET['page'];
		if ($is_analytics_page) {
			wp_enqueue_script('jquery-ui-datepicker');
			wp_enqueue_script('nudgesells-chartjs', NUDGESELLS_PLUGIN_URL . 'admin/js/vendor/chart.umd.min.js', array(), '4.4.0', true);
			wp_enqueue_script('nudgesells-admin-chart', NUDGESELLS_PLUGIN_URL . 'admin/js/nudgesells-admin-chart.js', array('nudgesells-chartjs'), NUDGESELLS_VERSION, true);
		}

		if (in_array($hook, $this->page_hooks, true)) {
			$deps = array('jquery');
			if ($is_settings_page) {
				$deps[] = 'wp-color-picker';
			}
			if ($is_analytics_page) {
				$deps[] = 'jquery-ui-datepicker';
			}
			wp_enqueue_script('nudgesells-admin', NUDGESELLS_PLUGIN_URL . 'admin/js/nudgesells-admin.js', $deps, NUDGESELLS_VERSION, true);
		}
	}

	/**
	 * Render header.
	 *
	 * @param mixed $title
	 * @param string $actions_html
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public static function render_header($title, $actions_html = '')
	{
	?>
		<div class="nudgesells-header">
			<div class="nudgesells-header-top">
				<div class="nudgesells-header-title">
					<img src="<?php echo esc_url( NUDGESELLS_PLUGIN_URL . '/admin/img/nudgesells-logo.svg' ); ?>" width="140" height="23" alt="<?php esc_html_e( 'NudgeSells', 'nudgesells' ); ?>">
					<?php if ($actions_html) : ?>
						<div class="nudgesells-header-actions"><?php echo wp_kses_post($actions_html); ?></div>
					<?php endif; ?>
				</div>
			</div>
			<h1>
				<?php echo esc_html($title); ?>
			</h1>
		</div>
		<?php
	}

	/**
	 * Render cpt screen header.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function render_cpt_screen_header()
	{
		$screen = get_current_screen();
		if (! $this->is_nudgesells_cpt_screen($screen)) {
			return;
		}

		$is_experiment = 'nudgesells_ab_test' === $screen->post_type;
		$title         = $is_experiment ? esc_html__('A/B Tests', 'nudgesells') : esc_html__('Offers', 'nudgesells');
		$list_url      = admin_url('edit.php?post_type=' . $screen->post_type);

		$actions = '';
		if ('post' === $screen->base) {
			$actions = '<a href="' . esc_url($list_url) . '" class="button">&larr; ' . esc_html(sprintf(
				esc_html__('Back to %s', 'nudgesells'),
				$title
			)) . '</a>';
		}

		echo '<div class="wrap nudgesells-wrap nudgesells-cpt-screen-header">';
		self::render_header($title, $actions);
		echo '</div>';
	}

	/**
	 * Fix parent file.
	 *
	 * @param mixed $parent_file
	 *
	 * @return mixed
	 * @since 1.0.0
	 */
	public function fix_parent_file($parent_file)
	{
		$screen = get_current_screen();
		if ($this->is_nudgesells_cpt_screen($screen)) {
			return self::SLUG;
		}
		return $parent_file;
	}

	/**
	 * Fix submenu file.
	 *
	 * @param mixed $submenu_file
	 *
	 * @return mixed
	 * @since 1.0.0
	 */
	public function fix_submenu_file($submenu_file)
	{
		$screen = get_current_screen();
		if ($this->is_nudgesells_cpt_screen($screen)) {
			return 'edit.php?post_type=' . $screen->post_type;
		}
		return $submenu_file;
	}

	/**
	 * Add helpful links to plugin listing item row meta
	 * 
	 * @since   1.0.0
	 */
	public function plugin_listing_links($links_array, $plugin_file_name, $plugin_data, $status)
	{
		if (strpos($plugin_file_name, 'nudgesells.php') !== false) {
			/* translators: %s: Link text */
			$links_array[] = wp_kses_post( sprintf( '<a href="https://nudgesells.com/docs/" target="_blank">%s</a>', __( 'Documentation', 'nudgesells' ) ) );

			if (NudgeSells_Pro_Loader::is_active()) {
				/* translators: %s: Link text */
				$links_array[] = wp_kses_post( sprintf( '<a href="https://nudgesells.com/docs/#support" target="_blank">%s</a>', __( 'Support', 'nudgesells' ) ) );
			} else {
				/* translators: %s: Link text */
				$links_array[] = wp_kses_post( sprintf( '<a href="https://nudgesells.com/pricing/" target="_blank">%s</a>', __( 'Upgrade', 'nudgesells' ) ) );
			}
		}
		return $links_array;
	}

	/**
	 * Customize the footer copy for the admin area.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function edit_admin_footer_text() {
		$screen = get_current_screen();
		if ( !$this->is_our_screen($screen->base) ) {
			return;
		}

		echo sprintf(
			/* translators: 1: Plugin site link. 2: Wordpress.org plugin link */
			esc_html__( 'Thank you for creating with %1$s. Please rate %2$s to help spread the word!', 'nudgesells'),
			'<a href="https://nudgesells.com" target="_blank">NudgeSells</a>',
			'<a href="https://wordpress.org/plugins/nudgesells/" target="_blank">★★★★★</a>'
		);
	}

	/**
	 * Short description.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function edit_admin_footer_version() {
		$screen = get_current_screen();
		if ( !$this->is_our_screen($screen->base) ) {
			return;
		}
		
		echo esc_html__( 'NudgeSells', 'nudgesells') . ' &bullet; ' . esc_attr( NUDGESELLS_VERSION );
	}
}
