<?php

/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across the
 * admin area.
 *
 * @link       https://www.facebook.com/nikita.flo.735
 * @since      1.0.0
 *
 * @package    FLO_ADMIN
 */

/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    FLO_ADMIN
 * @author     Nichita Sinitin <nikita@flosites.net>
 */
class FLO_ADMIN
{
	/**
	 * The unique identifier of this plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $theme_name    The string used to uniquely identify this plugin.
	 */
	protected $theme_name;

	/**
	 * The current version of the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $version    The current version of the plugin.
	 */
	protected $version;

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, define the locale, and set the hooks for the admin area and
	 *
	 * @since    1.0.0
	 */
	public function __construct()
	{
		if (defined('FLEX_GUTENBERG_THEME_VERSION')) {
			$this->version = FLEX_GUTENBERG_THEME_VERSION;
		} else {
			$this->version = '1.0.0';
		}
		$this->plugins = array(
	  'floForms' => array(
		'path' => 'https://downloads.wordpress.org/plugin/flo-forms.zip',
		'entry' => 'flo-forms/flo-forms.php'
	  ),
	  'floFormsPro' => array(
		'path' => '',
		'entry' => 'flo-forms-pro/flo-forms.php'
	  ),
	  'flex' => array(
		'path' => 'https://s3.amazonaws.com/themes-downloadable-resources/plugins/flo-flex-builder.zip',
		'entry' => 'flo-flex-builder/flo-flex-builder.php'
	  ),
	  'floGutenberg' => array(
		'path' => 'https://themes-downloadable-resources.s3.amazonaws.com/plugins/flo-gutenberg-blocks.zip',
		'entry' => 'flo-gutenberg-blocks/flo-guten-blocks.php'
	  ),
	  'floMigration' => array(
		'path' => 'https://themes-downloadable-resources.s3.amazonaws.com/plugins/flomigration.zip',
		'entry' => 'flomigration/flomigration.php'
	  )
	);

		$this->loader_uri = get_template_directory_uri() . '/dist/'; // TODO : change uri
		$this->loader_path = dirname(__FILE__);

		$this->define_admin_hooks();
	}

	private function define_admin_hooks()
	{
		add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_styles'));
		add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'), 1);

		add_action('after_switch_theme', array($this, 'redirect_on_activation'));
		add_action('after_switch_theme', array($this, 'reset_wizard'));
	}

	public function reset_wizard()
	{
		update_option('wizardFinished', '0');
	}

	public function enqueue_admin_styles()
	{
		$loader_uri = $this->loader_uri;
		wp_enqueue_style('flo-admin', $loader_uri . 'admin.min.css', array(), $this->version, 'all');
		wp_enqueue_style('flo-admin-icomoon', get_template_directory_uri() . '/flo-app/admin/assets/icomoon/style.css', array(), $this->version, 'all');
		wp_enqueue_style('wp-color-picker');
	}

	public function enqueue_admin_scripts()
	{
		$loader_uri = $this->loader_uri;
		wp_enqueue_script('flo-admin', $loader_uri . 'admin.min.js', array('jquery'), $this->version, FALSE);
		wp_enqueue_script('jquery-ui-selectmenu');
		wp_enqueue_media();
		wp_enqueue_script('wp-color-picker');

		if (is_plugin_active('seo-by-rank-math/rank-math.php') || class_exists('RankMath')) {
			wp_enqueue_script('extend-content-rankmath', $loader_uri . 'rankMathCompat.min.js', ['jquery'], mt_rand(0, 99999), TRUE);
		}

		// localize the changelog data
		$change_log = FLO_COMMON::getFormatedChangelog();
		if (!$change_log) {
			$change_log = []; // empty array
		}
		wp_localize_script('flo-admin', 'flo_theme_change_log', $change_log);

		// localize wizard data	
		$root = dirname(dirname(dirname(dirname(dirname(__FILE__)))));

		// update_option('wizardFinished', '0');

		$json_data = [
	  'flex_version' => defined('Flo_Flex_Builder_Version') ? Flo_Flex_Builder_Version : '1.0.0',
	  'flo_environment' => defined('FLO_ENVIROMENT') ? FLO_ENVIROMENT : 'PROD',
	  'theme_name' => strtolower(wp_get_theme()->Name),
	  'wizardFinished' => get_option('wizardFinished'),
	  'currentOrderKey' => get_option('flotheme_theme_key'),
	  'lastThemeInstalledWithWizard' => get_option('lastThemeInstalledWithWizard'),
	  'flex_active_stylekit_name' => get_option('flex_active_stylekit_name'),
	  'flex_stylekits_manifest' => get_option('flex_stylekits_manifest'),
	  'userInfo' => get_option('userInfo'),
	  'flexWizardNonce' => wp_create_nonce('flex-nonce'),
	  'migrationFinished' => get_option("Flo_Migration_Finished"),
	  'migrated_from_flex_theme'=> $this-> get_old_theme(),
	];

		foreach ($this->plugins as $key => $plugin) {
			$path = $root . '/plugins/' . $this->plugins[$key]['entry'];

			$json_data['is_' . $key . '_installed'] = file_exists($path) ? 'true' : 'false';
			$json_data['is_' . $key . '_activated'] = is_plugin_active($this->plugins[$key]['entry']) ? 'true' : 'false';
		}

		wp_localize_script('flo-admin', 'flo_admin_apps_wizard_data', $json_data);


	}
	/**
	 * This function is to check if old theme if flex theme, to run migration plugin
	 */
	public function get_old_theme()
	{
		$themes= array("Nomade","Nessa","Mono","Ember","Zephyr","Cotton", "Verso", "June", "Lyra", "Faro", "Velvet", "Elise");
		$old_theme= get_option('flo_prev_theme');
		if (in_array($old_theme, $themes)) {
			return "true";
		} else {
			return "false";
		}
	}
	public function redirect_on_activation()
	{
		//redirect to theme settings page on theme activation
		$redirect =  get_admin_url() . "admin.php?page=parent";
		global $pagenow;
		if (is_admin() && isset($_GET['activated']) && $pagenow == "themes.php") {
			wp_redirect($redirect);
		}
	}
}

new FLO_ADMIN();
