<?php

// the old ones
// to common that cannot distinguish from other syntaxes
define('COLOR_VAR_V0_START', '{c=');
define('COLOR_VAR_V0_ALPHA_SEP', '@');
define('COLOR_VAR_V0_END', '}');

// the new ones and getting sanitized by some database settings 
// (that does not support unicode)
// ▶ '\U0026#x25b6'
// ▲ '\U0026#x25b2'
// ━ '\U0026#x2501'
// 🄰 '\U0026#xd83c'
// ▼ '\U0026#x25bc'
// ◀ '\U0026#x25c0'
// ⮕ '\U0026#x2b95'
// ⬇ '\U0026#x2b07'
// ⬅ '\U0026#x2b05'
define('COLOR_VAR_V1_START', '▶c=');
define('COLOR_VAR_V1_ALPHA_SEP', '▲━🄰');
define('COLOR_VAR_V1_BACKUP_SEP', '▼c=');
define('COLOR_VAR_V1_END', 'c◀');
define('GRADIENT_VAR_V1_START', '⮕g=');
define('GRADIENT_VAR_V1_BACKUP_SEP', '⬇g=');
define('GRADIENT_VAR_V1_END', 'g⬅');

// usable characters !@#$%^*(){}[]|~`',.?/-_=+
// unusable characters &\"
// avoid characters: <>
// we decided to use shortcode as they still has performance and character escaping issues
// the best option is to use CSS comments instead so we can even display the value directly without escaping
// /*@@dragBlockColorStart*/var(--wp--preset--color--primary,/*backup*/#FFFFFF/*@@dragBlockColorEnd*/)
// /*@@dragBlockColorStart*/var(--wp--preset--color--primary,/*backup*/#ffffffAA/*hasAlphaAppender=AA/*@@dragBlockColorEnd*/)
// when we use AlphaAppender, we get AlphaAppender and replace the whole with rawColor+Alpha

// /*@@dragBlockGradientStart*/var(--wp--preset--gradient--NAME,/*backupGradient*/FULL_GRADIENT_VALUE/*@@dragBlockGradientEnd*/);
// /*@@dragBlockGradientStart*/var(--wp--preset--gradient--vivid-cyan-blue-to-vivid-purple,/*backupGradient*/linear-gradient(135deg, rgb(11, 25, 92) 0%, rgb(0, 208, 130) 100%)/*@@dragBlockGradientEnd*/);

// we add full color at the backup (including the alpha) so even when we cannot parse, 
// the color still display correctly
// we place ) at then end of the ENDMARKER just incase we break the inside content to smaller variables
// we still can end all their comments with */ before give the ')' at the end

define('COLOR_VAR_V2_START',       '/*@@dragBlockColorStart*/var(--wp--preset--color--');
define('COLOR_VAR_V2_BACKUP_SEP',  ',/*backupColor*/');
define('COLOR_VAR_V2_ALPHA_SEP',   '/*hasAlphaAppender=');
define('COLOR_VAR_V2_END',         '/*@@dragBlockColorEnd*/)');

define('GRADIENT_VAR_V2_START',    		'/*@@dragBlockGradientStart*/var(--wp--preset--gradient--');
define('GRADIENT_VAR_V2_BACKUP_SEP',   	',/*backupGradient*/');
define('GRADIENT_VAR_V2_END',      		'/*@@dragBlockGradientEnd*/)');
/**
EDITOR
 */
