<?php

namespace PixelioCartAbandonRecoveryMore\Admin\Services;

use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Email_Templates_Repository;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

class Pixelio_Cart_Abandon_Recovery_More_Enqueuer_Service
{

	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $pixelio_wordpress_wrapper;
	private Pixelio_Cart_Abandon_Recovery_More_Util $pixelio_util;

	public function __construct($pixelio_util, $pixelio_wordpress_wrapper)
	{
		$this->pixelio_wordpress_wrapper = $pixelio_wordpress_wrapper;
		$this->pixelio_util = $pixelio_util;
	}

	/**
	 * Common styles for all admin pages
	 */
	public function enqueue_common_styles(): void
	{
		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_style(
			Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . 'update-style',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('../assets/css/components/pixelio_cart_abandon_recovery_more_update_popup.css', __FILE__),
			[],
			'0.0.1'
		);
	}

	public function enqueue_header_script(): void
	{
		$handle = Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . 'header-script';
		$src = $this->pixelio_wordpress_wrapper->pixelio_plugins_url('/../assets/js/pages/dist/header.bundle.js', __FILE__);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script($handle, $src, [], '0.0.1', true);

		$api_key = $this->pixelio_util->pixelio_cart_abandon_recovery_more_decrypt_api_key('pixelio_api_key');
		$base_url = $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url();
		$locale = $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_locale();
		$lang = substr($locale, 0, 2);
		$pixelio_vars = [
			'api_key' => $this->pixelio_wordpress_wrapper->pixelio_esc_attr($api_key),
			'api_is_available' => $this->pixelio_wordpress_wrapper->pixelio_esc_url($base_url . 'is_beta_test_signup_available'),
			'api_signup' => $this->pixelio_wordpress_wrapper->pixelio_esc_url($base_url . 'sign_up_beta_test'),
			'api_quota' => $this->pixelio_wordpress_wrapper->pixelio_esc_url($base_url . 'my-quota'),
			'msg_join_beta' => $this->pixelio_wordpress_wrapper->pixelio_esc_html('You must agree to join the beta to proceed.'),
			'msg_select_comm' => $this->pixelio_wordpress_wrapper->pixelio_esc_html('Please select your preferred communication method.'),
			'msg_email_required' => $this->pixelio_wordpress_wrapper->pixelio_esc_html('Please enter your email.'),
			'msg_signing_up' => $this->pixelio_wordpress_wrapper->pixelio_esc_html('Signing up...'),
			'msg_success' => $this->pixelio_wordpress_wrapper->pixelio_esc_html('Thank you for subscribing to our beta program, your request is pending verification. Please check your email.'),
			'msg_error' => $this->pixelio_wordpress_wrapper->pixelio_esc_html('Error signing up. Please try again.'),
			'msg_server_error' => $this->pixelio_wordpress_wrapper->pixelio_esc_html('Error connecting to the server.'),
			'ajaxError' => $this->pixelio_wordpress_wrapper->pixelio_esc_html('AJAX request failed'),
			'nonce' => $this->pixelio_wordpress_wrapper->pixelio_wp_create_nonce('pixelio_verify_domain_nonce'),
			'language' => $lang,
		];

		$this->pixelio_wordpress_wrapper->pixelio_wp_localize_script($handle, 'pixelio_vars', $pixelio_vars);
	}


