<?php
namespace OxygenApp\Hooks;
if ( ! defined( 'ABSPATH' ) ) exit;

use OxygenApp\Config\Config;
use OxygenApp\Utils\Labels;
use OxygenApp\Models\BackgroundSound;
use OxygenApp\Models\Background;
use OxygenApp\Models\AdminPlaylist;
use OxygenApp\Hooks\IHook;
use OxygenApp\Models\BackgroundCategory;

class ActionEnqueueScripts implements IHook {

    static function register() {
        add_action('admin_enqueue_scripts', [self::class, 'loadAdminScripts']);
    }

    static function loadAdminScripts() {
        $current_screen = get_current_screen();
        $isAdminSettingsPage = $current_screen->id === 'toplevel_page_' . Config::getInstance()->getSetting('main_settings_page_slug');
        $isAdminBackgroundSoundSingle = $current_screen->post_type === BackgroundSound::POST_TYPE && $current_screen->base === 'post';
        $isAdminPlaylistTax = $current_screen->id === 'edit-' . AdminPlaylist::TAX && $current_screen->post_type === BackgroundSound::POST_TYPE;
        $isAdminBackgroundSingle = $current_screen->post_type === Background::POST_TYPE && $current_screen->base === 'post';

        $isAdminBackgroundTax = $current_screen->id === 'edit-' . BackgroundCategory::TAX && $current_screen->post_type === Background::POST_TYPE;

        $isUserSettingsPage = $current_screen->id === 'toplevel_page_' . Config::getInstance()->getSetting('users_dashboard_page_slug');

        
        if ($isAdminSettingsPage || $isAdminBackgroundSoundSingle || $isAdminPlaylistTax
         || $isAdminBackgroundSingle || $isUserSettingsPage || $isAdminBackgroundTax) {
            wp_enqueue_script('jquery');
            self::loadAdminAssets();
            self::loadBootstrap();
            wp_enqueue_media();
        }
    }

    private static function loadAdminAssets() {
        wp_register_script('oxyleaf-admin-settings', OXYLEAF_APP_PUBLIC_URL . '/assets/admin/settings/dist/settings.js', ['jquery'], 'v1', true);
        wp_localize_script('oxyleaf-admin-settings', 'OXYLEAF_ADMIN_SETTINGS', [
            'labels' => Labels::getAdminLabels(),
            'urls' => [
                'publicUrl' => OXYLEAF_APP_PUBLIC_URL,
            ]
        ]);

        // Add settings for Ajax calls
        wp_localize_script('oxyleaf-admin-settings', 'OXYLEAF_APP_AJAX', [
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('oxyleaf_app_settings_nonce'),
        ]);

        wp_enqueue_script('oxyleaf-admin-settings');
        wp_enqueue_style('oxyleaf-admin-settings', OXYLEAF_APP_PUBLIC_URL . '/assets/admin/settings/dist/settings.css', NULL, 'v1');

        wp_enqueue_script('oxyleaf-select2', OXYLEAF_APP_PUBLIC_URL . '/assets/vendor/select2/select2.full.min.js', ['oxyleaf-admin-settings'], 'v1', true);
        wp_enqueue_style('oxyleaf-select2', OXYLEAF_APP_PUBLIC_URL . '/assets/vendor/select2/select2.min.css', ['oxyleaf-admin-settings'], 'v1');

        wp_enqueue_script('oxyleaf-toastr', OXYLEAF_APP_PUBLIC_URL . '/assets/vendor/toastr/toastr.min.js', ['oxyleaf-admin-settings'], 'v1', true);
        wp_enqueue_style('oxyleaf-toastr', OXYLEAF_APP_PUBLIC_URL . '/assets/vendor/toastr/toastr.min.css', ['oxyleaf-admin-settings'], 'v1');
    }

    private static function loadBootstrap() {
        wp_enqueue_script('oxyleaf-bootstrap', OXYLEAF_APP_PUBLIC_URL . '/assets/vendor/bootstrap/bootstrap.min.js', NULL, 'v1', true);
        wp_enqueue_style('oxyleaf-bootstrap', OXYLEAF_APP_PUBLIC_URL . '/assets/vendor/bootstrap/bootstrap.min.css', NULL, 'v1');
    }

	static function enqueRemoteStyle($key, $url, $version = 'v1'): void {
        $protocol = is_ssl() ? 'https' : 'http';
        wp_enqueue_style($key, $protocol . $url, NULL, $version);
    }

	static function enqueRemoteScript($key, $url, array $dependencies = [], string $version = 'v1', bool $inFooter = true): void {
        $protocol = is_ssl() ? 'https' : 'http';
        wp_enqueue_script($key, $protocol . $url, $dependencies, $version, $inFooter);
    }
}