<?php
namespace Admin;

/**
 * Class Assets
 * @package Forumax_Pro\Admin
 */
class Admin_Assets
{
	/**
	 * Assets constructor.
	 */
	public function __construct()
	{
		add_action('admin_enqueue_scripts', [$this, 'admin_scripts'], 999);
	}

	public function admin_scripts()
	{
		wp_enqueue_style('bbpc-admin-global', FORUMAX_STYLES . 'admin/admin-global.css');

		wp_register_style('bbpc-admin', FORUMAX_STYLES . 'admin/bbpc-admin.css');

		wp_register_style('sweetalert', FORUMAX_VEND . 'sweetalert/sweetalert.css');
		wp_register_script('sweetalert', FORUMAX_VEND . 'sweetalert/sweetalert.min.js', ['jquery'], '1.0.0', true);

		wp_register_script('bbpc-notify-review', FORUMAX_ASSETS . 'admin/js/review.js', array('jquery'), FORUMAX_VERSION, true);

		if (forumax_admin_pages('admin')) {
			wp_enqueue_style('bbpc-admin');
			wp_enqueue_style('frmx-nice-select', FORUMAX_VEND . 'frmx-nice-select/nice-select.css');
			wp_enqueue_style('bbpc-admin-ui', FORUMAX_STYLES . 'admin/admin-ui-style.css');
			wp_enqueue_style('sweetalert');

			// Scripts.
			wp_enqueue_script('jquery-ui', FORUMAX_ADMIN_ASS . 'js/jquery-ui.js', ['jquery'], '1.12.1', true);
			wp_enqueue_script('mixitup', FORUMAX_VEND . 'mixitup/mixitup.min.js', ['jquery'], '3.3.1', true);
			wp_enqueue_script('mixitup-multifilter', FORUMAX_VEND . 'mixitup/mixitup-multifilter.js', ['jquery'], '3.3.1', true);
			wp_enqueue_script('mixitup-pagination', FORUMAX_VEND . 'mixitup/mixitup-pagination.min.js', ['jquery'], '3.3.2', true);
			wp_enqueue_script('frmx-nice-select', FORUMAX_VEND . 'frmx-nice-select/jquery.nice-select.min.js', ['jquery'], '1.0', true);
			wp_enqueue_script('tabby-polyfills', FORUMAX_ADMIN_ASS . 'js/tabby.polyfills.min.js', ['jquery'], '1.0', true);
			wp_enqueue_script('sortable', FORUMAX_ADMIN_ASS . 'js/Sortable.min.js', ['jquery'], '1.0', true);
			wp_enqueue_script('accordion', FORUMAX_ADMIN_ASS . 'js/accordion.min.js', ['jquery'], '1.0', true);
			wp_enqueue_script('bbpc-admin-main', FORUMAX_ADMIN_ASS . 'js/admin-main.js', ['jquery'], '1.0', true);
			wp_enqueue_script('sweetalert');
		}

		if (forumax_admin_pages('settings')) {
			wp_enqueue_style('bbpc-admin');
			wp_enqueue_style('sweetalert');
			wp_enqueue_script('sweetalert');
			wp_enqueue_script('bbpc-admin-global', FORUMAX_ADMIN_ASS . 'js/admin-global.js', FORUMAX_VERSION);
		}

		/*
				if ( forumax_admin_pages('settings') ) {
					wp_deregister_style('csf-fa5');
					wp_deregister_style('csf-fa5-v4-shims');
				}
		*/

		// Dashboard page assets.
		if (forumax_admin_pages('dashboard')) {
			wp_enqueue_style('bbpc-admin');
			wp_enqueue_style(
				'forumax-dashboard',
				FORUMAX_STYLES . 'admin/dashboard-styles.css',
				array(),
				FORUMAX_VERSION
			);
		}

		// Setup Wizard page assets.
		if (forumax_admin_pages('setup')) {
			wp_enqueue_style('bbpc-admin');
			wp_enqueue_style(
				'forumax-setup-wizard',
				FORUMAX_STYLES . 'admin/dashboard-styles.css',
				array(),
				FORUMAX_VERSION
			);
			wp_enqueue_script(
				'forumax-setup-wizard-js',
				FORUMAX_ADMIN_ASS . 'js/setup-wizard.js',
				array('jquery'),
				FORUMAX_VERSION,
				true
			);
			wp_localize_script(
				'forumax-setup-wizard-js',
				'forumaxWizard',
				[
					'ajaxurl'      => esc_url( admin_url( 'admin-ajax.php' ) ),
					'nonce'        => wp_create_nonce( 'forumax-wizard-nonce' ),
					'fieldRequired' => esc_html__( 'This field is required', 'forumax' ),
					'saveFinish'   => esc_html__( 'Save & Finish', 'forumax' ),
					'continue'     => esc_html__( 'Continue', 'forumax' ),
					'saving'       => esc_html__( 'Saving…', 'forumax' ),
					'saveError'    => esc_html__( 'Could not save settings. Please try again.', 'forumax' ),
				]
			);
		}

		// Free analytics presentation page assets.
		if ( forumax_admin_pages( 'analytics' ) && ! defined( 'BBPCPRO_PATH' ) && ! class_exists( '\Forumax_Pro\Admin\Analytics' ) ) {
			wp_enqueue_style( 'bbpc-admin' );
			wp_enqueue_style(
				'forumax-analytics-locked',
				FORUMAX_ADMIN_ASS . 'css/analytics-locked.css',
				array(),
				FORUMAX_VERSION
			);
		}


		// Localize the script with new data
		$ajax_url = admin_url('admin-ajax.php');
		$wpml_current_language = apply_filters('wpml_current_language', null);

		if (!empty($wpml_current_language)) {
			$ajax_url = add_query_arg('wpml_lang', $wpml_current_language, $ajax_url);
		}

		wp_localize_script(
			'jquery',
			'forumax_local_object',
			[
				'ajaxurl' => esc_url($ajax_url),
				'create_forum_title' => esc_html__('Enter Forum Title', 'forumax'),
				'create_topic_title' => esc_html__('Enter Topic Title', 'forumax'),
				'forum_delete_title' => esc_html__('Are you sure to delete?', 'forumax'),
				'forum_delete_desc' => esc_html__("This forum will be deleted with all the topics and you won't be able to revert!", 'forumax'),
				'topic_delete_desc' => esc_html__("This topic will be deleted and you won't be able to revert!", 'forumax'),
				'forumax_assets' => FORUMAX_ASSETS,
				'nonce' => wp_create_nonce('bbpc-admin-nonce')
			]
		);
	}
}
