<?php
/**
 * Core plugin functionality.
 *
 * @package photoshelter-importer
 */

namespace PhotoShelter\Importer\Core;

use function PhotoShelter\Importer\Admin\get_localized_objects_array;

/**
 * Bootstrap.
 *
 * Things done on plugin initialization.
 *
 * @since 1.0.0
 */
function bootstrap() {
	add_action( 'init', __NAMESPACE__ . '\\register_assets' );
}

/**
 * Activate the plugin.
 *
 * Things to do when the plugin is activated.
 *
 * @since 1.0.0
 */
function activate() {
	flush_rewrite_rules();
}

/**
 * Deactivate the plugin.
 *
 * Things to do when the plugin is deactivated.
 *
 * @since 1.0.0
 */
function deactivate() {
}

/**
 * Get Plugin info array.
 *
 * @return array Array of plugin info.
 * @since 1.0.0
 */
function get_plugin_info() {
	$default_headers = array(
		'Name'        => 'Plugin Name',
		'PluginURI'   => 'Plugin URI',
		'Version'     => 'Version',
		'Description' => 'Description',
		'Author'      => 'Author',
		'AuthorURI'   => 'Author URI',
		'TextDomain'  => 'Text Domain',
		'DomainPath'  => 'Domain Path',
	);

	$plugin_data = \get_file_data( PHOTOSHELTER_IMPORTER_PLUGIN, $default_headers, 'plugin' );

	return array_change_key_case( $plugin_data, CASE_LOWER );
}

/**
 * Get Plugin data value by key string.
 *
 * @param string $key Optional data key string.
 *
 * @return string|array String if key is specified and exists, or Array if no key is specified.
 * @since 1.0.0
 */
function get_plugin_data( $key = '' ) {
	$plugin_info = get_plugin_info();

	if ( empty( $key ) ) {
		return $plugin_info;
	}

	if ( isset( $plugin_info[ $key ] ) ) {
		return $plugin_info[ $key ];
	}

	return '';
}

/**
 * Register assets.
 *
 * @since 1.1.0
 */
function register_assets() {
	$asset_root = PHOTOSHELTER_IMPORTER_PATH . 'assets/build/';
	$asset_uri  = PHOTOSHELTER_IMPORTER_URL . 'assets/build/';

	$asset_files = glob( $asset_root . '*.asset.php' );

	// Enqueue runtime.js, if it exists.
	if ( true === is_readable( $asset_root . 'runtime.js' ) ) {
		enqueue_script(
			'photoshelter-importer/runtime',
			$asset_uri . 'runtime.js',
			array(),
			filemtime( $asset_root . 'runtime.js' )
		);
	}

	foreach ( $asset_files as $asset_file ) {
		$asset_script = require $asset_file;

		$asset_filename = basename( $asset_file );

		$asset_slug_parts = explode( '.asset.php', $asset_filename );
		$asset_slug       = array_shift( $asset_slug_parts );

		$asset_handle = sprintf( 'photoshelter-importer/%s', $asset_slug );

		$stylesheet_path = $asset_root . $asset_slug . '.css';
		$stylesheet_uri  = $asset_uri . $asset_slug . '.css';

		$javascript_path = $asset_root . $asset_slug . '.js';
		$javascript_uri  = $asset_uri . $asset_slug . '.js';

		if ( true === is_readable( $stylesheet_path ) ) {
			// Filter dependencies to only include registered styles.
			$style_dependencies = array_filter(
				$asset_script['dependencies'],
				function ( $dep ) {
					return wp_style_is( $dep, 'registered' );
				}
			);

			wp_register_style(
				$asset_handle,
				$stylesheet_uri,
				$style_dependencies,
				$asset_script['version']
			);
		}

		if ( true === is_readable( $javascript_path ) ) {
			// Filter dependencies to only include registered scripts.
			$script_dependencies_before = $asset_script['dependencies'];
			$script_dependencies_after  = array_filter(
				$asset_script['dependencies'],
				function ( $dep ) {
					return wp_script_is( $dep, 'registered' );
				}
			);

			wp_register_script(
				$asset_handle,
				$javascript_uri,
				$asset_script['dependencies'],
				$asset_script['version'],
				array(
					'in_footer' => false,
				)
			);
		}
	}
}

/**
 * Enqueue script.
 *
 * @since 1.1.0
 *
 * @param string $handle       Script handle.
 * @param string $src          Script source.
 * @param array  $dependencies Script dependencies.
 * @param string $version      Script version.
 * @param bool   $in_footer    Whether to enqueue in footer.
 */
function enqueue_script(
	string $handle,
	string $src = '',
	array $dependencies = array(),
	$version = false,
	bool $in_footer = true
) {
	$localizes = array();

	switch ( $handle ) {
		case 'photoshelter-importer/library':
			$localizes[] = array(
				'object_name' => 'PhotoShelterImporter',
				'value'       => get_localized_objects_array(),
			);
			break;
	}

	wp_enqueue_script( $handle, $src, $dependencies, $version, $in_footer );

	if ( 0 < count( $localizes ) ) {
		foreach ( $localizes as $localize ) {
			$object_name  = $localize['object_name'] ?? '';
			$local_params = true === isset( $localize['value'] ) && true === is_array( $localize['value'] ) ?
				$localize['value'] :
				array();

			wp_localize_script(
				$handle,
				$object_name,
				$local_params
			);
		}
	}
}

/**
 * Enqueue style.
 *
 * @since 1.1.0
 *
 * @param string           $handle       Style handle.
 * @param string           $src          Style source.
 * @param string[]         $dependencies Style dependencies.
 * @param string|bool|null $version      Style version.
 * @param string           $media        Style media.
 *
 * @return void
 */
function enqueue_style(
	string $handle,
	string $src = '',
	array $dependencies = array(),
	$version = false,
	string $media = 'all'
) {
	wp_enqueue_style( $handle, $src, $dependencies, $version, $media );
}
