<?php

namespace IGD_PRO;

defined( 'ABSPATH' ) || exit();

class Enqueue {

	private static $instance = null;

	public function __construct() {
		add_action( 'igd_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
		add_action( 'igd_admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ], 10, 2 );
	}

	public function enqueue_scripts() {

		$load_on_all_pages = igd_get_settings( 'loadScriptsOnAllPages' );

		// Enqueue PRO frontend scripts here
		$css_deps = [
			'igd-frontend'
		];

		// Slick Slider CSS
		wp_register_style( 'igd-slick-theme', IGD_PRO_ASSETS . '/vendor/slick/slick-theme.css', [], IGD_PRO_VERSION );
		wp_register_style( 'igd-slick', IGD_PRO_ASSETS . '/vendor/slick/slick.css', [ 'igd-slick-theme' ], IGD_PRO_VERSION );
		if ( $load_on_all_pages ) {
			$css_deps[] = 'igd-slick';
		}

		// Plyr CSS
		wp_register_style( 'igd-plyr', IGD_PRO_ASSETS . '/vendor/plyr/plyr.min.css', [], '3.7.8' );
		if ( $load_on_all_pages ) {
			$css_deps[] = 'igd-plyr';
		}

		wp_enqueue_style( 'igd-frontend-pro', IGD_PRO_ASSETS . '/css/frontend.css', $css_deps, IGD_PRO_VERSION );

		$js_deps = [
			'igd-frontend'
		];

		// Slider
		wp_register_script( 'igd-slick', IGD_PRO_ASSETS . '/vendor/slick/slick.js', [ 'wp-element' ], IGD_PRO_VERSION, true );
		if ( $load_on_all_pages ) {
			$js_deps[] = 'igd-slick';
		}

		// Plyr
		wp_register_script( 'igd-plyr', IGD_PRO_ASSETS . '/vendor/plyr/plyr.min.js', [ 'jquery' ], '3.7.8', true );
		if ( $load_on_all_pages ) {
			$js_deps[] = 'igd-plyr';
		}

		wp_register_script( 'igd-frontend-pro', IGD_PRO_ASSETS . '/js/frontend.js', $js_deps, IGD_PRO_VERSION, true );

	}

	public function enqueue_admin_scripts( $hook, $admin_pages = [] ) {

		// Enqueu Admin Styles
		wp_enqueue_style( 'igd-admin-pro', IGD_PRO_ASSETS . '/css/admin.css', [ 'igd-admin' ], IGD_PRO_VERSION );

		// Statistics page scripts
		if ( ! empty( $admin_pages['statistics_page'] ) && $admin_pages['statistics_page'] === $hook ) {

			if ( igd_get_settings( 'enableStatistics' ) ) {
				wp_register_script( 'igd-chart', IGD_PRO_ASSETS . '/vendor/chart.js', array(), '3.0.0' );

				wp_enqueue_script( 'igd-statistics', IGD_PRO_ASSETS . '/js/statistics.js', [
					'igd-admin',
					'igd-chart'
				], IGD_PRO_VERSION, true );
			}

		}

		// Settings Page Scripts
		if ( isset( $admin_pages['settings_page'] ) && $admin_pages['settings_page'] === $hook ) {
			// Google Picker API
			wp_enqueue_script( 'igd-google-drive-picker', IGD_PRO_ASSETS . '/vendor/google-picker-api.js', array(), IGD_PRO_VERSION, true );
		}

		// Slick Slider CSS
		wp_register_style( 'igd-slick-theme', IGD_PRO_ASSETS . '/vendor/slick/slick-theme.css', [], IGD_PRO_VERSION );
		wp_register_style( 'igd-slick', IGD_PRO_ASSETS . '/vendor/slick/slick.css', [ 'igd-slick-theme' ], IGD_PRO_VERSION );

		// Plyr CSS
		wp_register_style( 'igd-plyr', IGD_PRO_ASSETS . '/vendor/plyr/plyr.min.css', [], '3.7.8' );

		// Slider
		wp_register_script( 'igd-slick', IGD_PRO_ASSETS . '/vendor/slick/slick.js', [ 'wp-element' ], IGD_PRO_VERSION, true );

		// Plyr
		wp_register_script( 'igd-plyr', IGD_PRO_ASSETS . '/vendor/plyr/plyr.min.js', [ 'jquery' ], '3.7.8', true );

		// Slick Slider
		if ( igd_is_block_editor() || igd_is_divi_builder() || igd_is_elementor_editor()) {
			wp_enqueue_style( 'igd-slick' );
			wp_enqueue_script( 'igd-slick' );

			wp_enqueue_style( 'igd-plyr' );
			wp_enqueue_script( 'igd-plyr' );
		}

		wp_enqueue_script( 'igd-admin-pro', IGD_PRO_ASSETS . '/js/admin.js', [ 'igd-admin' ], IGD_PRO_VERSION, true );
	}

	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

}

Enqueue::instance();
