<?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_COMMON
 */

/**
 * 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_COMMON
 * @author     Nichita Sinitin <nikita@flosites.net>
 */
class FLO_COMMON
{
	/**
	 * 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->loader_uri = get_template_directory_uri() . '/dist/common/[**.css|**.js]'; // TODO : change uri
		$this->loader_path = dirname(__FILE__);

		$this->define_common_hooks();
	}

	private function define_common_hooks()
	{
		if (is_admin()) {
			add_action('admin_enqueue_scripts', array($this, 'enqueue_common_styles'));
			add_action('admin_enqueue_scripts', array($this, 'enqueue_common_scripts'));
		} else {
			add_action('wp_enqueue_scripts', array($this, 'enqueue_common_styles'));
			add_action('wp_enqueue_scripts', array($this, 'enqueue_common_scripts'));
		}
	}

	public function enqueue_common_styles()
	{
		$loader_uri = $this->loader_uri;
		// wp_enqueue_style( 'flo-common-css', $loader_uri . '[rest_of_the_path]', array(), $this->version, 'all' );
	}

	public function enqueue_common_scripts()
	{
		$loader_uri = $this->loader_uri;
		// wp_enqueue_script( 'flo-common-js', $loader_uri . '[rest_of_the_path]', array( 'jquery' ), $this->version, false );
	}


	/*
	* Retrieve the readme content from remote repo
	* @return - string/bool
	*/
	public static function getRemoteReadMe()
	{
		$transient_name = 'flo_guten_theme_readme';

		if (FALSE == $response = get_transient($transient_name)) {
			// request to remote repo
			$response = wp_remote_get('https://gitlab.com/api/v4/projects/24873564/repository/files/readme.txt/raw?private_token=dxreV5rHUSW8WyW2cuUA&ref=master');

			// save the request result to transient
	  set_transient($transient_name, $response, 24 * 60 * 60); // 24 hours transient
		}

		if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
			return wp_remote_retrieve_body($response);
		}
		return FALSE;
	}

	/*
	* Retrieve the changelog from the Readme data and format it.
	* @return array
	*/
	public static function getFormatedChangelog()
	{
		$result = [];

		$readme = self::getRemoteReadMe();

		// read everything between == Changelog == And == Credits ==
		$pattern = '/(== Changelog ==.*== Credits ==)/s';
		$raw_change_log = preg_match($pattern, $readme, $matches);

		if (is_array($matches) && sizeof($matches) && isset($matches[0])) {
			$raw_change_log = $matches[0];

			// remove == Changelog == & == Credits ==
			$pattern2 = '/(= \d.* =.*)(?=[\n]=)/sU';
			preg_match_all($pattern2, $raw_change_log, $change_log_array);

			if (is_array($change_log_array) && sizeof($change_log_array) && isset($change_log_array[0])) {
				foreach ($change_log_array[0] as $release_log) {
					// split release log by new line
					$release_arr = explode(PHP_EOL, $release_log);

					$current_release_log = [];
					$current_release_log_data = [];
					foreach ($release_arr as $key => $release_row) {

			// figure out the date and version
						if (strpos($release_row, '= ') === 0) {
							$rr = str_replace('= ', '', $release_row);
							$rr = str_replace(' =', '', $rr);
							$rr = explode(' - ', $rr);

							if (is_array($rr) && sizeof($rr)) {
								$current_release_log['realeaseDate'] = $rr[1];
								$current_release_log['version'] = $rr[0];
							}
						} else {
							if ($release_row != '') {
								// append each change log entry
								$current_release_log_data[] = ['name' => $release_row];
							} else {
								// The last row always should be empty because

								if (sizeof($current_release_log)) {
									$current_release_log['changelog'] = $current_release_log_data;
									$result[] = $current_release_log;

									// clear the current_release_log, so in case there are more empty rows, it does not affect us
									$current_release_log = [];
								}
							}
						} // EOF figure out the date and version
					} // EOF foreach ($release_arr ...
				}
			}
		}

		//var_dump($result);
		return $result;
	}

	public static function maybe_deactivate_flo_updater()
	{
		if (get_option('flo_pronto_disable_flo_updater') != '1') {
			self::deactivate_flo_updater();
			update_option('flo_pronto_disable_flo_updater', '1');
		}
	}

	/**
	 * Deactivate Flo Updater plugin.
	 * Because Pronto does not need it
	 */
	public static function deactivate_flo_updater()
	{
		if (is_admin() && !wp_doing_ajax()) {
			include_once(ABSPATH . 'wp-admin/includes/plugin.php');

			if (function_exists('is_plugin_active') && is_plugin_active('flothemes-themeup/flothemes-themeup.php')) {
				deactivate_plugins('flothemes-themeup/flothemes-themeup.php');
			}
		}
	}
}

new FLO_COMMON();
