<?php
/**
 * @link       http://splendorstudio.net
 * @since      1.0.0
 *
 * @package    Fancify Core
 */
class Fancify_Lite_Admin extends Fancify_Lite_Controller {

	/**
	 * Constructor
	 *
	 * @since    1.0.0
	 */
	protected function __construct() {
		$this->register_hook_callbacks();
	}

	/**
	 * Register callbacks for actions and filters
	 *
	 * @since    1.0.0
	 */
	protected function register_hook_callbacks() {
		Fancify_Lite_Actions::add_smart_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
		Fancify_Lite_Actions::add_smart_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		
		$this->check_core_plugin();
	}

	/**
	 * Register the stylesheets for the admin side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {
		$font_query_args = array(
			'family' => 'Poppins:100,200,300,400,500,600,700,800,900%7CQuicksand:100,200,300,400,500,600,700,800,900%7CLato:100,200,300,400,500,600,700,800,900%7C',
			'subset' => 'latin'
		);

		wp_enqueue_style(
			'fancify-google-fonts',
			add_query_arg( $font_query_args, "//fonts.googleapis.com/css" ),
			array(),
			null
		);

		wp_enqueue_style( 'fancify-lite', FANCIFY_ADMIN_CSS_URL . 'admin' . '.css' , FANCIFY_VERSION, true );
	}	

	/**
	 * Register the JavaScript for the admin side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {
		wp_enqueue_script(
			'fancify',
			FANCIFY_ADMIN_JS_URL . 'admin.js',
			array( 'jquery', 'wp-util', 'updates' ),
			FANCIFY_VERSION,
			true
		);

		$this->loclize();
	}

	/**
	 * Define some loclization to js.
	 *
	 * @since    1.0.0
	 */
	public function loclize(){
		wp_localize_script( 'fancify' , 
			'fancifyObject', 
			array(
				'adminajax'         		=> esc_url( admin_url( 'admin-ajax.php' ) ),
				'plugin_installation_error'	=> esc_html__( 'Something went wrong with plugin installation.', 'fancify-lite' ),
				'redirect'					=> esc_html__( ' and you are redirecting...', 'fancify-lite' ),
				'activate'					=> esc_html__( 'Activate fancify core', 'fancify-lite' ),
				'core_status'       		=> $this->check_insalled_or_not(),
				'nonce' 					=> wp_create_nonce( 'fancify_lite_admin_ajax_nounce' ),
			)
		);
	}
	
	/**
	 * Include admin files.
	 *
	 * @since    1.0.0
	 */
	private function check_core_plugin() {
		// Determine plugin is activated or not
		if ( $this->check_insalled_or_not() ) {
			return;
		}

		// Check if user have ability to install plugin
		if ( ! current_user_can( 'install_plugins' ) ) {
			return;
		}

		// Include main installer class
		require_once FANCIFY_ADMIN_PATH . 'welcome/welcome.php';// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- loading welcome theme page its not template

		$core = Fancify_Lite_Fancify_Lite_Core::get_instance();
		$core->status = $this->check_insalled_or_not();
	}

	/**
	 * check core is installed or not
	 *
	 * @since    1.0.0
	 */
	private function check_insalled_or_not() {
		// Determine plugin is activated or not
		if ( class_exists( 'FancifyCore' ) ) {
			return true;
		} else {
			return false;
		}
	}

}

Fancify_Lite_Admin::get_instance();
