<?php

/**
 * Block Editor (Gutenberg) related functionality
 *
 * @package Kubelio\Core
 * @since 1.0.0
 */

namespace Kubelio\Core;

if (! defined('ABSPATH')) {
	exit;
}

/**
 * Block Class
 */
class Block {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_filter('admin_body_class', array($this, 'admin_classes'));
		add_filter('wp_theme_json_data_theme', array($this, 'customizer_theme_json'));
		add_action('init', array($this, 'register_block_pattern_categories'));
		add_action('init', array($this, 'register_block_styles'));
	}

	/**
	 * Add admin css classes
	 *
	 * @param string $classes Space-separated list of admin body classes.
	 * @return string
	 */
	public function admin_classes($classes) {
		$classes .= ' kubelio-root kubelio-default-scheme';
		return $classes;
	}

	/**
	 * Get color palette from customizer
	 *
	 * @param \WP_Theme_JSON_Data $theme_json The theme JSON data.
	 * @return \WP_Theme_JSON_Data
	 */
	public function customizer_theme_json($theme_json) {
		$theme_data      = wp_json_file_decode(get_stylesheet_directory() . '/theme.json', array('associative' => true));
		$default_palette = $theme_data['settings']['color']['palette'] ?? array();

		$primary_color = get_theme_mod('color_primary', kubelio_get_theme('color')['primary']);

		$custom_palette = array(
			array(
				'slug'  => 'primary',
				'color' => $primary_color,
				'name'  => 'Primary',
			),
			array(
				'slug'  => 'primary-light',
				'color' => kubelio_generate_hex_color($primary_color, 15, 'light'),
				'name'  => 'Primary Light',
			),
			array(
				'slug'  => 'primary-dark',
				'color' => kubelio_generate_hex_color($primary_color, 15, 'dark'),
				'name'  => 'Primary Dark',
			),
			array(
				'slug'  => 'heading',
				'color' => get_theme_mod('color_heading', kubelio_get_theme('color')['heading']),
				'name'  => 'Heading',
			),
			array(
				'slug'  => 'body',
				'color' => get_theme_mod('color_body', kubelio_get_theme('color')['body']),
				'name'  => 'body',
			),
			array(
				'slug'  => 'meta',
				'color' => get_theme_mod('color_meta', kubelio_get_theme('color')['meta']),
				'name'  => 'Meta',
			),
			array(
				'slug'  => 'text-link',
				'color' => get_theme_mod('color_link', kubelio_get_theme('color')['text-link']),
				'name'  => 'Link',
			),
			array(
				'slug'  => 'text-link-active',
				'color' => get_theme_mod('color_link_active', kubelio_get_theme('color')['text-link-active']),
				'name'  => 'Link Active',
			),
			array(
				'slug'  => 'background',
				'color' => get_theme_mod('color_background', kubelio_get_theme('color')['background']),
				'name'  => 'Background',
			),
			array(
				'slug'  => 'surface',
				'color' => get_theme_mod('color_surface', kubelio_get_theme('color')['surface']),
				'name'  => 'Surface',
			),
			array(
				'slug'  => 'outline',
				'color' => get_theme_mod('color_border', kubelio_get_theme('color')['outline']),
				'name'  => 'Outline',
			),
		);

		$palette_by_slug = array();

		foreach ($default_palette as $color) {
			if (isset($color['slug'])) {
				$palette_by_slug[$color['slug']] = $color;
			}
		}

		foreach ($custom_palette as $color) {
			$palette_by_slug[$color['slug']] = array_merge($palette_by_slug[$color['slug']] ?? array(), $color);
		}

		$new_data = array(
			'version'  => 3,
			'settings' => array(
				'color'      => array(
					'palette' => array_values($palette_by_slug),
				),
				'custom'     => array(
					'color'         => array(
						'overlay' => get_theme_mod('color_backdrop', kubelio_get_theme('color')['overlay']),
						'info'    => get_theme_mod('color_info', kubelio_get_theme('color')['info']),
						'success' => get_theme_mod('color_success', kubelio_get_theme('color')['success']),
						'warning' => get_theme_mod('color_warning', kubelio_get_theme('color')['warning']),
						'danger'  => get_theme_mod('color_danger', kubelio_get_theme('color')['danger']),
					),
					'button'        => array(
						'color'      => get_theme_mod('button_color', kubelio_get_theme('button')['primary']['color']),
						'background' => get_theme_mod('button_background', kubelio_get_theme('button')['primary']['background']),
					),
					'button-action' => array(
						'color'      => get_theme_mod('button_color', kubelio_get_theme('button')['primary']['color']),
						'background' => get_theme_mod('button_background', kubelio_get_theme('button')['primary']['background']),
					),
				),
				'typography' => array(
					'fontFamilies' => array(
						array(
							'slug'       => 'heading',
							'fontFamily' => get_theme_mod('typography_heading', kubelio_get_theme('typography')['heading'])['font-family'] ?? kubelio_get_theme('typography')['heading']['font-family'],
							'name'       => 'Heading',
						),
						array(
							'slug'       => 'body',
							'fontFamily' => get_theme_mod('typography_body', kubelio_get_theme('typography')['body'])['font-family'] ?? kubelio_get_theme('typography')['body']['font-family'],
							'name'       => 'Body',
						),
					),
				),
			),
		);

		return $theme_json->update_with($new_data);
	}

	/**
	 * Register custom block patterns category
	 */
	public function register_block_pattern_categories() {

		register_block_pattern_category(
			'kubelio',
			array(
				'label' => esc_html__('Kubelio', 'kubelio'),
			)
		);

		register_block_pattern_category(
			'hero',
			array(
				'label' => esc_html__('Hero', 'kubelio'),
			)
		);

		register_block_pattern_category(
			'page',
			array(
				'label' => esc_html__('Page', 'kubelio'),
			)
		);
	}

	/**
	 * Register custom block styles
	 */
	public function register_block_styles() {

		register_block_style(
			'core/navigation',
			array(
				'name'  => 'underline',
				'label' => esc_html__('Underline', 'kubelio'),
			)
		);

		register_block_style(
			'core/post-terms',
			array(
				'name'  => 'colored',
				'label' => esc_html__('Colored Pill', 'kubelio'),
			)
		);

		register_block_style(
			'core/post-terms',
			array(
				'name'  => 'glass',
				'label' => esc_html__('Glass', 'kubelio'),
			)
		);

		register_block_style(
			'core/post-terms',
			array(
				'name'  => 'outline',
				'label' => esc_html__('Outline Pill', 'kubelio'),
			)
		);

		register_block_style(
			'core/post-terms',
			array(
				'name'  => 'dot-separator',
				'label' => esc_html__('Dot Separator', 'kubelio'),
			)
		);

		register_block_style(
			'core/categories',
			array(
				'name'  => 'list-underline',
				'label' => esc_html__('Underline', 'kubelio'),
			)
		);

		register_block_style(
			'core/categories',
			array(
				'name'  => 'hastag',
				'label' => esc_html__('Hashtag', 'kubelio'),
			)
		);

		register_block_style(
			'core/post-title',
			array(
				'name'  => 'underline-hover',
				'label' => esc_html__('Underline On Hover', 'kubelio'),
			)
		);

		register_block_style(
			'core/details',
			array(
				'name'  => 'boxed',
				'label' => esc_html__('Boxed', 'kubelio'),
			)
		);

		register_block_style(
			'core/accordion',
			array(
				'name'  => 'panel',
				'label' => esc_html__('Panel', 'kubelio'),
			)
		);

		register_block_style(
			'core/image',
			array(
				'name'  => 'circle',
				'label' => esc_html__('Circle', 'kubelio'),
			)
		);
	}
}
