<?php
/**
 * Gutenberg Editor CSS
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package     Logtik
 * @author      Logtik
 * @copyright   Copyright (c) 2019, Logtik
 * @link        http://wedesignthemes.com/
 * @since       Logtik 1.0
 */

if ( ! class_exists( 'Gutenberg_Editor_CSS' ) ) :

	/**
	 * Admin Helper
	 */
	class Gutenberg_Editor_CSS {

		function __construct() {

			add_action( 'admin_enqueue_scripts', array( $this, 'logtik_backend_editor_fonts' ) );
			add_action( 'current_screen', array( $this,  'logtik_current_screen_hook' ) );
			add_action( 'enqueue_block_editor_assets', array( $this, 'logtik_backend_editor_styles' ) );
			add_filter( 'tiny_mce_before_init', array( $this, 'logtik_theme_editor_dynamic_styles' ) );
		}

		function logtik_backend_editor_fonts() {

			if ( !class_exists( 'Kirki' ) ) {
				LOGTIK_Kirki::admin_enqueue_fonts();
			}
		}

		function logtik_current_screen_hook( $current_screen ) {

			if ( 'post' == $current_screen->base ) {

				$urls = LOGTIK_Kirki::admin_editor_fonts_url();
				add_editor_style( $urls );
				$custom_font_css = logtik_styles_custom_font();
				if( !empty( $custom_font_css ) ) {
					add_editor_style( $custom_font_css );
				}
				add_editor_style( 'css/editor-style.css' );
			}
		}

		function logtik_theme_editor_dynamic_styles( $mceInit ) {

			$styles = '';

			$fonts = cs_get_option('custom_font_fields');
			if( !empty ( $fonts ) && count( $fonts ) > 0 ){
				foreach( $fonts as $font ):
					$styles .= '@font-face {';
						$styles .= "font-family: '{$font['custom_font_name']}';";
						$styles .= "src: url('{$font['custom_font_woof']}') format('woff'),";
							$styles .= "url('{$font['custom_font_woof2']}') format('woff2');";
						$styles .= 'font-weight: normal;';
						$styles .= 'font-style: normal;';
					$styles .= '}';
				endforeach;
			}

			if ( isset( $mceInit['content_style'] ) ) {
				$mceInit['content_style'] .= ' ' . $styles . ' ';
			} else {
				$mceInit['content_style'] = $styles . ' ';
			}

			return $mceInit;
		}

		function logtik_backend_editor_styles() {

			if ( !class_exists( 'Kirki' ) ) {
				LOGTIK_Kirki::admin_enqueue_fonts();
			}

			wp_enqueue_style( 'logtik-gutenberg', get_theme_file_uri('/css/admin-gutenberg.css'), false, LOGTIK_THEME_VERSION, 'all' );
			wp_add_inline_style( 'logtik-gutenberg', logtik_styles_custom_font() );
		}
	}

	new Gutenberg_Editor_CSS();

endif;