<?php

add_filter('render_block', 'dragblock_renderability_render', 1, 2);
function dragblock_renderability_render($block_content, $parsed_block)
{

	// var_dump('dragblock_renderability_render()', $parsed_block['blockName']);
	
	if (
		'core/null' === $parsed_block['blockName'] ||
		empty($parsed_block['attrs']['dragBlockRenderability'])
	) {
		return $block_content;
	}


	/**
    The props that have same slug will be counted as a group of an OR group
    Different groups will be counted as a group of an AND group
	 */
	// check conditions
	$browsers = null;
	$devices = null;
	$oses = null;
	$user_logged = null;
	$languages = null;
	// var_dump(get_locale(), $parsed_block['attrs']['dragBlockRenderability']);
	// var_dump($parsed_block['blockName'], $parsed_block['attrs']['dragBlockClientId'], $parsed_block['attrs']['dragBlockRenderability']);
	$url_query_conditions = array();

	// var_dump($_GET, $parsed_block['attrs']['dragBlockRenderability']);

	// calculate the condition for each group
	foreach ($parsed_block['attrs']['dragBlockRenderability'] as $condition) {

		if (!empty($condition['disabled']) || empty($condition['slug']) || !isset($condition['value'])) {			
			continue;
		}
		
		// url-queries
		// this uses complicated logic so we collect first and calculate lates
		if ('url-query' === $condition['slug']) {
			// var_dump($condition);
			if (empty($condition['name'])) continue;			
			
			$name = $condition['name'];
			
			// if (!isset($_GET[$name])) {
			// 	var_dump($name , 'is empty');
			// 	return '';
			// }

			if (empty($url_query_conditions[$name])) {
				$url_query_conditions[$name] = array();
			}
			array_push($url_query_conditions[$name], $condition);
			continue;
		}


		if (empty($condition['value'])) {
			continue;
		}
		if ('render' === $condition['slug']) {
			if ($condition['value'] === 'never') {
				return '';
			}
			return $block_content;
		}

		// if one condition of a group is matched
		// the whole group condition will be true because 
		// same slug will use OR operator
		// ex: browser = firefox, browser = chrome => browser = firefox OR chrome
		// as browser = firefox AND chrome will never be satisfied.

		// languages
		if ('language' === $condition['slug']) {
			if (null === $languages) {
				$languages = false;
			}
			if (false === $languages) {
				if ((empty($condition['operator']) || '==' === $condition['operator'])) {
					$languages = (get_locale() === $condition['value']);
				} else {
					$languages = (get_locale() !== $condition['value']);
				}
			}
			continue;
		}

		// browsers
		if ('browser' === $condition['slug']) {
			if (null === $browsers) {
				$browsers = false;
			}
			if (false === $browsers) {
				if ((empty($condition['operator']) || '==' === $condition['operator'])) {
					$browsers = (dragblock_get_current_browser() === $condition['value']);
				} else {
					$browsers = (dragblock_get_current_browser() !== $condition['value']);
				}
			}
			continue;
		}

		// devices
		if ('device' === $condition['slug']) {
			if (null === $devices) {
				$devices = false;
			}
			if (false === $devices) {
				if ((empty($condition['operator']) || '==' === $condition['operator'])) {
					$devices = (dragblock_get_current_device() === $condition['value']);
				} else {
					$devices = (dragblock_get_current_device() !== $condition['value']);
				}
			}
			continue;
		}

		// os
		if ('os' === $condition['slug']) {
			if (null === $oses) {
				$oses = false;
			}
			if (false === $oses) {
				if ((empty($condition['operator']) || '==' === $condition['operator'])) {
					$oses = (dragblock_get_current_os() === $condition['value']);
				} else {
					$oses = (dragblock_get_current_os() !== $condition['value']);
				}
			}
			continue;
		}

		// user logged
		if ('user-logged' === $condition['slug']) {
			if (null === $user_logged) {
				$user_logged = false;
			}
			if (false === $user_logged) {
				$user_logged_status = is_user_logged_in() ? 'in' : 'out';
				if ((empty($condition['operator']) || '==' === $condition['operator'])) {
					$user_logged = ($user_logged_status === $condition['value']);
				} else {
					$user_logged = ($user_logged_status !== $condition['value']);
				}
			}
			continue;
		}
	}


	// final check the url-query	
	// URL query uses the name of search query parameter as slug
	// so the logical operator is different
	// same name => OR because s == a OR s == b makes sense
	// different name => AND because users expect that
	// as different names use AND so if one name missed,
	// the whole is wrong
	// var_dump($url_query_conditions);
	foreach ($url_query_conditions as $name => $conditions) {
		// use OR for same query name
		$logic = false;
		// if not isse than we don't have this name in the conditions
		// so don't need to check
		$current = isset($_GET[$name]) ? sanitize_text_field(wp_unslash($_GET[$name])) : '';
		// var_dump($name, $current);
		foreach ($conditions as $cond) {
			$operator = empty($cond['operator']) || $cond['operator'] === '==' ? '==' : '!=';
			$value = $cond['value'];

			// using OR so only one condition satisfied, whole is true
			if (
				$operator === '==' && $current === $value ||
				$operator === '!=' && $current !== $value
			) {
				$logic = true;
				break;
			}
		}

		if ($logic === false) return '';
	}


	// if something is not available, set them to true
	if (null === $browsers) $browsers = true;
	if (null === $devices) $devices = true;
	if (null === $oses) $oses = true;
	if (null === $user_logged) $user_logged = true;
	if (null === $languages) $languages = true;

	if ($browsers && $devices && $oses && $user_logged && $languages) return $block_content;

	return '';
}
