<?php

/**
 * Plugin Notice Class.
 *
 * @package Kubelio\Admin
 * @since 1.0.0
 */

namespace Kubelio\Admin;

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

/**
 * Notice Class
 */
class Notice {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action('admin_notices', array($this, 'plugin_requirement_notice'));
		add_action('wp_ajax_kubelio_install_plugin', array($this, 'ajax_install_plugin'));
		add_action('wp_ajax_kubelio_activate_plugin', array($this, 'ajax_activate_plugin'));
		add_action('wp_ajax_kubelio_dismiss_notice', array($this, 'ajax_dismiss_notice'));
	}

	/**
	 * Display admin notice if Kubelio Companion is not active.
	 */
	public function plugin_requirement_notice() {
		// Only show for users who can install plugins.
		if (! current_user_can('install_plugins')) {
			return;
		}

		// Check if dismissed.
		if (get_theme_mod('kubelio_companion_notice_dismissed', false)) {
			return;
		}

		// Don't show on the TGMPA install page.
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin page routing check, no form processing.
		if (isset($_GET['page']) && 'tgmpa-install-plugins' === sanitize_key(wp_unslash($_GET['page']))) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			return;
		}

		// Required to use is_plugin_active() outside of admin context.
		include_once(ABSPATH . 'wp-admin/includes/plugin.php');

		$is_installed    = file_exists(WP_PLUGIN_DIR . '/kubelio-companion/kubelio-companion.php');

		// If the plugin is already active, we don't need to show the notice.
		if (is_plugin_active('kubelio-companion/kubelio-companion.php')) {
			return;
		}

		$tgmpa_url = admin_url('admin.php?page=tgmpa-install-plugins');


		if ($is_installed) {
			$plugin_action = 'activate-plugin';
		} else {
			$plugin_action = 'install-plugin';
		}

		$action_url = wp_nonce_url(
			add_query_arg(
				array(
					'plugin'         => urlencode('kubelio-companion'),
					'tgmpa-activate' => $plugin_action,
				),
				$tgmpa_url
			),
			'tgmpa-activate',
			'tgmpa-nonce'
		);

		get_template_part('inc/admin/views/plugin-notice', null, array(
			'is_installed'    => $is_installed,
			'action_url'      => $action_url,
		));
	}

	/**
	 * AJAX Handler for installing plugin
	 */
	public function ajax_install_plugin() {
		check_ajax_referer('kubelio_admin_nonce', 'nonce');

		if (! current_user_can('install_plugins')) {
			wp_send_json_error('No permission');
		}

		include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
		include_once ABSPATH . 'wp-admin/includes/plugin-install.php';

		$plugin_url = 'https://downloads.wordpress.org/plugin/kubelio-companion.zip';

		$skin     = new \WP_Ajax_Upgrader_Skin();
		$upgrader = new \Plugin_Upgrader($skin);
		$install  = $upgrader->install($plugin_url, array(
			'overwrite_package' => true,
		));

		if (is_wp_error($install)) {
			wp_send_json_error($install->get_error_message());
		}

		if (!$install) {
			wp_send_json_error('Installation failed');
		}

		// Activate after install
		$activate = activate_plugin('kubelio-companion/kubelio-companion.php');

		$redirect_url = get_theme_mod('demo_imported') ? admin_url('admin.php?page=kubelio-panel') : admin_url('admin.php?page=kubelio-setup');

		wp_send_json_success(array(
			'message'  => 'Plugin installed and activated',
			'redirect' => $redirect_url,
		));
	}

	/**
	 * AJAX Handler for activating plugin
	 */
	public function ajax_activate_plugin() {
		check_ajax_referer('kubelio_admin_nonce', 'nonce');

		if (! current_user_can('install_plugins')) {
			wp_send_json_error('No permission');
		}

		$plugin_file = 'kubelio-companion/kubelio-companion.php';
		$activate = activate_plugin($plugin_file);

		if (is_wp_error($activate)) {
			wp_send_json_error($activate->get_error_message());
		}

		$redirect_url = get_theme_mod('demo_imported') ? admin_url('admin.php?page=kubelio-panel') : admin_url('admin.php?page=kubelio-setup');

		wp_send_json_success(array(
			'message'  => 'Plugin activated',
			'redirect' => $redirect_url,
		));
	}

	/**
	 * AJAX Handler for dismissing notice
	 */
	public function ajax_dismiss_notice() {
		check_ajax_referer('kubelio_admin_nonce', 'nonce');

		if (! current_user_can('install_plugins')) {
			wp_send_json_error('No permission');
		}

		set_theme_mod('kubelio_companion_notice_dismissed', true);

		wp_send_json_success();
	}
}
