<?php
namespace Svea_Webpay_For_Woocommerce;

use Svea_Webpay_For_Woocommerce\Gateways\WC_Gateway_Svea_Invoice;
use Svea_Webpay_For_Woocommerce\Gateways\WC_Gateway_Svea_Part_Pay;

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

/**
 * Class to handle script inclusions
 */
class Scripts {

	/**
	 * Initialize the class
	 *
	 * @return void
	 */
	public function init() {
		add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
		add_action( 'wp_enqueue_scripts', [ $this, 'checkout_enqueue_scripts' ] );
	}

	/**
	 * Register and enqueue stylesheets and javascripts for backend use
	 *
	 * @return void
	 */
	public function admin_enqueue_scripts() {
		/**
		 * Link to the font awesome stylesheet for icons
		 */
		wp_enqueue_style( 'font-awesome-regular', WC_SVEAWEBPAY_GATEWAY_PLUGIN_URL . 'assets/fonts/fontawesome/css/regular.min.css', [], '5.14.0' );

		wp_enqueue_style( 'sveawebpay-backend-css', WC_SVEAWEBPAY_GATEWAY_PLUGIN_URL . 'assets/css/backend.min.css', [], Plugin::VERSION );
		wp_enqueue_script( 'sveawebpay-backend-js', WC_SVEAWEBPAY_GATEWAY_PLUGIN_URL . 'assets/js/backend.min.js', [ 'jquery' ], Plugin::VERSION, true );

		global $woocommerce;

		$countries = $woocommerce->countries->get_allowed_countries();

		$only_one_allowed_country = false;

		if ( count( $countries ) <= 1 ) {
			$only_one_allowed_country = array_keys( $countries )[0];
		}

		$svea_data['gaSecurity'] = wp_create_nonce( Ajax_Functions::GET_ADDRESS_NONCE_NAME );
		$svea_data['ajaxUrl'] = admin_url( 'admin-ajax.php' );
		$svea_data['onlyOneAllowedCountry'] = ( $only_one_allowed_country ? $only_one_allowed_country : false );

		/**
		 * Localize the javascript with Svea data
		 */
		wp_localize_script( 'sveawebpay-backend-js', 'Svea', $svea_data );

		$phrases = [
			// translators: %d is the number of items to be credited
			'confirm_credit_items'           => __( 'Are you sure you want to credit %d items?', 'svea-webpay-for-woocommerce' ),
			// translators: %d is the number of items to be delivered
			'confirm_deliver_items'          => __( 'Are you sure you want to deliver %d items?', 'svea-webpay-for-woocommerce' ),
			'not_selected_any_items'         => __( 'You have not selected any items yet.', 'svea-webpay-for-woocommerce' ),
			'no_payment_plans_country_total' => __( 'There are no available payment plans for this country and order total.', 'svea-webpay-for-woocommerce' ),
			'your_address_was_found'         => __( 'Your address was found.', 'svea-webpay-for-woocommerce' ),
			'part_payment_plans'             => __( 'Part Payment Plans', 'svea-webpay-for-woocommerce' ),
			'company_name'                   => __( 'Company Name', 'svea-webpay-for-woocommerce' ),
			'invoice_fee'                    => __( 'Invoice fee', 'svea-webpay-for-woocommerce' ),
			'includes'                       => __( 'Includes', 'svea-webpay-for-woocommerce' ),
			'vat'                            => __( 'VAT', 'svea-webpay-for-woocommerce' ),
			'could_not_get_address'          => __( 'An error occurred whilst getting your address. Please try again later.', 'svea-webpay-for-woocommerce' ),
		];

		/**
		 * Localize the javascript with translated phrases
		 */
		wp_localize_script( 'sveawebpay-backend-js', 'Phrases', $phrases );
	}

	/**
	 * Register and enqueue stylesheets and javascripts
	 *
	 * @return  void
	 */
	public function checkout_enqueue_scripts() {
		/**
		 * Only enqueue scripts and styles in the checkout page
		 */
		if (
			( ! function_exists( 'is_checkout' ) || ! is_checkout() ) &&
			( ! function_exists( 'is_checkout_pay_page' ) || ! is_checkout_pay_page() ) &&
			( ! function_exists( 'is_product' ) || ! is_product() )
		) {
			return;
		}

		/**
		 * Link to the font awesome stylesheet for icons
		 */
		wp_enqueue_style( 'font-awesome-regular', WC_SVEAWEBPAY_GATEWAY_PLUGIN_URL . 'assets/fonts/fontawesome/css/regular.min.css', [], '5.14.0' );

		/**
		 * Enqueue styles and javascript, cache-bust using versioning
		 */
		wp_enqueue_style( 'sveawebpay-styles', WC_SVEAWEBPAY_GATEWAY_PLUGIN_URL . 'assets/css/frontend.min.css', [], Plugin::VERSION );
		wp_enqueue_script( 'sveawebpay-js', WC_SVEAWEBPAY_GATEWAY_PLUGIN_URL . 'assets/js/frontend.min.js', [ 'jquery' ], Plugin::VERSION, true );

		global $woocommerce;

		$countries = $woocommerce->countries->get_allowed_countries();

		$only_one_allowed_country = false;

		if ( count( $countries ) <= 1 ) {
			$only_one_allowed_country = array_keys( $countries )[0];
		}

		$svea_data = [];

		$wc_invoice = WC_Gateway_Svea_Invoice::init();
		$wc_part_pay = WC_Gateway_Svea_Part_Pay::init();

		$svea_data['gaSecurity'] = wp_create_nonce( Ajax_Functions::GET_ADDRESS_NONCE_NAME );
		$svea_data['ajaxUrl'] = admin_url( 'admin-ajax.php' );
		$svea_data['onlyOneAllowedCountry'] = ( $only_one_allowed_country ? $only_one_allowed_country : false );
		$svea_data['sameShippingAsBilling'] = [
			$wc_invoice->id  => $wc_invoice->enabled === 'yes' ? ( $wc_invoice->same_shipping_as_billing ? true : false ) : false,
			$wc_part_pay->id => $wc_part_pay->enabled === 'yes' ? ( $wc_part_pay->same_shipping_as_billing ? true : false ) : false,
		];

		$svea_data['isPayPage'] = is_checkout_pay_page() ? true : false;

		if ( is_checkout_pay_page() ) {
			$svea_data['customerCountry'] = WC()->customer->get_billing_country();
		}

		/**
		 * Localize the javascript with Svea data
		 */
		wp_localize_script( 'sveawebpay-js', 'Svea', $svea_data );

		$phrases = [
			'no_payment_plans_country_total' => __( 'There are no available payment plans for this country and order total.', 'svea-webpay-for-woocommerce' ),
			'your_address_was_found'         => __( 'Your address was found.', 'svea-webpay-for-woocommerce' ),
			'part_payment_plans'             => __( 'Part Payment Plans', 'svea-webpay-for-woocommerce' ),
			'company_name'                   => __( 'Company Name', 'svea-webpay-for-woocommerce' ),
			'invoice_fee'                    => __( 'Invoice fee', 'svea-webpay-for-woocommerce' ),
			'includes'                       => __( 'Includes', 'svea-webpay-for-woocommerce' ),
			'vat'                            => __( 'VAT', 'svea-webpay-for-woocommerce' ),
			'could_not_get_address'          => __( 'An error occurred whilst getting your address. Please try again later.', 'svea-webpay-for-woocommerce' ),
		];

		/**
		 * Localize the javascript with translated phrases
		 */
		wp_localize_script( 'sveawebpay-js', 'Phrases', $phrases );
	}
}
