<?php


// we can create similar patterns that are much more beautiful
// remove_theme_support('core-block-patterns');

// you can find the core patterns at:
// /Users/sneeit/Sites/wordpress/app/public/wp-includes/block-patterns.php

/* default WordPress Pattern Categories
'banner' : 'Banners'
'buttons' : 'Buttons'
'columns' : 'Columns'
'text' : 'Text'
'query' : 'Posts' // overridden by 'posts'category
'posts' : 'Posts'
'featured' : 'Featured'
'call-to-action' : 'Call to Action'
'team' : 'Team'
'testimonials' : 'Testimonials'
'services' : 'Services'
'contact' : 'Contact'
'about' : 'About'
'portfolio' : 'Portfolio'
'gallery' : 'Gallery'
'media' : 'Media'
'videos' : 'Videos'
'audio' : 'Audio'
'footer' : 'Footers'
'header' : 'Headers'
*/

// register free block patterns
add_action('rest_api_init', 'dragblock_patterns_rest_api_init', 1);

// dragblock_patterns_rest_api_init();
function dragblock_patterns_rest_api_init()
{
	// $DEBUG = true;
	$DEBUG = false;


	$pattern_duration__timer = 60 * 60 * 24; // refresh every day    
	$pattern_key__timer = DRAGBLOCK_K_PATTERN_CACHE . '--time';

	// debug line
	// enable these lines to test
	if ($DEBUG) {
		delete_transient(DRAGBLOCK_K_PATTERN_CACHE);
		delete_transient($pattern_key__timer);
		// var_dump(WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( ));
		// die();
	}

	$pattern_data = get_transient(DRAGBLOCK_K_PATTERN_CACHE);
	$pattern_time = get_transient($pattern_key__timer);
	// $api_url = 'http://localhost/sneeit/api/validation/'; // for testing purposes
	$api_url = 'https://sneeit.com/api/validation/';

	// var_dump('Pattern after reading' , $pattern_data);
	// if no cache or testing
	// we get from the remote repository
	if (!$pattern_time || DRAGBLOCK_IS_LOCAL) {

		// Make the POST request        
		$response = wp_remote_request($api_url, array(
			'body' => array(
				'get' => 'patterns',
			),
			'method' => 'POST',
		));

		if (!is_wp_error($response)) {
			$pattern_data = json_decode(wp_remote_retrieve_body($response), true);
			if ($DEBUG) {
				// var_dump($response['body'], $api_url, $pattern_data);
				// die();
			}
			if (!empty($pattern_data['error']) || empty($pattern_data['patterns'])) {
				// restore back the pattern cache if getting error
				$pattern_data = get_transient(DRAGBLOCK_K_PATTERN_CACHE);
			} else {
				// the pattern data should be saved forever just in case 
				// we can't load data from the server
				set_transient(DRAGBLOCK_K_PATTERN_CACHE, $pattern_data);
				set_transient($pattern_key__timer, true, $pattern_duration__timer);
			}
		} else {
			if ($DEBUG) {
				// var_dump('pattern server get error', $response->get_error_message());
				// die();				
			}
		}
	}

	if ($DEBUG) {
		// var_dump('Pattern out of if' , $pattern_data);
		// die();
	}

	// we check pattern data here so if it does not 
	if (!$pattern_data) {
		// don't add theme support at this time because they are not beautiful
		// add_theme_support('core-block-patterns');
		return;
	}

	// register pattern categories based on the pattern categories of Sneeit
	// var_dump($pattern_data['categories']); die();
	if (!empty($pattern_data['categories'])) {
		foreach ($pattern_data['categories'] as $cat_slug => $cat_props) {
			$cat_slug = trim(explode('-', $cat_slug)[0]);
			if ($DEBUG) {
				// 'posts-patterns'
				// 'forms-patterns'
				// 'footer-patterns'
				// 'header-patterns'
				// 'misc-patterns'
				// '404-patterns'
				var_dump($cat_slug);
			}

			register_block_pattern_category($cat_slug, $cat_props);
		}
		if ($DEBUG) {
			// die();
		}
	}

	// add pattens to each categories
	foreach ($pattern_data['patterns'] as $pattern_id => $pattern) {
		if (empty($pattern['name']) || empty($pattern['categories'])) {
			continue;
		}
		for ($i = 0; $i < count($pattern['categories']); $i++) {
			$pattern['categories'][$i] = trim(explode('-', $pattern['categories'][$i])[0]);
		}
		if ($DEBUG) {
			var_dump($pattern);
		}
		if (strpos($pattern['name'], 'dragblock/') === false) {
			$pattern['name'] = 'dragblock/' . $pattern['name'];
		}
		register_block_pattern($pattern['name'], $pattern);
	}
	if ($DEBUG) {
		die();
	}
}
