<?php

/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across the
 * public-facing side
 *
 * @link       https://www.facebook.com/nikita.flo.735
 * @since      1.0.0
 *
 * @package    FLO_PUBLIC
 */

/**
 * The core plugin class.
 *
 * This is used to define internationalization, public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    FLO_PUBLIC
 * @author     Nichita Sinitin <nikita@flosites.net>
 */

require "generic-typography.php";
require "form-style.php";

class FLO_PUBLIC
{
	/**
	 * The unique identifier of this plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $theme_name    The string used to uniquely identify this plugin.
	 */
	protected $theme_name;

	/**
	 * The current version of the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $version    The current version of the plugin.
	 */
	protected $version;

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, define the locale, and set the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function __construct()
	{
		if (defined('FLEX_GUTENBERG_THEME_VERSION')) {
			$this->version = FLEX_GUTENBERG_THEME_VERSION;
		} else {
			$this->version = '1.0.0';
		}

		$this->loader_uri = get_template_directory_uri() . '/dist/';
		$this->assets_uri = get_template_directory_uri() . '/assets/';
		$this->loader_path = dirname(__FILE__);

		$this->define_public_hooks();
		$this->loader_path = dirname(__FILE__);
	}

	private function define_public_hooks()
	{
		add_action('wp_enqueue_scripts', array($this, 'enqueue_public_styles'));
		add_action('wp_enqueue_scripts', array($this, 'enqueue_public_scripts'));

		// Stop emoji, let's not support that
		remove_action('wp_head', 'print_emoji_detection_script', 7);
		remove_action('wp_print_styles', 'print_emoji_styles');

		$loader_path = $this->loader_path;
		include $loader_path . '/render/class-fgt-render.php';
	}

	public function enqueue_public_styles()
	{
		$loader_uri = $this->loader_uri;

		wp_dequeue_style('flex_gutenberg_theme-style');

		$globalSk = get_option('flex_active_stylekit_name');
		$allSkData = get_option('flex_all_stylekits_obj');

		// wp_register_style('fgt-public', false);
		// wp_enqueue_style('fgt-public');

		if (isset($globalSk) && isset($allSkData[$globalSk])) {
			// TODO: add verification for stylekits
			wp_enqueue_style('fgt-public', $loader_uri . 'public.min.css', array(), $this->version, 'all');
			// $themes_public_css = file_get_contents(get_template_directory() . '/dist/public.min.css');
			add_filter('body_class', function ($classes) {
				return array_merge($classes, array('flex-stylekit-present'));
			});
		} else {
			// $themes_public_css = file_get_contents(get_template_directory() . '/dist/publicdefault.min.css');
			wp_enqueue_style('fgt-public', $loader_uri . 'publicdefault.min.css', array(), $this->version, 'all');
			wp_enqueue_style('flo-default', 'https://fonts.googleapis.com/css2?family=Libre+Franklin:wght@100;400&family=Nanum+Myeongjo:wght@700&display=swap', array(), '1.0.0', 'all');
		}

		wp_enqueue_style('fgt-public-icons', get_template_directory_uri() . '/flo-app/public/assets/icomoon/style.css', [], $this->version, 'all');
		// wp_add_inline_style('fgt-public', $themes_public_css);

		add_action('wp_head', 'fgt_render_gt');
		add_action('wp_head', 'fgt_render_form_styles');
	}

	public function enqueue_public_scripts()
	{
		$loader_uri = $this->loader_uri;
		wp_enqueue_script('fgt-public', $loader_uri . 'public.min.js', array('jquery'), $this->version, TRUE);

		$theme_url = get_template_directory_uri();
		$data = [
			'themeUrl' => $theme_url,
			'assetUrl' => $theme_url . '/dist/'
		];

		wp_localize_script('fgt-public', 'fgtPublicData', $data);

		if (is_singular() && comments_open() && get_option('thread_comments')) {
			wp_enqueue_script('comment-reply');
		}
	}
}

new FLO_PUBLIC();
