<?php
/**
 * Enqueue admin scripts and styles.
 *
 * @package CoffeeWP_Media_Cleanup
 */

namespace CWPM\Admin;

use CWPM\Core\Db;

defined( 'ABSPATH' ) || exit;

class Assets {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
	}

	/**
	 * Enqueue assets on plugin pages only.
	 *
	 * @param string $hook_suffix The current admin page hook.
	 */
	public function enqueue( $hook_suffix ) {
		if ( ! $this->is_plugin_page( $hook_suffix ) ) {
			return;
		}

		wp_enqueue_style(
			'color-punch',
			CWPM_URL . 'assets/vendor/color-punch/css/app.css',
			array(),
			CWPM_VERSION
		);

		$admin_css = CWPM_PATH . 'assets/css/admin.css';
		wp_enqueue_style(
			'cwpm-admin',
			CWPM_URL . 'assets/css/admin.css',
			array( 'color-punch' ),
			file_exists( $admin_css ) ? (string) filemtime( $admin_css ) : CWPM_VERSION
		);

		wp_enqueue_style(
			'font-awesome',
			CWPM_URL . 'assets/vendor/font-awesome/css/all.min.css',
			array(),
			'6.5.1'
		);

		$cp_js_path = CWPM_PATH . 'assets/vendor/color-punch/js/';
		$cp_js_url  = CWPM_URL . 'assets/vendor/color-punch/js/';

		$cp_scripts = array( 'theme-switcher', 'tabs', 'modal', 'dropdown', 'alerts', 'save-button' );
		foreach ( $cp_scripts as $script ) {
			if ( file_exists( $cp_js_path . $script . '.js' ) ) {
				wp_enqueue_script(
					'cp-' . $script,
					$cp_js_url . $script . '.js',
					array(),
					CWPM_VERSION,
					true
				);
			}
		}

		wp_enqueue_script(
			'cwpm-admin',
			CWPM_URL . 'assets/js/admin.js',
			array( 'jquery', 'cp-theme-switcher', 'cp-tabs', 'cp-modal', 'cp-dropdown', 'cp-alerts' ),
			CWPM_VERSION,
			true
		);

		$user_theme = get_user_meta( get_current_user_id(), 'cwpm_theme_preference', true );
		$scan_meta  = Db::get_scan_meta();
		$nav_state  = Page::get_nav_state();

		$prefs = get_user_meta( get_current_user_id(), 'cwpm_user_prefs', true );
		if ( ! is_array( $prefs ) ) {
			$prefs = array(
				'compact_sizes'  => true,
				'group_by_month' => true,
				'per_page'       => 200,
			);
		}

		$config = array(
			'ajaxUrl'            => admin_url( 'admin-ajax.php' ),
			'nonce'              => wp_create_nonce( 'cwpm_nonce' ),
			'theme'              => $user_theme ? $user_theme : 'light',
			'pluginUrl'          => CWPM_URL,
			'scanMeta'           => $scan_meta ? array(
				'status'        => $nav_state['status'],
				'scan_id'       => $nav_state['scan_id'],
				'completed_at'  => $nav_state['completed_at'],
				'started_at'    => $nav_state['started_at'],
				'scanned_files' => (int) $nav_state['scanned_files'],
				'total_files'   => (int) $nav_state['total_files'],
			) : null,
			'userPrefs'          => $prefs,
			'autoDeleteLogs'     => (bool) get_option( 'cwpm_auto_delete_logs', false ),
			'trashRetentionDays' => (int) get_option( 'cwpm_trash_retention_days', 0 ),
			'wooActive'          => class_exists( 'WooCommerce' ),
		);

		/**
		 * Filter the JS-side CWPMConfig payload before it is localized.
		 *
		 * Extensions add their own state keys here so the main tree no longer
		 * needs to know about extension-only options.
		 *
		 * @param array $config The localization payload.
		 */
		$config = apply_filters( 'cwpm_admin_localize_data', $config );

		wp_localize_script( 'cwpm-admin', 'CWPMConfig', $config );
	}

	/**
	 * Check if the current page is a plugin page.
	 *
	 * @param string $hook_suffix Current admin page hook.
	 * @return bool
	 */
	private function is_plugin_page( $hook_suffix ) {

		$slug     = Page::MENU_SLUG;
		$prefixes = array(
			'toplevel_page_' . $slug,
			$slug . '_page_',
		);
		foreach ( $prefixes as $prefix ) {
			if ( 0 === strpos( (string) $hook_suffix, $prefix ) ) {
				return true;
			}
		}
		return false;
	}
}
