<?php

namespace ProudNerds\Core;

/**
 * Class: Assets
 *
 * Loads the front and back end assets required for the website.
 */
class Assets {
    /**
     * @var string $assetsPathURI Assets URI path.
     */
    private static string $assetsPathURI = '';

    /**
     * @var string $assetsPathDir Assets directory path.
     */
    private static string $assetsPathDir = '';

    /**
     * Assets constructor.
     */
    public function __construct() {
        // Set the path URI and directory
        self::$assetsPathURI = get_stylesheet_directory_uri() . '/assets/';
        self::$assetsPathDir = get_stylesheet_directory() . '/assets/';
        // Load required stylesheets and scripts
        add_action('wp_enqueue_scripts', [__CLASS__, 'loadFrontEndAssets'], 10, 0);
        add_action('admin_enqueue_scripts', [__CLASS__, 'loadBackEndAssets'], 10, 0);
        add_action('wp_enqueue_scripts', [__CLASS__, 'loadCustomAssets'], 10, 0);
    }

    /**
     * Load assets based on the environment.
     *
     * @param bool $admin Whether to load admin assets.
     */
    static function loadAssets(bool $admin) {
        $prod = (OTAP_ENVIRONMENT == 'production');

        // Load comments script if it is enabled
        if(is_singular() && comments_open() && get_option('thread_comments')) {
            wp_enqueue_script('comment-reply');
        }

        // Enqueue front end stylesheets
        $uri = self::$assetsPathURI . 'sass/app' . ($admin ? '-admin' : '') . ($prod ? '.min.css' : '.compiled.css');
        $dir = self::$assetsPathDir . 'sass/app' . ($admin ? '-admin' : '') . ($prod ? '.min.css' : '.compiled.css');
        wp_enqueue_style('app', $uri, null, filemtime($dir));

        // Enqueue print stylesheet
        if(!$admin) {
            $print_uri = self::$assetsPathURI . 'sass/print' . ($prod ? '.min.css' : '.compiled.css');
            $print_dir = self::$assetsPathDir . 'sass/print' . ($prod ? '.min.css' : '.compiled.css');
            wp_enqueue_style('print', $print_uri, null, filemtime($print_dir), 'print');
        }

        // Enqueue jQuery
        wp_enqueue_script('jquery');

        // Associative array of script names and variables to localize
        $variables = [];

        // Enqueue front end scripts
        $uri = self::$assetsPathURI . 'javascript/app' . ($admin ? '-admin' : '') . ($prod ? '.min.js' : '.js');
        $dir = self::$assetsPathDir . 'javascript/app' . ($admin ? '-admin' : '') . ($prod ? '.min.js' : '.js');
        wp_enqueue_script('app', $uri, array('jquery'), filemtime($dir), true);
        $uriBlock = self::$assetsPathURI . 'javascript/blockhooks' . ($admin ? '-admin' : '') . ($prod ? '.min.js' : '.js');
        $dirBlock = self::$assetsPathDir . 'javascript/blockhooks' . ($admin ? '-admin' : '') . ($prod ? '.min.js' : '.js');
        wp_enqueue_script('js-blockhooks', $uriBlock, array('wp-blocks', 'wp-dom-ready', 'wp-edit-post'), filemtime($dirBlock), true);

        // Localize the script
        if(!empty($variables)) {
            wp_localize_script('app', 'variables', $variables);
        }
    }

    /**
     * Load assets for the front end.
     */
    static function loadFrontEndAssets() {
        self::loadAssets(false);
    }

    /**
     * Load assets for the front end.
     */
    static function loadBackEndAssets() {
        self::loadAssets(true);
    }

    /**
     * Enqueue custom asset scripts and styles required for the child theme like fonts, JS plugins, etc. Please check
     * if they have an available Composer or NPM package first before adding it using scripts.
     */
    static function loadCustomAssets() {
        wp_enqueue_style('font-open-sans', 'https://fonts.googleapis.com/css2?family=Open+Sans&display=swap', null, '', 'all');
    }
}