<?php

/**
 * Register FLEX Template Meta Box
 */
function flothemes_content_width_metabox_add_meta_box()
{
	add_meta_box(
		'flothemes_content_width_metabox_meta_box',
		'<i class="fgt-icon-width"></i>'.__('Content Width', 'flothemes'),
		'flothemes_content_width_metabox_metabox_callback',
		[
			// 'gallery',
			// 'post',
			'fgt_g_template',
			'fgt_p_template'
		],
		'side',
		'core',
		array(
			'__back_compat_meta_box' => FALSE,
		)
	);
}
add_action('add_meta_boxes', 'flothemes_content_width_metabox_add_meta_box');

/**
 * FLEX Template Metabox Callback
 */
function flothemes_content_width_metabox_metabox_callback($post)
{
	$value = get_post_meta($post->ID, '_flothemes_content_width_metabox_field', TRUE);
	$value = $value ? $value : 800;

	$useGlobal = get_post_meta($post->ID, '_flothemes_use_global_content_width_metabox_field', TRUE);
	$useGlobal = $useGlobal === "on" || $useGlobal === ''; ?>

	<input
		class="fgt-metaboxes-switch"
		id="flothemes_use_global_content_width_metabox_field"
		name="flothemes_use_global_content_width_metabox_field"
		data-title="<?php _e('Use Global', 'flothemes') ?>"
		value="true"
		type="checkbox" <?php if ($useGlobal) {
		echo 'checked';
	} ?>
	/>
	<input
		id="flothemes_content_width_metabox_field"
		name="flothemes_content_width_metabox_field"
		class="fgt-metaboxes-slider"
		data-condition-checked-false="flothemes_use_global_content_width_metabox_field"
		type="number"
		data-title="Content Width"
		value="<?php echo $value ?>"
		unit="PX"
		min='400'
		max='1280'
	/>
	<?php
}

/**
 * Save FLEX Template Metabox
 */
function flothemes_content_width_metabox_save_postdata($post_id)
{
	if (array_key_exists('flothemes_content_width_metabox_field', $_POST)) {
		update_post_meta($post_id, '_flothemes_content_width_metabox_field', $_POST['flothemes_content_width_metabox_field']);
	}

	if (array_key_exists('flothemes_use_global_content_width_metabox_field', $_POST) && $_POST['flothemes_use_global_content_width_metabox_field'] === 'true') {
		update_post_meta($post_id, '_flothemes_use_global_content_width_metabox_field', 'on');
	} else {
		update_post_meta($post_id, '_flothemes_use_global_content_width_metabox_field', 'off');
	}
}
add_action('save_post', 'flothemes_content_width_metabox_save_postdata');

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

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

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