<?php

/**
 * Register FLEX Template Meta Box
 */
function flothemes_p_template_metabox_add_meta_box()
{
	add_meta_box(
		'flothemes_p_template_metabox_meta_box',
		'<i class="fgt-icon-meta-template"></i>' . __('Template', 'flothemes'),
		'flothemes_p_template_metabox_metabox_callback',
		['post'],
		'side',
		'core',
		array(
			'__back_compat_meta_box' => FALSE,
		)
	);
}
add_action('add_meta_boxes', 'flothemes_p_template_metabox_add_meta_box');

/**
 * FLEX Template Metabox Callback
 */
function flothemes_p_template_metabox_metabox_callback($post)
{
	$value = get_post_meta($post->ID, '_flothemes_p_template_metabox_field', TRUE);
	$value_use_global = get_post_meta($post->ID, '_flothemes_use_global_p_metabox_field', TRUE);
	//var_dump($value_use_global);


	$templates = get_posts(array(
		'numberposts' => -1,
		'post_type'   => 'fgt_p_template',
		'suppress_filters' => TRUE,
	));


	// $value === '' -> for the new posts
	// $value_use_global === 'on' for existing posts
	$useGlobal = ($value_use_global === 'on' || $value === ''); ?>

	<input class="fgt-metaboxes-switch" id="flothemes_use_global_p_metabox_field" name="flothemes_use_global_p_metabox_field" data-title="<?php _e('Use Global', 'flothemes') ?>" value="true" type="checkbox" <?php if ($useGlobal) {
		echo 'checked';
	} ?> />
	<select id="flothemes_p_template_metabox_field" name="flothemes_p_template_metabox_field" class="fgt-metaboxes-select" data-condition-checked-false="flothemes_use_global_p_metabox_field">
		<!-- <option
			value=""
			<?php if ($value === "") {
		echo 'selected';
	} ?>
		>
			Global Template
		</option> -->
		<?php foreach ($templates as $template) { ?>
			<option value="<?php echo $template->ID; ?>" <?php
																										if ((int) $value === $template->ID) {
																											echo 'selected';
																										} ?>>
				<?php echo $template->post_title; ?>
			</option>
		<?php } ?>
		<!-- <option
			value="false"
			<?php if ($value === 'false') {
																											echo 'selected';
																										} ?>
		>
			Build Custom Layout
		</option> -->
	</select>
<?php
}

/**
 * Save FLEX Template Metabox
 */
function flothemes_p_template_metabox_save_postdata($post_id)
{
	if (array_key_exists('flothemes_p_template_metabox_field', $_POST)) {
		update_post_meta($post_id, '_flothemes_p_template_metabox_field', $_POST['flothemes_p_template_metabox_field']);
	}
	if (array_key_exists('flothemes_use_global_p_metabox_field', $_POST) && $_POST['flothemes_use_global_p_metabox_field'] === 'true') {
		update_post_meta($post_id, '_flothemes_use_global_p_metabox_field', 'on');
	} else {
		update_post_meta($post_id, '_flothemes_use_global_p_metabox_field', '');
	}
}
add_action('save_post', 'flothemes_p_template_metabox_save_postdata');

/**
 * Register FLEX Template Meta Field to Rest API
 */
function flothemes_p_template_metabox_register_meta()
{
	register_meta(
		'post',
		'_flothemes_p_template_metabox_field',
		array(
			'type'			=> 'string',
			'single'		=> TRUE,
			'show_in_rest'	=> TRUE,
		)
	);
}
add_action('init', 'flothemes_p_template_metabox_register_meta');

/**
 * Register FLEX Template Metabox to Rest API
 */
function flothemes_p_template_metabox_api_posts_meta_field()
{
	register_rest_route(
		'flothemes/v1',
		'/update-meta',
		array(
			'methods'  => 'POST',
			'callback' => 'flothemes_p_template_metabox_update_callback',
			'args'     => array(
				'id' => array(
					'sanitize_callback' => 'absint',
				),
			),
			'permission_callback' => '__return_true'
		)
	);
}
add_action('rest_api_init', 'flothemes_p_template_metabox_api_posts_meta_field');

/**
 * FLEX Template REST API Callback for Gutenberg
 */
function flothemes_p_template_metabox_update_callback($data)
{
	return update_post_meta($data['id'], $data['key'], $data['value']);
}
