<?php
if (! defined('ABSPATH')) exit;

class UAMT_Enqueue_Script
{
    /**
     * Summary of instance
     * @return UAMT_Enqueue_Script
     */
    public static function instance()
    {
        static $instance = null;
        if (null === $instance) {
            $instance = new self();
        }
        return $instance;
    }
    /**
     * Summary of __construct
     */
    public function __construct()
    {
        add_action('admin_enqueue_scripts', array($this, 'uamt_enqueue_admin_scripts'));
    }


    /**
     * Summary of uamt_enqueue_admin_scripts
     * @param mixed $hook
     * @return void
     */
    public function uamt_enqueue_admin_scripts($hook)
    {
        if ('settings_page_uamt_admin_page' !== $hook) {
            return;
        }

        // sweetalert2
        wp_enqueue_style(
            'uamt-sweetalert2',
            UAMT_ASSETS . '/css/sweetalert2.min.css',
            [],
            '11.4.8'
        );

        wp_enqueue_style(
            'uamt-style',
            UAMT_ASSETS . '/css/admin-style.css',
            [],
            UAMT_VERSION
        );

        // sweetalert2
        wp_enqueue_script(
            'uamt-sweetalert2',
            UAMT_ASSETS . '/js/sweetalert2.min.js',
            ['jquery'],
            '11.4.8',
            true
        );

        wp_enqueue_script(
            'uamt-script',
            UAMT_ASSETS . '/js/admin-script.js',
            ['jquery'],
            UAMT_VERSION,
            true
        );

        wp_localize_script('uamt-script', 'uamt', [
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('uamt'),
            'save_nonce' => wp_create_nonce('uamt_settings_nonce'),
            'i18n' => [
                'file_desc' => __('File Description', 'upload-any-mime-types'),
                'mime_type' => __('MIME Type', 'upload-any-mime-types'),
                'extension' => __('Extension (without dot)', 'upload-any-mime-types'),
                'extension_exists' => 'Extension "%s" already exists. Please use a different extension.',
                'confirm_delete' => __('Are you sure you want to delete this MIME type?', 'upload-any-mime-types'),
                'saving' => __('Saving...', 'upload-any-mime-types'),
                'saved' => __('Settings saved successfully.', 'upload-any-mime-types'),
                'error' => __('Error saving settings.', 'upload-any-mime-types')
            ],
            'default_extensions' => array_map(function ($type) {
                return $type['ext'];
            }, UAMT_Mime_Types_Handler::uamt_get_default_mime_types())
        ]);

        wp_enqueue_style('dashicons');
    }
}

UAMT_Enqueue_Script::instance();
