<?php

namespace WPSS\feedback;

// Exit if accessed directly.
if (!defined('ABSPATH')) {
	exit;
}

class WPSSLC_UsersFeedback
{

	private $plugin_url = WPSSLC_URL;
	private $plugin_version = WPSSLC_VERSION;
	private $plugin_name = 'WPSyncSheets Lite For Contact Form 7';
	private $plugin_slug = 'wpssc';
	private $plugin_download_id = WPSSLC_PLUGIN_ITEM_ID;

	public function __construct()
	{
		add_action('admin_enqueue_scripts', array($this, 'enqueue_feedback_scripts'));
		add_action('wp_ajax_' . $this->plugin_slug . '_submit_deactivation_response', array($this, 'submit_deactivation_response'));
		add_action('admin_init', array($this, 'on_init'));
	}

	public function on_init()
	{
		add_action('admin_head', array($this, 'show_deactivate_feedback_popup'));
	}

	/*
	 |-----------------------------------------------------------------|
	 |   Enqueue all scripts and styles to required page only          |
	 |-----------------------------------------------------------------|
	 */
	public function enqueue_feedback_scripts()
	{
		$screen = get_current_screen();
		if (isset($screen) && 'plugins' === $screen->id) {
			wp_enqueue_script(
				esc_attr($this->plugin_slug . '-feedback-script'),
				esc_url($this->plugin_url . 'feedback/js/admin-feedback.js'),
				array('jquery'),
				esc_attr($this->plugin_version),
				true
			);
			wp_enqueue_style(
				esc_attr($this->plugin_slug . '-feedback-style'),
				esc_url($this->plugin_url . 'feedback/css/admin-feedback.css'),
				null,
				esc_attr($this->plugin_version)
			);
		}
	}

	/*
	 |-----------------------------------------------------------------|
	 |   HTML for creating feedback popup form                         |
	 |-----------------------------------------------------------------|
	 */
	public function show_deactivate_feedback_popup()
	{

		$screen = get_current_screen();
		if (!isset($screen) || 'plugins' !== $screen->id) {
			return;
		}

		$deactivate_reasons = self::get_deactivate_reasons_arr();
?>
		<div id="<?php echo esc_attr($this->plugin_slug); ?>-deactivate-feedback-dialog-wrapper" class="hide-feedback-popup">
		  	<div class="<?php echo esc_attr($this->plugin_slug); ?>-deactivation-container">
				<div class="<?php echo esc_attr($this->plugin_slug); ?>-deactivation-response">
					<div id="<?php echo esc_attr($this->plugin_slug); ?>-deactivate-feedback-dialog-header">
						<div class="<?php echo esc_attr($this->plugin_slug); ?>-form-title--icon-wrapper">
							<img class="<?php echo esc_attr($this->plugin_slug); ?>-icon"
								src="<?php echo esc_url(plugins_url('../feedback/images/deactive-header-logo.png', __FILE__)); ?>"
								alt="<?php echo esc_attr__('Header Logo', 'contactsheets-lite'); ?>">
							<h2 class="<?php echo esc_attr($this->plugin_slug); ?>-title"><?php echo esc_html__('Quick Feedback', 'contactsheets-lite'); ?></h2>
						</div>
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="<?php echo esc_attr($this->plugin_slug); ?>-deactivate-feedback-dialog-close">
							<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12"></path>
						</svg>
					</div>
					<div id="<?php echo esc_attr($this->plugin_slug); ?>-loader-wrapper">
						<div class="<?php echo esc_attr($this->plugin_slug); ?>-loader-container">
							<img class="<?php echo esc_attr($this->plugin_slug); ?>-preloader" src="<?php echo esc_url($this->plugin_url . 'feedback/images/preloader.gif'); ?>">
						</div>
					</div>
					<div id="<?php echo esc_attr($this->plugin_slug); ?>-form-wrapper" class="<?php echo esc_attr($this->plugin_slug); ?>-form-wrapper-cls">
						<form id="<?php echo esc_attr($this->plugin_slug); ?>-deactivate-feedback-dialog-form" method="post">
							<?php wp_nonce_field('wpss_deactivate_feedback_nonce', esc_attr($this->plugin_slug . '-wpnonce')); ?>
							<input type="hidden" name="action" value="wpss_deactivate_feedback" />
							<div id="<?php echo esc_attr($this->plugin_slug); ?>-deactivate-feedback-dialog-form-caption"><?php echo esc_html__('If you have a moment, please share why you are deactivating this plugin.', 'contactsheets-lite'); ?></div>
							<div id="<?php echo esc_attr($this->plugin_slug); ?>-deactivate-feedback-dialog-form-body">
								<?php
		$reason_key_arr = array('didnt_work_as_expected', 'found_a_better_plugin', 'couldnt_get_the_plugin_to_work');
		foreach ($deactivate_reasons as $reason_key => $reason):
?>
									<div class="<?php echo esc_attr($this->plugin_slug); ?>-deactivate-feedback-dialog-input-wrapper">
										<input id="<?php echo esc_attr($this->plugin_slug . '-deactivate-feedback-' . $reason_key); ?>"
											class="<?php echo esc_attr($this->plugin_slug); ?>-deactivate-feedback-dialog-input"
											type="radio" name="reason_key" value="<?php echo esc_attr($reason_key); ?>" />
										<label for="<?php echo esc_attr($this->plugin_slug . '-deactivate-feedback-' . $reason_key); ?>" class="<?php echo esc_attr($this->plugin_slug); ?>-deactivate-feedback-dialog-label"><?php echo esc_html($reason['title']); ?></label>
										<?php if (!empty($reason['input_placeholder'])): ?>
											<textarea class="<?php echo esc_attr($this->plugin_slug); ?>-feedback-text"
												name="reason_<?php echo esc_attr($reason_key); ?>"
												placeholder="<?php echo esc_attr($reason['input_placeholder']); ?>"></textarea>
											<?php
				if (in_array($reason_key, $reason_key_arr, true)) {
					$twae_plugin_url = esc_url('https://wordpress.org/plugins/contactsheets-lite/');
				}
			endif;
?>
										<?php if (!empty($reason['alert'])): ?>
											<div class="<?php echo esc_attr($this->plugin_slug); ?>-feedback-text"><?php echo esc_html($reason['alert']); ?></div>
										<?php
			endif; ?>
									</div>
								<?php
		endforeach; ?>
								<input class="<?php echo esc_attr($this->plugin_slug); ?>-GDPR-data-notice"
									id="<?php echo esc_attr($this->plugin_slug); ?>-GDPR-data-notice"
									type="checkbox">
								<label for="<?php echo esc_attr($this->plugin_slug); ?>-GDPR-data-notice"><?php echo esc_html__('I consent to WPSyncSheets plugins collecting all the information I submit through this form and using it to respond to my inquiry.', 'contactsheets-lite'); ?></label>
								<div class="<?php echo esc_attr($this->plugin_slug); ?>-uninstall-feedback-privacy-policy">
									<?php echo esc_html__('We value your feedback and do not collect personal data.', 'contactsheets-lite'); ?> <a href="<?php echo esc_url('https://www.wpsyncsheets.com/privacy-policy/'); ?>" target="_blank"><?php echo esc_html__('Privacy Policy', 'contactsheets-lite'); ?></a>
								</div>
								<div class="<?php echo esc_attr($this->plugin_slug); ?>-required-field-messages"></div>
							</div>
							<div class="<?php echo esc_attr($this->plugin_slug); ?>-plugin-popup-button-wrapper">
								<a class="<?php echo esc_attr($this->plugin_slug); ?>-button" id="<?php echo esc_attr($this->plugin_slug); ?>-plugin-submitNdeactivate"><?php echo esc_html__('Submit and Deactivate', 'contactsheets-lite'); ?></a>
								<a class="<?php echo esc_attr($this->plugin_slug); ?>-button" id="<?php echo esc_attr($this->plugin_slug); ?>-plugin-skipNdeactivate"><?php echo esc_html__('Skip and Deactivate', 'contactsheets-lite'); ?></a>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
		<?php
	}

