<?php

namespace Cart\notify;

use Cart\notify\Frontend\NotificationData;
use Cart\notify\Settings\Repository;
use Cart\notify\WooCommerce\Integration;

defined( 'ABSPATH' ) || exit;

/**
 * Assets handlers class
 */
class Assets {
	/**
	 * Settings repository.
	 *
	 * @var Repository
	 */
	private $settings;

	/**
	 * WooCommerce integration helper.
	 *
	 * @var Integration
	 */
	private $woocommerce;

	/**
	 * Frontend notification data builder.
	 *
	 * @var NotificationData
	 */
	private $notification_data;

	/**
	 * Class constructor
	 *
	 * @param Repository|null       $settings Settings repository.
	 * @param Integration|null      $woocommerce WooCommerce integration helper.
	 * @param NotificationData|null $notification_data Notification data builder.
	 */
	function __construct( Repository $settings = null, Integration $woocommerce = null, NotificationData $notification_data = null ) {
		$this->settings          = $settings ? $settings : new Repository();
		$this->woocommerce       = $woocommerce ? $woocommerce : new Integration();
		$this->notification_data = $notification_data ? $notification_data : new NotificationData( $this->settings );

		add_action( 'admin_enqueue_scripts', [ $this, 'register_assets' ] );
		add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ] );

		// Js localization
		add_action( 'admin_enqueue_scripts', [ $this, 'cart_notify_localize_script' ] );

		// Frontend variable localization
		add_action( 'wp_enqueue_scripts', [ $this, 'cns_custom_enqueue_scripts' ], 20 );
	}

	/**
	 * Validate that WooCommerce runtime APIs are available.
	 *
	 * @return bool
	 */
	private function is_woocommerce_ready() {
		return $this->woocommerce->is_available();
	}

	/**
	 * All available scripts
	 *
	 * @return array
	 */
	public function get_scripts() {
		return [ 
			'cartnotify-script' => [ 
				'src' => CART_NOTIFY_ASSETS . '/js/frontend.js',
				'version' => filemtime( CART_NOTIFY_PATH . '/assets/js/frontend.js' ),
				'deps' => [ 'jquery' ]
			],

			'cartnotify-libs-script' => [ 
				'src' => CART_NOTIFY_ASSETS . '/libs/notiny.js',
				'version' => filemtime( CART_NOTIFY_PATH . '/assets/libs/notiny.js' ),
				'deps' => [ 'jquery' ]
			],

			'cartnotify-admin-script' => [ 
				'src' => CART_NOTIFY_ASSETS . '/js/CartNAdmin.js',
				'version' => filemtime( CART_NOTIFY_PATH . '/assets/js/CartNAdmin.js' ),
				'deps' => [ 'jquery' ]
			]
		];
	}

	/**
	 * All available styles
	 *
	 * @return array
	 */
	public function get_styles() {
		return [ 
			'cartnotify-libs-style' => [ 
				'src' => CART_NOTIFY_ASSETS . '/libs/notiny.css',
				'version' => filemtime( CART_NOTIFY_PATH . '/assets/libs/notiny.css' )
			],

			'cartnotify-style' => [ 
				'src' => CART_NOTIFY_ASSETS . '/css/frontend.css',
				'version' => filemtime( CART_NOTIFY_PATH . '/assets/css/frontend.css' )
			],

			'cartnotifyadmin-style' => [ 
				'src' => CART_NOTIFY_ASSETS . '/css/cartnotifyadmin.css',
				'version' => filemtime( CART_NOTIFY_PATH . '/assets/css/cartnotifyadmin.css' )
			]

		];
	}

	/**
	 * Register scripts and styles
	 *
	 * @return void
	 */
	public function register_assets() {
		$scripts = $this->get_scripts();
		$styles = $this->get_styles();

		foreach ( $scripts as $handle => $script ) {
			$deps = isset ( $script['deps'] ) ? $script['deps'] : false;

			wp_register_script( $handle, $script['src'], $deps, $script['version'], true );
		}

		foreach ( $styles as $handle => $style ) {
			$deps = isset ( $style['deps'] ) ? $style['deps'] : false;

			wp_register_style( $handle, $style['src'], $deps, $style['version'] );
		}

	}


	/**
	 * Js script localization
	 *
	 * @return array
	 */
	public function cart_notify_localize_script() {

		wp_enqueue_script( 'cartnotify-admin-script' );

		wp_localize_script( 'cartnotify-admin-script', 'CNS_admin', [ 
			'cns_admin_ajax' => admin_url( 'admin-ajax.php' ),
			'cns_ajax_nonce' => wp_create_nonce( 'updates' ),
		] );
	}

	/**
	 * Extra script var
	 *
	 * @return array
	 */
	public function cns_custom_enqueue_scripts() {
		if ( ! $this->is_woocommerce_ready() || ! wp_script_is( 'cartnotify-script', 'enqueued' ) ) {
			return;
		}

		$data = array_merge(
			array(
				'cns_ajax_nonce' => wp_create_nonce( 'ins_ajax_nonce' ),
				'ajax_url'       => admin_url( 'admin-ajax.php' ),
			),
			$this->woocommerce->get_script_data(),
			$this->notification_data->get_script_data()
		);

		wp_localize_script( 'cartnotify-script', 'CNS_params', $data );

		$this->add_frontend_inline_styles();
	}

	/**
	 * Add settings-driven inline styles.
	 *
	 * @return void
	 */
	private function add_frontend_inline_styles() {
		if ( ! wp_style_is( 'cartnotify-style', 'registered' ) ) {
			return;
		}

		$tokens = $this->settings->get_style_tokens();
		$css    = '.notiny{';

		foreach ( $tokens as $var_name => $var_value ) {
			if ( '' === $var_value ) {
				continue;
			}
			$css .= sprintf( '%s:%s;', esc_attr( $var_name ), esc_attr( $var_value ) );
		}

		$css .= '}';

		$custom_css = $this->settings->get_custom_css();
		if ( '' !== $custom_css ) {
			$css .= "\n" . $custom_css;
		}

		wp_add_inline_style( 'cartnotify-style', $css );
	}

}
