<?php

namespace Discountify\Base;

use Discountify;
use Discountify\Utils\Singleton;

defined( 'ABSPATH' ) || exit;

/**
 * Enqueue all css and js file class
 */
class Enqueue {

	use Singleton;

	/**
	 * Main calling function
	 */
	public function init() {
		// backend asset
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_assets' ) );
		// frontend asset
		add_action( 'wp_enqueue_scripts', array( $this, 'frontend_enqueue_assets' ) );
		// enqueue editor css.
		add_action( 'elementor/editor/before_enqueue_styles', array( $this, 'elementor_editor_css' ) );
		// enqueue editor js.
		add_action( 'elementor/frontend/before_enqueue_scripts', array( $this, 'elementor_js' ) );
	}


	/**
	 * all admin js files function
	 */
	public function admin_get_scripts() {
		$plugin_dir    = \Discountify::plugin_dir();
		$script_arr = array(
			'discountify-select2'         => array(
				'src'     => \Discountify::assets_url() . 'js/discountify-select2.js',
				'version' => file_exists( $plugin_dir . 'assets/js/discountify-select2.js' ) ? filemtime( $plugin_dir . 'assets/js/discountify-select2.js' ) : \Discountify::get_version(),
				'deps'    => array( 'jquery' ),
			),
			'discountify-flatpickr'       => array(
				'src'     => \Discountify::assets_url() . 'js/flatpickr.min.js',
				'version' => file_exists( $plugin_dir . 'assets/js/flatpickr.min.js' ) ? filemtime( $plugin_dir . 'assets/js/flatpickr.min.js' ) : \Discountify::get_version(),
				'deps'    => array(),
			),
			'admin-discountify'           => array(
				'src'     => \Discountify::assets_url() . 'js/admin.js',
				'version' => file_exists( $plugin_dir . 'assets/js/admin.js' ) ? filemtime( $plugin_dir . 'assets/js/admin.js' ) : \Discountify::get_version(),
				'deps'    => array( 'jquery', 'discountify-select2', 'discountify-flatpickr' ),
			),
		);

		return $script_arr;
	}

	/**
	 * all admin css files function
	 */
	public function admin_get_styles() {
		$admin_css_path = \Discountify::plugin_dir() . 'assets/css/admin.css';
		return array(
			'discountify-admin'           => array(
				'src'     => \Discountify::assets_url() . 'css/admin.css',
				'version' => file_exists( $admin_css_path ) ? filemtime( $admin_css_path ) : \Discountify::get_version(),
			),
			'discountify-select2'         => array(
				'src'     => \Discountify::assets_url() . 'css/select2.css',
				'version' => \Discountify::get_version(),
			),
			'discountify-flatpickr'       => array(
				'src'     => \Discountify::assets_url() . 'css/flatpickr.min.css',
				'version' => \Discountify::get_version(),
			),
		);
	}

	/**
	 * Enqueue admin js and css function
	 *
	 * @param  $var
	 */
	public function admin_enqueue_assets() {
		$screen = get_current_screen();
		$pages  = \Discountify\Utils\Helper::admin_pages_id();

		// load js in specific pages
		if ( is_admin() && ( in_array( $screen->id, $pages ) ) ) {
			foreach ( $this->admin_get_scripts() as $key => $value ) {
				$deps    = ! empty( $value['deps'] ) ? $value['deps'] : false;
				$version = ! empty( $value['version'] ) ? $value['version'] : false;
				wp_enqueue_script( $key, $value['src'], $deps, $version, true );
			}


			// css

			foreach ( $this->admin_get_styles() as $key => $value ) {
				$deps    = isset( $value['deps'] ) ? $value['deps'] : false;
				$version = ! empty( $value['version'] ) ? $value['version'] : false;
				wp_enqueue_style( $key, $value['src'], $deps, $version, 'all' );
			}

			// localize for admin
			extract( Discountify\Utils\Helper::instance()->get_settings() );
			$form_data                      = array();
			$form_data['ajax_url']          = admin_url( 'admin-ajax.php' );
			$form_data['rules_page']        = esc_url( admin_url() . 'admin.php?page=discountify-rules' );
			$form_data['discountify_nonce'] = wp_create_nonce( 'discountify_nonce' );
			$form_data['currency']          = class_exists( 'WooCommerce' ) ? get_woocommerce_currency_symbol() : '';
			$form_data['is_pro']            = class_exists( 'DiscountifyPro' ) ? true : false;
			$form_data['subtotal']          = esc_html__( 'Subtotal:', 'discountify' );
			$form_data['discount']          = esc_html__( 'Discount:', 'discountify' );
			$form_data['min_spend']         = esc_html__( 'Minimum Spend:', 'discountify' );
			$form_data['max_spend']         = esc_html__( 'Maximum Spend:', 'discountify' );
			$form_data['min_qty']           = esc_html__( 'Minimum QTY:', 'discountify' );
			$form_data['max_qty']           = esc_html__( 'Maximum QTY:', 'discountify' );
			$form_data['free']              = esc_html__( 'Free', 'discountify' );
			$form_data['all_products']      = esc_html__( 'All Products', 'discountify' );

			wp_localize_script( 'admin-discountify', 'discount_admin_data', $form_data );
		}
	}

	/**
	 * all js files function
	 */
	public function frontend_get_scripts() {
		$script_arr = array();

		return $script_arr;
	}

	/**
	 * all css files function
	 */
	public function frontend_get_styles() {
		$enequeue = array(
			'discountify-public' => array(
				'src'     => \Discountify::assets_url() . 'css/public.css',
				'version' => \Discountify::get_version(),
			),
		);

		return $enequeue;
	}

	/**
	 * Enqueue admin js and css function
	 */
	public function frontend_enqueue_assets() {
		// js
		$scripts = $this->frontend_get_scripts();

		foreach ( $scripts as $key => $value ) {
			$deps    = isset( $value['deps'] ) ? $value['deps'] : false;
			$version = ! empty( $value['version'] ) ? $value['version'] : false;
			wp_enqueue_script( $key, $value['src'], $deps, $version, true );
		}

		// css
		$styles = $this->frontend_get_styles();

		foreach ( $styles as $key => $value ) {
			$deps    = isset( $value['deps'] ) ? $value['deps'] : false;
			$version = ! empty( $value['version'] ) ? $value['version'] : false;
			wp_enqueue_style( $key, $value['src'], $deps, $version, 'all' );
		}

		// localize for frontend
		$form_data                 = array();
		$form_data['wpc_ajax_url'] = admin_url( 'admin-ajax.php' );

		wp_localize_script( 'discountify-js', 'discountify_client_data', array( json_encode( $form_data ) ) );
	}

	/**
	 * Elementor editor css loaded
	 */
	public function elementor_editor_css() {
	}

	/**
	 * Elementor js loaded
	 */
	public function elementor_js() {
	}
}
