<?php
/**
 * Admin Assets Class File
 *
 * This file contains Admin Assets class.
 * https://codex.wordpress.org/AJAX_in_Plugins
 */

namespace CarDealerPress\Includes\Init;

use CarDealerPress\includes\functions\Admin_Ajax;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Admin_Assets {

	public function __construct() {
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_assets' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'third_party_admin_assets' ) );
		$this->load_ajax_handle();
	}

	public function admin_assets() {
		wp_enqueue_style( 'cdp_admin_css', CDP_CSS . 'templates/admin/admin.css', array(), CDP_VERSION );
		wp_enqueue_script(
			'ajax-script',
			CDP_JS . 'templates/admin/admin.js',
			array(
				'jquery',
				'jquery-ui-core',
				'jquery-ui-dialog',
				'jquery-ui-datepicker',
			),
			CDP_VERSION,
			true
		);
		wp_localize_script(
			'ajax-script',
			'ajax_object',
			array(
				'ajax_url' => admin_url( 'admin-ajax.php' ),
				'nonce'    => wp_create_nonce( 'cdp_nonce' ),
			)
		);
	}

	public function third_party_admin_assets() {
		wp_enqueue_style( 'select2-css', CDP_CSS . 'select2/select2.min.css', array(), '4.1.0' );
		wp_enqueue_script( 'select2-js', CDP_JS . 'select2/select2.min.js', 'jquery', '4.0.13', true );

		if ( function_exists( 'wp_enqueue_media' ) ) {
			wp_enqueue_media();
		} else {
			wp_enqueue_style( 'thickbox' );
			wp_enqueue_script( 'media-upload' );
			wp_enqueue_script( 'thickbox' );
		}
		wp_enqueue_style( 'jquery-ui-datepicker' );
	}

	public function load_ajax_handle() {
		$admin_ajax = new Admin_Ajax();
		add_action( 'wp_ajax_admin_handle_request', array( &$admin_ajax, 'admin_handle_request' ) );
	}
}
