<?php

if ( ! class_exists( 'ALPHA_enqueue_scripts' ) ) :

	class ALPHA_enqueue_scripts {

		public $internalDir = 'gutenberg-extra/';
		/*
		 * Plugin constructor
		 */
		function __construct() {
			add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_block_modal_script' ) );
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_editor_pixel_media_tab_scripts' ) );
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_editor_header_toolbar_button' ) );
			add_action( 'enqueue_block_assets', array( $this, 'enqueue_editor_blocks_scripts' ) );
			add_action( 'init', array( $this, 'register_scripts' ) );
		}
		/**
		 * Enqueue block-modal compiled JS for Gutenberg editor.
		 */
		function enqueue_editor_block_modal_script() {

				$asset_file_inspector_generator = include plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'build/inspector-generator/index.asset.php';
				wp_enqueue_script(
					'alpha-inspector-generator',
					plugins_url( 'build/inspector-generator/index.js', ALPHA_PLUGIN_FILE ),
					$asset_file_inspector_generator['dependencies'],
					$asset_file_inspector_generator['version'],
					true
				);

			wp_enqueue_script(
				'alpha-block-modal',
				plugins_url( 'build/block-modal/index.js', ALPHA_PLUGIN_FILE ),
				array( 'wp-blocks', 'wp-element', 'wp-editor' ),
				filemtime( plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'build/block-modal/index.js' ),
				true
			);

			if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
				wp_enqueue_script(
					'alpha-blocks-dev-tools',
					plugins_url( 'build/dev-tools/index.js', ALPHA_PLUGIN_FILE ),
					array( 'wp-blocks', 'wp-dom-ready', 'wp-data' ),
					filemtime( plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'build/dev-tools/index.js' ),
					true
				);
			}
		}

		function enqueue_editor_header_toolbar_button() {
			// wp_enqueue_script(
			// 'header-toolbar-button',
			// plugins_url( 'assets/header-toolbar-button.js', ALPHA_PLUGIN_FILE ),
			// array( 'jquery' ),
			// filemtime( plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'assets/header-toolbar-button.js' ),
			// true
			// );
			wp_localize_script(
				'alpha-block-modal',
				'alpha',
				array(
					'alphaLibraryText' => __( 'Alpha Library', 'alpha' ),
				)
			);
			wp_enqueue_style(
				'alpha-style-modal-library',
				plugins_url( 'assets/style-modal-library.css', ALPHA_PLUGIN_FILE ),
				array(),
				filemtime( plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'assets/style-modal-library.css' )
			);
		}

		function enqueue_editor_pixel_media_tab_scripts() {
			wp_enqueue_script(
				'pixel-media-tab',
				plugins_url( 'assets/pixel-media-tab/pixel-media-tab.js', ALPHA_PLUGIN_FILE ),
				array( 'jquery', 'media-views', 'alpb-upload-image' ),
				filemtime( plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'assets/pixel-media-tab/pixel-media-tab.js' ),
				true
			);

			wp_enqueue_style(
				'pixel-media-tab',
				plugins_url( 'assets/pixel-media-tab/pixel-media-tab.css', ALPHA_PLUGIN_FILE ),
				array(),
				filemtime( plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'assets/pixel-media-tab/pixel-media-tab.css' )
			);

						// Enqueue upload-image-to-wp.js on frontend
			wp_enqueue_script(
				'alpb-upload-image',
				plugins_url( 'assets/pixel-media-tab/upload-image-to-wp.js', ALPHA_PLUGIN_FILE ),
				array(),
				filemtime( plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'assets/pixel-media-tab/upload-image-to-wp.js' ),
				true
			);
			// Localize nonce for upload-attachment
			wp_localize_script(
				'alpb-upload-image',
				'alpbUploadImage',
				array(
					'ajaxurl'   => admin_url( 'admin-ajax.php' ),
					'nonce'     => wp_create_nonce( 'upload-attachment' ),
					'pluginUrl' => plugin_dir_url( ALPHA_PLUGIN_FILE ),
				)
			);
		}

		function enqueue_editor_blocks_scripts() {
			if ( is_admin() ) {
				// Hard Coded Admin CSS
				wp_enqueue_style(
					'alpha-admin-css',
					plugins_url( 'admin-assets/admin-dashboard.css', ALPHA_PLUGIN_FILE ),
					array(),
					filemtime( plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'admin-assets/admin-dashboard.css' )
				);
				// Admin CSS with Tailwind
				wp_enqueue_style(
					'alpha-tailwind-admin-css',
					plugins_url( 'admin-assets/tailwind-admin-build.css', ALPHA_PLUGIN_FILE ),
					array(),
					filemtime( plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'admin-assets/tailwind-admin-build.css' )
				);
			}
		}

		public function register_scripts() {

			// All Block CSS from HTML
			wp_register_style(
				'alpha-block-tailwind-style',
				plugins_url( 'assets/tailwind-frontend-build.css', ALPHA_PLUGIN_FILE ),
				array(),
				filemtime( plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'assets/tailwind-frontend-build.css' )
			);
			wp_register_script(
				'alpha-alpine-collapse',
				plugins_url( 'assets/js/collapse.js', ALPHA_PLUGIN_FILE ),
				array(),
				filemtime( plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'assets/js/collapse.js' ),
				true
			);

			wp_register_script(
				'alpine-init',
				plugins_url( 'assets/js/init.js', ALPHA_PLUGIN_FILE ),
				array(),
				filemtime( plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'assets/js/init.js' ),
				true
			);

			wp_register_script(
				'alpine',
				plugins_url( 'assets/js/alpine.js', ALPHA_PLUGIN_FILE ),
				array( 'alpha-alpine-collapse', 'alpine-init' ),
				filemtime( plugin_dir_path( ALPHA_PLUGIN_FILE ) . 'assets/js/alpine.js' ),
				true
			);
		}
	}

endif;