add_action('enqueue_block_editor_assets', 'dragblock_editor_init_editor_assets');
function dragblock_editor_init_editor_assets()
{

	// var_dump($font_families); die();
	// init font families
	$font_families = get_option(DRAGBLOCK_FONT_LIB_SLUG, array());
	$font_names = [
		"Arial",
		"Arial Narrow",
		"Arial Black",
		"Book Antiqua",
		"Cambria",
		"Century Gothic",
		"Courier New",
		"Cursive",
		"Franklin Gothic Medium",
		"Georgia",
		"Gill Sans",
		"Impact",
		"Lucida Console",
		"Lucida Sans",
		"Monospace",
		"Tahoma",
		"Times New Roman",
		"Trebuchet MS",
		"Verdana"
	];
	foreach ($font_families as $family) {
		if (empty($family['fontFamily'])) {
			continue;
		}
		array_unshift($font_names, $family['fontFamily']);
	}

	dragblock_enqueue(DRAGBLOCK_EDITOR_INIT_SLUG . '-animate', 'assets/css/animate.min.css');
	//dragblock_enqueue('tocss', 'assets/plugins/tocss/tocss.js');
	dragblock_enqueue(DRAGBLOCK_EDITOR_INIT_SLUG,  'build/applications/editor-init/client/editor/index.js', array('jquery'));
	dragblock_enqueue(DRAGBLOCK_EDITOR_INIT_SLUG, 'build/applications/editor-init/client/editor/index.css');

	// Define the data to pass to your script
	$script_data = dragblock_admin_common_inline_init_script(array(
		'fontName' => $font_names,		
		'taxonomies' => array_keys(
			get_taxonomies(
				array(
					'public'   => true,
					'_builtin' => false,
				)
			)
		),
		'postTypes' => array_keys(get_post_types()),
		'themeUri' => get_stylesheet_directory_uri(),
		'pluginUri' => DRAGBLOCK_URL,
		/**
        Don't change this as it saved in thousands of themes
        Used to distinguish between normal taxes and custom taxes
		 */
		'startTaxQueryKey' => DRAGBLOCK_START_TAX_QUERY_KEY,
		'postViewsKey' => DRAGBLOCK_POST_VIEWS_KEY,

		// deprecated but necessary for backwards compatibility
		'colorVarV0Start' => COLOR_VAR_V0_START,
		'colorVarV0AlphaSep' => COLOR_VAR_V0_ALPHA_SEP,
		'colorVarV0End' => COLOR_VAR_V0_END,
		'colorVarV1Start' => COLOR_VAR_V1_START,
		'colorVarV1AlphaSep' => COLOR_VAR_V1_ALPHA_SEP,
		'colorVarV1BackupSep' => COLOR_VAR_V1_BACKUP_SEP, 
		'colorVarV1End' => COLOR_VAR_V1_END,
		'gradientVarV1Start' => GRADIENT_VAR_V1_START,
		'gradientVarV1BackupSep' => GRADIENT_VAR_V1_BACKUP_SEP,
		'gradientVarV1End' => GRADIENT_VAR_V1_END,

		// version 2
		'colorVarV2Start' => COLOR_VAR_V2_START,
		'colorVarV2BackupSep' => COLOR_VAR_V2_BACKUP_SEP, // fallback color
		'colorVarV2AlphaSep' => COLOR_VAR_V2_ALPHA_SEP,		
		'colorVarV2End' => COLOR_VAR_V2_END,
		
		'gradientVarV2Start' => GRADIENT_VAR_V2_START,
		'gradientVarV2BackupSep' => GRADIENT_VAR_V2_BACKUP_SEP, // fallback gradient
		'gradientVarV2End' => GRADIENT_VAR_V2_END,
	));

	// Localize your script with the data
	// keep the name dragBlockEditorInit long for easier searching
	// hanlder will be converted to dragBlockEditorInit
	wp_localize_script(DRAGBLOCK_EDITOR_INIT_SLUG, 'dragBlockEditorInit', $script_data);
}

/**
EDITOR IFRAME
 */
add_action('after_setup_theme', 'dragblock_editor_init_editor_iframe', 100);
function dragblock_editor_init_editor_iframe()
{
	add_editor_style(DRAGBLOCK_URL  .  'assets/css/animate.min.css');

	// if allow users to modify the default styles, then don't need to load this for editor
	// because the custom css will be loaded to the editor automatically via the style.css in theme.json
	if (!DRAGBLOCK_CUSTOM_DEFAULT_STYLE) {
		add_editor_style(DRAGBLOCK_URL . 'build/applications/editor-init/client/front/style-index.css');
	}
}

/**
FRONT PAGES
 */
add_action('wp_enqueue_scripts', 'dragblock_editor_init_front_scripts');
function dragblock_editor_init_front_scripts()
{
	// if allow users to modify the default styles, then just need to load an empty css
	// if (DRAGBLOCK_CUSTOM_DEFAULT_STYLE) {
	//     dragblock_enqueue(DRAGBLOCK_EDITOR_INIT_SLUG, 'build/applications/editor-init/client/index.css');
	// } else {
	//     dragblock_enqueue(DRAGBLOCK_EDITOR_INIT_SLUG, 'build/applications/editor-init/client/style-index.css');
	// }
	dragblock_enqueue(DRAGBLOCK_EDITOR_INIT_SLUG,  'build/applications/editor-init/client/front/index.js');

	if (!DRAGBLOCK_CUSTOM_DEFAULT_STYLE) {
		dragblock_enqueue(DRAGBLOCK_EDITOR_INIT_SLUG, 'build/applications/editor-init/client/front/style-index.css');
	}
}