	public function submit_deactivation_response()
	{
		if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'wpss_deactivate_feedback_nonce')) {
			wp_send_json_error();
		}
		else {
			$reason = isset($_POST['reason']) ? sanitize_text_field(wp_unslash($_POST['reason'])) : '';
			$message = isset($_POST['message']) ? sanitize_textarea_field(wp_unslash($_POST['message'])) : '';
			$deactivate_reasons = self::get_deactivate_reasons_arr();
			$deativation_reason = array_key_exists($reason, $deactivate_reasons) ? $reason : 'other';
			$sanitized_message = $message ? $message : 'N/A';
			$admin_email = sanitize_email(get_option('admin_email'));
			$site_url = esc_url(site_url());
			$feedback_url = esc_url('https://feedback.wpsyncsheets.com/wp-json/wpssplugins-feedback/v1/feedback');

			$response = wp_remote_post(
				$feedback_url,
				array(
				'timeout' => 30,
				'body' => array(
					'plugin_version' => esc_html($this->plugin_version),
					'plugin_name' => esc_html($this->plugin_name),
					'plugin_download_id' => esc_html($this->plugin_download_id),
					'plugin_type' => 'lite',
					'plugin_slug' => 'wpsslc',
					'reason' => esc_html($deativation_reason),
					'review' => esc_html($sanitized_message),
					'email' => esc_html($admin_email),
					'domain' => esc_url($site_url),
				),
			)
			);

			wp_send_json_success(array('response' => $response));
		}
	}

	public function get_deactivate_reasons_arr()
	{
		$deactivate_reasons = array(
			'didnt_work_as_expected' => array(
				'title' => esc_html__('The plugin didn\'t work as expected', 'contactsheets-lite'),
				'input_placeholder' => esc_html__('What did you expect?', 'contactsheets-lite'),
			),
			'found_a_better_plugin' => array(
				'title' => esc_html__('I found a better plugin', 'contactsheets-lite'),
				'input_placeholder' => esc_html__('Please share which plugin', 'contactsheets-lite'),
			),
			'couldnt_get_the_plugin_to_work' => array(
				'title' => esc_html__('The plugin is not working', 'contactsheets-lite'),
				'input_placeholder' => esc_html__('Please share your issue. So we can fix that for other users.', 'contactsheets-lite'),
			),
			'temporary_deactivation' => array(
				'title' => esc_html__('It\'s a temporary deactivation', 'contactsheets-lite'),
				'input_placeholder' => '',
			),
			'other' => array(
				'title' => esc_html__('Other', 'contactsheets-lite'),
				'input_placeholder' => esc_html__('Please share the reason', 'contactsheets-lite'),
			),
		);
		return $deactivate_reasons;
	}
}

new WPSSLC_UsersFeedback();