<?php

/**
 * Fired during plugin activation
 *
 * @link       oxyninja.com
 * @since      3.4.0
 *
 * @package    Oxy_Ninja
 * @subpackage Oxy_Ninja/includes
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      3.4.0
 * @package    Oxy_Ninja
 * @subpackage Oxy_Ninja/includes
 * @author     OxyNinja <rados@oxyninja.com>
 */
class Oxy_Ninja_Activator {
	/**
	 * WooCore / WooCommerce Notice if Oxygen Elements for WooCore not active
	 *
	 * @since    3.4.0
	 */
	public static function woocore_notices() {
		// TODO : ADMIN NOTICES
		$woocore_check = isset(get_option('oxygen_vsb_source_sites')['woocore']['accesskey']);
		if (!$woocore_check) {
			return false;
		}
		function substring_in_array($needle, array $haystack) {
			return array_reduce(
				$haystack,
				function ($inArray, $item) use ($needle) {
					return $inArray || false !== strpos($item, $needle);
				},
				false
			);
		}

		$woocore_woo = substring_in_array('oxygen-woocommerce', (array) get_option('active_plugins', []));

		function woocore_woo_oxy_elements() {
			$class = 'notice notice-warning';
			$message = __('Please activate Oxygen Elements for WooCommerce to use WooCore.', 'oxy-ninja');

			printf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), esc_html($message));
		}

		if ($woocore_check && !$woocore_woo) {
			add_action('admin_notices', 'woocore_woo_oxy_elements');
			return false;
		}
	}

	/**
	 * OxyNinja Icons - Add
	 *
	 * @since    3.4.0
	 */
	public static function oxyninja_icons_activate($set_name = '', $set_type = '') {
		if (empty($set_name) || empty($set_type)) {
			return false;
		}

		if (!function_exists('svg_admin_notice_error_on')) {
			function svg_admin_notice_error_on() {
				$class = 'notice notice-warning';
				$message = __(
					"Couldn't add OxyNinja SVG icon set. Try increasing the 'max_allowed_packet' database setting.",
					'oxy-ninja'
				);

				printf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), esc_html($message));
			}
		}

		if (!defined('CT_VERSION')) {
			return false;
		}

		// Parts Taken from Oxygen Builder
		$network_active = false;
		$containsSymbols = true;

		$file_content = file_get_contents(OXYNINJA_MAIN . 'admin/svg/' . $set_type . '/symbol-defs.svg');
		$xml = simplexml_load_string($file_content);

		foreach ($xml->children() as $def) {
			if ($def->getName() == 'defs') {
				foreach ($def->children() as $symbol) {
					if ($symbol->getName() == 'symbol') {
						$symbol['id'] = str_replace(' ', '', $set_name) . $symbol['id'];
					} else {
						$containsSymbols = false;
					}
				}
			} else {
				$containsSymbols = false;
			}
		}

		$file_content = $xml->asXML();

		if (!function_exists('is_plugin_active_for_network')) {
			require_once ABSPATH . '/wp-admin/includes/plugin.php';
		}
		if (is_plugin_active_for_network('oxy-ninja/oxy-ninja.php')) {
			$network_active = true;
		}

		if ($network_active) {
			if ($containsSymbols) {
				if (version_compare(CT_VERSION, '3.7.9', '<')) {
					$svg_sets = get_site_option('ct_svg_sets', []);
					if (isset($svg_sets[$set_name])) {
						return;
					}
					$svg_sets[$set_name] = $file_content;
					$result = update_site_option('ct_svg_sets', $svg_sets, get_site_option('oxygen_options_autoload'));
					if (false === $result) {
						add_action('admin_notices', 'svg_admin_notice_error_on');
					}
				} else {
					$svg_sets = get_site_option('ct_svg_sets_' . $set_name . ' 0', []);
					if ($svg_sets) {
						return false;
					}
					$result = update_site_option(
						'ct_svg_sets_' . $set_name . ' 0',
						$file_content,
						get_option('oxygen_options_autoload')
					);
					if (false === $result) {
						add_action('admin_notices', 'svg_admin_notice_error_on');
					} else {
						$svg_sets_names = get_site_option('ct_svg_sets_names', []);
						$svg_sets_names[$set_name] = 1;
						update_site_option('ct_svg_sets_names', $svg_sets_names, get_option('oxygen_options_autoload'));
					}
				}
			}
		} else {
			if ($containsSymbols) {
				if (version_compare(CT_VERSION, '3.7.9', '<')) {
					$svg_sets = get_option('ct_svg_sets', []);
					if (isset($svg_sets[$set_name])) {
						return;
					}
					$svg_sets[$set_name] = $file_content;
					$result = update_option('ct_svg_sets', $svg_sets, get_option('oxygen_options_autoload'));
					if (false === $result) {
						add_action('admin_notices', 'svg_admin_notice_error_on');
					}
				} else {
					$svg_sets = get_option('ct_svg_sets_' . $set_name . ' 0', []);
					if ($svg_sets) {
						return false;
					}
					$result = update_option(
						'ct_svg_sets_' . $set_name . ' 0',
						$file_content,
						get_option('oxygen_options_autoload')
					);
					if (false === $result) {
						add_action('admin_notices', 'svg_admin_notice_error_on');
					} else {
						$svg_sets_names = get_option('ct_svg_sets_names', []);
						$svg_sets_names[$set_name] = 1;
						update_option('ct_svg_sets_names', $svg_sets_names, get_option('oxygen_options_autoload'));
					}
				}
			}
		}
	}

	/**
	 * OxyNinja Icons - Remove
	 *
	 * @since    3.4.0
	 */
	public static function oxyninja_icons_remove($set_name = '') {
		if (empty($set_name)) {
			return false;
		}

		$svg_sets = get_option('ct_svg_sets', []);

		if (!defined('CT_VERSION')) {
			return false;
		}

		if (version_compare(CT_VERSION, '3.7.9', '<')) {
			if (isset($svg_sets[$set_name])) {
				unset($svg_sets[$set_name]);
				update_option('ct_svg_sets', $svg_sets, get_option('oxygen_options_autoload'));
			} else {
				return false;
			}
		} else {
			$svg_sets_names = get_option('ct_svg_sets_names', []);
			if (isset($svg_sets_names[$set_name])) {
				delete_option('ct_svg_sets_' . $set_name . ' 0');
				unset($svg_sets_names[$set_name]);
				update_option('ct_svg_sets_names', $svg_sets_names, get_option('oxygen_options_autoload'));
			}
		}
	}
}
