<?php
/**
 * Plugin Name:       Thim Blocks
 * Description:       Gutenberg blocks library to create WordPress sites in the Gutenberg Block Editor
 * Version:           1.0.4
 * Plugin URI:        https://thimpress.com/thim-blocks
 * Requires at least: 6.x
 * Requires PHP:      7.4
 * Author:            ThimPress
 * Author URI:        https://thimpress.com
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       thim-blocks
 */

namespace ThimPress\ThimBlocks;

use ThimPress\ThimBlocks\Gutenberg\GutenbergHandleMain;
use ThimPress\ThimBlocks\Gutenberg\BlockStyleHandle;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

final class Thim_Blocks {
	protected static $instance        = null;
	const PLUGIN_FILE                 = __FILE__;
	public static $version            = '';
	public static $plugin_dir         = '';
	public static $plugin_url         = '';
	public static $plugin_basename    = '';
	public static $plugin_folder_name = '';
	public static $dir_assets_dist    = '';
	public static $url_assets_dist    = '';

	private function __construct() {
		try {
			$this->prepare_before_handle();
			$this->includes();
			$this->hooks();
		} catch ( Throwable $e ) {
			error_log( 'Thim Blocks initialization error: ' . $e->getMessage() );
		}
	}

	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Prepare before handle.
	 * 1.set constant.
	 * 2.Get default time limit of server.
	 * 3.Update version of LP undefined.
	 *
	 * @return void
	 * @since 1.0.0
	 * @version 1.0.0
	 */
	public function prepare_before_handle() {
		$plugin_info              = $this->get_plugin_info();
		self::$version            = $plugin_info['Version'] ?? '1.0.3';
		self::$plugin_dir         = plugin_dir_path( self::PLUGIN_FILE );
		self::$plugin_url         = plugin_dir_url( self::PLUGIN_FILE );
		self::$plugin_basename    = plugin_basename( self::PLUGIN_FILE );
		self::$plugin_folder_name = dirname( self::$plugin_basename );
		self::$dir_assets_dist    = self::$plugin_dir . 'assets/dist';
		self::$url_assets_dist    = self::$plugin_url . 'assets/dist';
	}

	/**
	 * Get plugin info.
	 *
	 * @return array
	 */
	public function get_plugin_info(): array {
		require_once ABSPATH . 'wp-admin/includes/plugin.php';

		$default_headers = array(
			'Version'    => 'Version',
			'TextDomain' => 'Text Domain',
		);

		return get_file_data( self::PLUGIN_FILE, $default_headers, 'plugin' );
	}

	protected function includes() {
		include_once 'vendor/autoload.php';
	}

	public function hooks() {
		add_action( 'init', array( $this, 'main_handle' ), 9 );
		// Hook into 'init' with priority 9 so our blocks register before certain other hooks.

		// Enable SVG upload for blocks that use SVG icons
		add_filter( 'upload_mimes', array( $this, 'thim_enable_upload_svg' ) );

		require_once self::$plugin_dir . 'inc/Gutenberg/BlockStyleHandle.php';
		new BlockStyleHandle();
	}

	public function main_handle() {
		$this->load_plugin_text_domain();
		$this->register_icon_font();

		$gutenbergHandle = GutenbergHandleMain::instance();
		$gutenbergHandle->register_blocks();
	}

	/**
	 * Handle load text domain.
	 *
	 * @since 1.0.0
	 * @version 1.0.0
	 */
	public function load_plugin_text_domain() {
		load_plugin_textdomain( 'thim-blocks', false, self::$plugin_folder_name . '/languages' );
		// Load plugin translation files for internationalization.
	}

	public function register_icon_font() {
		$css_url  = self::$url_assets_dist . '/fonts/tb-icon.css';
		$css_path = self::$dir_assets_dist . '/fonts/tb-icon.css';

		if ( file_exists( $css_path ) ) {
			wp_register_style(
				'thim-blocks-icons',
				$css_url,
				array(),
				filemtime( $css_path )
			);

			add_action(
				'enqueue_block_assets',
				function () {
					wp_enqueue_style( 'thim-blocks-icons' );
				}
			);
		}
	}

	/**
	 * Enable SVG file upload support.
	 *
	 * @param array $mimes Allowed mime types.
	 * @return array Modified mime types.
	 */
	public function thim_enable_upload_svg( $mimes ) {
		$mimes['svg'] = 'image/svg+xml';
		return $mimes;
	}

}

Thim_Blocks::instance();
