<?php

/**
 * Register FLEX Header Meta Box
 */
function flothemes_header_metabox_add_meta_box()
{
	global $public_post_types;

	add_meta_box(
		'flothemes_header_metabox_meta_box',
		'<i class="fgt-icon-meta-header"></i>'.__('Header', 'flothemes'),
		'flothemes_header_metabox_metabox_callback',
		$public_post_types,
		'side',
		'core',
		array(
			'__back_compat_meta_box' => FALSE,
		)
	);
}
add_action('add_meta_boxes', 'flothemes_header_metabox_add_meta_box');

/**
 * FLEX Header Metabox Callback
 */
function flothemes_header_metabox_metabox_callback($post)
{
	$value = get_post_meta($post->ID, '_flothemes_header_metabox_field', TRUE);
	$useGlobal = get_post_meta($post->ID, '_flothemes_use_global_header_metabox_field', TRUE);
	$mergeWithContent = get_post_meta($post->ID, '_flothemes_merge_with_content_header_metabox_field', TRUE);
	$elements_color = get_post_meta($post->ID, '_flothemes_elements_color_header_metabox_field', TRUE);
	if (!$elements_color) {
		$elements_color = "#000";
	}

	$headers = get_posts(array(
		'numberposts' => -1,
		'post_type'   => 'fgt_header',
		'suppress_filters' => TRUE,
	)); ?>

	<input class="fgt-metaboxes-switch" id="flothemes_use_global_header_metabox_field" name="flothemes_use_global_header_metabox_field" data-title="<?php _e('Use Global', 'flothemes') ?>" value="true" type="checkbox" <?php if ($useGlobal === "true" || $useGlobal === "") {
		echo 'checked';
	} ?> />
	<select class="fgt-metaboxes-select" name="flothemes_header_metabox_field" id="flothemes_header_metabox_field" data-condition-checked-false="flothemes_use_global_header_metabox_field">
		<?php foreach ($headers as $header) { ?>
			<option value="<?php echo $header->ID; ?>" <?php
																									if ((int) $value === $header->ID) {
																										echo 'selected';
																									} ?>>
				<?php echo $header->post_title; ?>
			</option>
		<?php } ?>
		<option value="DO_NOT_USE_HEADER" <?php if ($value === 'DO_NOT_USE_HEADER') {
																										echo 'selected';
																									} ?>>
			Remove Header
		</option>
	</select>
	<input class="fgt-metaboxes-switch" id="flothemes_merge_with_content_header_metabox_field" name="flothemes_merge_with_content_header_metabox_field" data-title="<?php _e('Merge With Content', 'flothemes') ?>" value="true" type="checkbox" <?php if ($mergeWithContent === "true") {
																										echo 'checked';
																									} ?> />
	<input class="fgt-metaboxes-cp" id="flothemes_elements_color_header_metabox_field" name="flothemes_elements_color_header_metabox_field" value="<?php esc_attr_e($elements_color); ?>" data-title="Elements Color" data-condition-checked-true="flothemes_merge_with_content_header_metabox_field" type="text" />
<?php
}

/**
 * Save FLEX Header Metabox
 */
function flothemes_header_metabox_save_postdata($post_id)
{
	if (empty($_POST['flothemes_use_meta_flag']) || $_POST['flothemes_use_meta_flag'] !== 'flo-meta-flag') {
		return;
	}

	if (array_key_exists('flothemes_header_metabox_field', $_POST)) {
		update_post_meta($post_id, '_flothemes_header_metabox_field', $_POST['flothemes_header_metabox_field']);
	}

	if (array_key_exists('flothemes_use_global_header_metabox_field', $_POST)) {
		update_post_meta($post_id, '_flothemes_use_global_header_metabox_field', $_POST['flothemes_use_global_header_metabox_field']);
	} else {
		update_post_meta($post_id, '_flothemes_use_global_header_metabox_field', 'false');
	}

	if (array_key_exists('flothemes_merge_with_content_header_metabox_field', $_POST)) {
		update_post_meta($post_id, '_flothemes_merge_with_content_header_metabox_field', $_POST['flothemes_merge_with_content_header_metabox_field']);
	} else {
		update_post_meta($post_id, '_flothemes_merge_with_content_header_metabox_field', FALSE);
	}

	if (array_key_exists('flothemes_elements_color_header_metabox_field', $_POST)) {
		update_post_meta($post_id, '_flothemes_elements_color_header_metabox_field', $_POST['flothemes_elements_color_header_metabox_field']);
	}
}
add_action('save_post', 'flothemes_header_metabox_save_postdata');

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

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

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