	public function enqueue_dashboard_scripts(): void
	{
		// Styles
		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_style(
			Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . 'dashboard-style',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('../assets/css/pages/pixelio_cart_abandon_recovery_more_admin_dashboard.css', __FILE__),
			[],
			'0.0.1'
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_style(
			Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . 'data-email',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('../assets/css/libs/datatables.css', __FILE__),
			[],
			'0.0.1'
		);

		// Scripts
		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . '-chart-script',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('../assets/js/libs/chart.js', __FILE__),
			[],
			'0.0.1',
			true
		);
		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . '-flatpickr-script',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('../assets/js/libs/flatpickr.js', __FILE__),
			[],
			'0.0.1',
			true
		);
		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . 'data-script',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('/../assets/js/libs/datatables.js', __FILE__),
			[Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . 'multi-tab-script', Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . 'remove-popup-script'],
			'0.0.1',
			true
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . '-dashboard-script',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('../assets/js/pages/dist/dashboard.bundle.js', __FILE__),
			[
				Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . '-chart-script',
				Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . 'data-script',
				Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . '-flatpickr-script'
			],
			'0.0.1',
			true
		);

		// Localize
		$this->pixelio_wordpress_wrapper->pixelio_wp_localize_script(
			Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . '-dashboard-script',
			'pixelio_ajax',
			[
				'apiKey' => $this->pixelio_wordpress_wrapper->pixelio_esc_attr(
					$this->pixelio_util->pixelio_cart_abandon_recovery_more_decrypt_api_key('pixelio_api_key')
				),
				'noDataText' => $this->pixelio_wordpress_wrapper->pixelio_esc_html('Hang tight! Your data will appear soon.'),
				'kpiGlobalUrl' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url() . 'kpi_global',
				'kpiByEmailUrl' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url() . 'kpi_by_email',
				'isIa' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked("ia-setup-mode"),
				'kpiByProductUrl' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url() . 'kpi_by_product',
				'kpiAbandonedCartsUrl' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url() . 'kpi_abandoned_carts',
				'lang' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_locale()
			]
		);
	}

	/**
	 * Integrations page scripts
	 */
	public function enqueue_integrations_scripts(): void
	{
		$slug = Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug;

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_style(
			$slug . 'email-style',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('../assets/css/pages/pixelio_cart_abandon_recovery_more_admin_integration.css', __FILE__),
			[],
			'0.0.1'
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			$slug . '-integrations-script',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('/../assets/js/pages/dist/integration.bundle.js', __FILE__),
			[],
			'0.0.1',
			true
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_localize_script(
			$slug . '-integrations-script',
			'pixelioVoteData',
			[
				'vote_endpoint' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url() . 'vote-for-integration',
				'user_id' => $this->pixelio_wordpress_wrapper->pixelio_get_current_user_id(),
				'api_key' => $this->pixelio_wordpress_wrapper->pixelio_esc_attr($this->pixelio_util->pixelio_cart_abandon_recovery_more_decrypt_api_key('pixelio_api_key')),
				'alert_choose_integration' => __('Please choose an integration before voting', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'alert_success' => __('Thank you for your vote.', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'alert_error' => __('An error happened. Try again later.', 'rivio-ai-cart-recovery-marketing-email-automation'),
			]
		);
	}

	/**
	 * FAQ page scripts
	 */
	public function enqueue_faq_scripts(): void
	{
		$slug = Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug;

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_style(
			$slug . 'faq-style',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('../assets/css/pages/pixelio_cart_abandon_recovery_more_admin_faq.css', __FILE__),
			[],
			'0.0.1'
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			$slug . '-faq-script',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('/../assets/js/pages/pixelio_cart_abandon_recovery_more_admin_faq.js', __FILE__),
			[],
			'0.0.1',
			true
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_localize_script(
			$slug . '-faq-script',
			'pixelioFaqData',
			[
				'locale' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_locale(),
				'apiKey' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_decrypt_api_key('pixelio_api_key'),
				'apiUrl' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url(),
				'errorLoadingFaq' => $this->pixelio_wordpress_wrapper->pixelio_translate('Error loading FAQ', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'loadingFaq' => $this->pixelio_wordpress_wrapper->pixelio_translate('Loading...', 'rivio-ai-cart-recovery-marketing-email-automation'),
			]
		);
	}

	/**
	 * Settings page scripts
	 */
	public function enqueue_settings_scripts(): void
	{
		$slug = Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug;

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_style(
			$slug . 'settings-style',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('../assets/css/pages/pixelio_cart_abandon_recovery_more_admin_settings.css', __FILE__),
			[],
			'0.0.1'
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			$slug . '-script',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('/../assets/js/pages/dist/settings.bundle.js', __FILE__),
			[],
			'0.0.1',
			true
		);

		$locale = $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_locale();
		$lang = substr($locale, 0, 2);

		$this->pixelio_wordpress_wrapper->pixelio_wp_localize_script(
			$slug . '-script',
			'pixelioData',
			[
				'apiKey' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_decrypt_api_key('pixelio_api_key'),
				'apiUrl' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url(),
				'language' => $lang,
			]
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_localize_script(
			$slug . '-script',
			'pixelio_ajax',
			[
				'exit_popup' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked('pixelio-cart-abandon-recovery-more-exit-popup-checkbox'),
			]
		);
	}

	/**
	 * First configuration page scripts
	 */
	public function enqueue_first_config_scripts(): void
	{
		$slug = Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug;
		$has_api_key = (bool)$this->pixelio_wordpress_wrapper->pixelio_esc_html($this->pixelio_wordpress_wrapper->pixelio_get_option('pixelio_api_key'));
		$locale = $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_locale();
		$lang = substr($locale, 0, 2);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_style(
			$slug . 'first-style',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('../assets/css/pages/pixelio_cart_abandon_recovery_more_admin_first_config.css', __FILE__),
			[],
			'0.0.1'
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			$slug . 'first-script',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('/../assets/js/pages/dist/first.bundle.js', __FILE__),
			[],
			'0.0.1',
			true
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_localize_script(
			$slug . 'first-script',
			'pixelioData',
			[
				'apiKey' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_decrypt_api_key('pixelio_api_key'),
				'ajaxUrl' => $this->pixelio_wordpress_wrapper->pixelio_admin_url('admin-ajax.php'),
				'nonce' => $this->pixelio_wordpress_wrapper->pixelio_wp_create_nonce('pixelio_verify_domain_nonce'),
				'apiUrl' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url(),
				'has_api_key' => $has_api_key,
				'selectShopType' => __('Please select your shop type.', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'selectOptionEachStep' => __('Please select at least one option in each step.', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'acceptTermOfUse' => __('Please accept the term of use first.', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'serverError' => __('Server error. Please try again.', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'wpServerError' => __('WordPress server error. Please try again.', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'invalidConfig' => __('The received configuration is invalid.', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'verifyingDomain' => __('Verifying domain...', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'enterDomain' => __('Please enter a domain name.', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'unexpectedError' => __('An unexpected error occurred.', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'language' => $lang,
			]
		);
	}

	/**
	 * Email creator page scripts
	 */
	public function enqueue_email_scripts(): void
	{
		$slug = Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug;
		$response = $this->pixelio_util->pixelio_cart_abandon_recovery_more_call_api(
			$this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url() . 'email-templates'
		);

		$templates = [];
		if (!$this->pixelio_wordpress_wrapper->pixelio_is_wp_error($response) && isset($response['data'])) {
			$templates = $response['data'];
		} else {
			$model = new Pixelio_Cart_Abandon_Recovery_More_Email_Templates_Repository($this->pixelio_wordpress_wrapper);
			$templates = array_merge(
				$model->pixelio_get_all_non_deleted_emails_templates(1),
				$model->pixelio_get_all_non_deleted_emails_templates(2)
			);
		}

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_style(
			$slug . 'quill-style',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('/../assets/vendor/quill/css/quill.css', __FILE__),
			[],
			'0.0.1'
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_style(
			$slug . 'email-style',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('/../assets/css/pages/pixelio_cart_abandon_recovery_more_admin_email_creator.css', __FILE__),
			[],
			'0.0.1'
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			$slug . 'quill-script',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('/../assets/vendor/quill/js/quill.js', __FILE__),
			[],
			'0.0.1',
			true
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			$slug . 'multi-tab-script',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('/../assets/js/components/pixelio_cart_abandon_recovery_more_multi_tab_switcher.js', __FILE__),
			[],
			'0.0.1',
			true
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			$slug . 'remove-popup-script',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('/../assets/js/components/pixelio_cart_abandon_recovery_more_admin_removal_popup.js', __FILE__),
			[],
			'0.0.1',
			true
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			$slug . 'data-script',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('/../assets/js/libs/datatables.js', __FILE__),
			[$slug . 'multi-tab-script', $slug . 'remove-popup-script'],
			'0.0.1',
			true
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			$slug . '-email-script',
			$this->pixelio_wordpress_wrapper->pixelio_plugins_url('/../assets/js/pages/dist/email.bundle.js', __FILE__),
			[$slug . 'data-script',$slug . 'quill-script',],
			'0.0.1',
			true
		);

		$locale = $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_locale();
		$lang = substr($locale, 0, 2);

		$this->pixelio_wordpress_wrapper->pixelio_wp_localize_script(
			$slug . '-email-script',
			'pixelioData',
			[
				'no_templates_text' => esc_html__('No templates found', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'activate' => esc_html__('Please request first your api key', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'placeholder_error' => esc_html__('Please select a product!', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'email_sent_success' => esc_html__('Email sent successfully!', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'email_sent_error' => esc_html__('Error sending the email.', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'selectProduct' => esc_html__('Please select a product!', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'enableCoupon' => esc_html__('Please don\'t forget to enable the coupon option below!', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'emailSent' => esc_html__('Email sent successfully!', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'emailError' => esc_html__('Error:', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'ajaxError' => esc_html__('AJAX error:', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'enterEmail' => esc_html__('Please enter your email.', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'acceptGDPR' => esc_html__('Please accept the GDPR terms.', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'thankYouSubscription' => esc_html__('Thank you for subscribing!', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'saveTemplateError' => esc_html__('Error saving template.', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'activateTemplateError' => esc_html__('Error while validating templates. Retry later', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'saveEmailTemplate' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url() . "save_email_template",
				'validateEmailTemplate' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url() . "validate_ia_email",
				'replaceByYourText' => esc_html__('Your text', 'rivio-ai-cart-recovery-marketing-email-automation'),
				'apiKey' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_decrypt_api_key('pixelio_api_key'),
				'apiUrl' => $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url(),
				'templates' => $templates,
				'ajaxurl' => $this->pixelio_wordpress_wrapper->pixelio_admin_url('admin-ajax.php'),
				'nonce' => $this->pixelio_wordpress_wrapper->pixelio_wp_create_nonce('pixelio_toggle_nonce'),
				'language' => $lang,
			]
		);
	}

    public function enqueue_rgpd_scripts()
    {
		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_script(
			'pixelio-rgpd-js',
			plugins_url('../assets/js/pages/dist/rgpd.bundle.js', __FILE__),
			['jquery'],
			'1.0.0',
			true
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_enqueue_style(
			'pixelio-rgpd-css',
			plugins_url('../assets/css/pages/pixelio_cart_abandon_recovery_more_admin_rgpd.css', __FILE__),
			[],
			'1.0.0'
		);

		$this->pixelio_wordpress_wrapper->pixelio_wp_localize_script(
			'pixelio-rgpd-js',
			'pixelioRgpd',
			[
				'ajaxUrl' => admin_url('admin-ajax.php'),
				'nonce'   => wp_create_nonce('pixelio_rgpd_nonce'),
			]
		);
    }

}
