<?php
/**
 * Blocks Setup
 * This file is loaded using 'after_setup_theme' hook at priority 10
 */

/* === WordPress Blocks === */

/** Add slightly more opinionated styles for the front end **/
add_theme_support( 'wp-block-styles' );

/** Responsive embedded content **/
add_theme_support( 'responsive-embeds' );
/** Remove custom background support - instead use colors in theme.json **/
remove_theme_support( 'custom-background' );


/**
 * Color Pallete
 * Add accent colors to Block Pallete
 */
add_filter( 'wp_theme_json_data_theme', 'hoot_wpblock_theme_json_data' );
if ( ! function_exists( 'hoot_wpblock_theme_json_data' ) ) :
function hoot_wpblock_theme_json_data( $theme_json ) {
	if ( ! function_exists( 'hoot_get_mod' ) )
		return $theme_json;
	$process = array(
		'accent'            => 'accent_color',
		'accent-font'       => 'accent_font',
		'hootlink'          => 'link_color',
		'hootlink-hover'    => 'link_hover_color',
		'button'            => 'button_color',
		'button-font'       => 'button_font',
	);
	$data = $theme_json->get_data();
	if ( is_array( $data )
		&& ! empty( $data['settings'] ) && is_array( $data['settings'] )
		&& ! empty( $data['settings']['color'] ) && is_array( $data['settings']['color'] )
		&& ! empty( $data['settings']['color']['palette'] ) && is_array( $data['settings']['color']['palette'] )
	) {
		$isthemearray = ( !empty( $data['settings']['color']['palette']['theme'] ) && is_array( $data['settings']['color']['palette']['theme'] ) ) ? true : false;
		$themepalette = $isthemearray ? $data['settings']['color']['palette']['theme'] : $data['settings']['color']['palette'];
		foreach ( $themepalette as $index => $color ) {
			if ( empty( $color['slug'] ) )
				continue;
			foreach ( $process as $slug => $modname ) {
				if ( $color['slug'] === $slug ) {
					$modval = hoot_get_mod( $modname );
					if ( ! empty( $modval ) )
						$themepalette[$index]['color'] = $modval;
					continue 2;
				}
			}
		}
		if ( $isthemearray )
			$data['settings']['color']['palette']['theme'] = $themepalette;
		else
			$data['settings']['color']['palette'] = $themepalette;
	}
	if ( function_exists( 'hoot_themeradii' ) ) {
		$radii = hoot_themeradii();

		if ( is_array( $data )
			&& ! empty( $data['settings'] ) && is_array( $data['settings'] )
			&& ! empty( $data['settings']['custom'] ) && is_array( $data['settings']['custom'] )
			&& ! empty( $data['settings']['custom']['radius'] ) && is_array( $data['settings']['custom']['radius'] )
		) {
			foreach ( $data['settings']['custom']['radius'] as $radiustype => $radval ) {
				if ( isset( $radii[ $radiustype ] ) ) {
					$data['settings']['custom']['radius'][ $radiustype ] = $radii[ $radiustype ];
				}
			}
		}

		$process = array(
			'blocks' => array(
				'core/image' => 'limit20',
				'core/gallery' => 'limit20',
				'core/cover' => 'limit12',
			),
			'elements' => array(
				'button' => 'button',
				'caption' => 'limit12',
			),
		);
		if ( is_array( $data ) && ! empty( $data['styles'] ) && is_array( $data['styles'] ) ) {
			foreach ( $process as $type => $processarray ) {
				if ( ! empty( $data['styles'][$type] ) && is_array( $data['styles'][$type] ) ) {
					foreach ( $processarray as $slug => $radiustype ) {
						if (
							isset( $radii[ $radiustype ] )
							&& ! empty( $data['styles'][$type][$slug] ) && is_array( $data['styles'][$type][$slug] )
							&& ! empty( $data['styles'][$type][$slug]['border'] ) && is_array( $data['styles'][$type][$slug]['border'] )
							&& isset( $data['styles'][$type][$slug]['border']['radius'] )
						) {
							$data['styles'][$type][$slug]['border']['radius'] = $radii[ $radiustype ];
						}
					}
				}
			}
		}
	}

	return new WP_Theme_JSON_Data( $data );
}
endif;


/**
 * Block Styles
 */

add_action( 'init', 'hoot_wpblock_styles' );
if ( ! function_exists( 'hoot_wpblock_styles' ) ) :
function hoot_wpblock_styles() {
	register_block_style( 'core/image', array(
		'name'  => 'hoot-image-border',
		'label' => esc_html__( 'Border', 'magazine-booster' ),
	) );
	register_block_style( 'core/image', array(
		'name'  => 'hoot-image-frame',
		'label' => esc_html__( 'Frame', 'magazine-booster' ),
	) );
	register_block_style( 'core/list', array(
		'name'  => 'hoot-checklist',
		'label' => __( 'Checkmark', 'magazine-booster' ),
		'inline_style' =>	'div ul.is-style-hoot-checklist { list-style-type: "\2713"; }' .
							'div ul.is-style-hoot-checklist li { padding-inline-start: 1ch; }',
	) );
	register_block_style( 'core/heading', array(
		'name'  => 'hoot-headblock1',
		'label' => __( 'Style 1', 'magazine-booster' ),
	) );
	register_block_style( 'core/heading', array(
		'name'  => 'hoot-headblock2',
		'label' => __( 'Style 2', 'magazine-booster' ),
	) );
	register_block_style( 'core/heading', array(
		'name'  => 'hoot-headblock3',
		'label' => __( 'Style 3', 'magazine-booster' ),
	) );
}
endif;



/**
 * Add Stylesheets
 */

// Load after main stylesheet (and hootkit if available), but before child theme's stylesheet (and child hootkit)
add_action( 'wp_enqueue_scripts', 'hoot_wpblock_assets', 16 );
if ( ! function_exists( 'hoot_wpblock_assets' ) ) :
function hoot_wpblock_assets(){
	$style_uri = hoot_locate_style( 'include/blocks/wpblocks' );
	wp_enqueue_style( 'hoot-wpblocks', $style_uri, array(), hoot_data()->template_version );
}
endif;

// Set dynamic css handle to hoot-wpblocks
add_filter( 'hoot_style_builder_inline_style_handle', 'hoot_wpblock_dynamic_css_handle', 4 );
if ( ! function_exists( 'hoot_wpblock_dynamic_css_handle' ) ) :
function hoot_wpblock_dynamic_css_handle(){ return 'hoot-wpblocks'; }
endif;
// Editor stylesheet (HBS loads @10)
add_action( 'enqueue_block_editor_assets', 'hoot_wpblock_editor_assets', 12 );
if ( ! function_exists( 'hoot_wpblock_editor_assets' ) ) :
function hoot_wpblock_editor_assets(){
	// This is loaded in only Backend...
	$style_uri = hoot_locate_style( 'include/blocks/wpblocks-editor' );
	wp_enqueue_style( 'hoot-wpblocks-editor', $style_uri, array(), hoot_data()->template_version );
}
endif;
// Editor stylesheet - This file is loaded using 'after_setup_theme' hook at priority 10
add_editor_style( 'include/admin/css/editor-style.css' );
