<?php
/**
 * Prepare and initialize the SplendorStudio Framework.
 *
 * @package SplendorStudio\Framework
 *
 * @since   1.0.0
 */
class Fancify_Lite_BootStrap {

	/**
	 *  Php version required
	 */
	const FANCIFY_PHP_VERSION = '7.0';

	/**
	 * Wp Version required
	 */
	const FANCIFY_REQUIRED_WP_VERSION = '5.0';

	/**
	 * instance
	 */
	private static $instance;

	/**
	 * Constructor
	 */
	function __construct() {
		//Check requirements and load main class
		if ( $this->requirements_needs() ) {
			$this->boot();
		} else {
			add_action( 'admin_notices', array( $this, 'requirements_error' ) );
		}
	}

	/**
	 * Instance
	 */
	public static function instance() {
		if (!isset(self::$instance)) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Checks if the system requirements are met
	 *
	 * @since    1.0.0
	 * @return bool True if system requirements are met, false if not
	 */
	public function requirements_needs() {
		global $wp_version;

		if ( version_compare( self::FANCIFY_PHP_VERSION, PHP_VERSION, '>' ) ) {
			return false;
		}

		if ( version_compare( $wp_version, self::FANCIFY_REQUIRED_WP_VERSION, '<' ) ) {
			return false;
		}

		return true;
	}

	/**
	 * Prints an error that the system requirements weren't met.
	 *
	 * @since    1.0.0
	 */
	public function requirements_error() {
		global $wp_version;

		$fancify_lite_php_version = self::FANCIFY_PHP_VERSION;
		$fancify_lite_required_wp_version = self::FANCIFY_REQUIRED_WP_VERSION;

		require_once get_template_directory() . '/lib/templates/general/admin/requirements-error.php';// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- not a template
	}

	/**
	 * Bootstrap fancify.
	 *
	 * @since    1.0.0
	 */
	public function boot() {
		add_action( 'fancify_lite_init', array( $this, 'fancify_lite_define_constants' ), -1 );
		add_action( 'fancify_lite_init', array( $this, 'fancify_lite_load_dependencies' ), -1 );
		add_action( 'fancify_lite_init', array( $this, 'fancify_lite_add_theme_support' ) );
		add_action( 'fancify_lite_init', array( $this, 'fancify_lite_includes' ) );
		add_action( 'fancify_lite_init', array( $this, 'fancify_lite_load_textdomain' ) );
	}

	/**
	 * Define constants.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public function fancify_lite_define_constants() {
		// Define version.
		define( 'FANCIFY_VERSION', '1.1.0' );

		// Define paths.
		if ( ! defined( 'FANCIFY_THEME_PATH' ) ) {
			define( 'FANCIFY_THEME_PATH', wp_normalize_path( trailingslashit( get_template_directory() ) ) );
		}

		define( 'FANCIFY_PATH', FANCIFY_THEME_PATH . 'lib/' );
		define( 'FANCIFY_API_PATH', FANCIFY_PATH . 'api/' );
		define( 'FANCIFY_ASSETS_PATH', FANCIFY_PATH . 'assets/' );
		define( 'FANCIFY_LANGUAGES_PATH', FANCIFY_PATH . 'languages/' );
		define( 'FANCIFY_RENDER_PATH', FANCIFY_PATH . 'render/' );
		define( 'FANCIFY_TEMPLATES_PATH', FANCIFY_PATH . 'templates/' );
		define( 'FANCIFY_STRUCTURE_PATH', FANCIFY_TEMPLATES_PATH . 'structure/' );
		define( 'FANCIFY_FRAGMENTS_PATH', FANCIFY_TEMPLATES_PATH . 'fragments/' );

		// Define urls.
		if ( ! defined( 'FANCIFY_THEME_URL')) {
			define( 'FANCIFY_THEME_URL', trailingslashit( get_template_directory_uri() ) );
		}

		define( 'FANCIFY_URL', FANCIFY_THEME_URL . 'lib/' );
		define( 'FANCIFY_API_URL', FANCIFY_URL . 'api/' );
		define( 'FANCIFY_ASSETS_URL', FANCIFY_URL . 'assets/' );

		// Define admin paths.
		define( 'FANCIFY_ADMIN_PATH', FANCIFY_PATH . 'admin/' );

		// Define admin url.
		define( 'FANCIFY_ADMIN_URL', FANCIFY_URL . 'admin/' );
		define( 'FANCIFY_ADMIN_ASSETS_URL', FANCIFY_ADMIN_URL . 'assets/' );
		define( 'FANCIFY_ADMIN_JS_URL', FANCIFY_ADMIN_ASSETS_URL . 'js/' );
		define( 'FANCIFY_ADMIN_CSS_URL', FANCIFY_ADMIN_ASSETS_URL . 'css/' );

		//Define inside theme string path for GET_TEMPLATE_PART String
		define( 'FANCIFY_INSIDE_TEMPLATES_PATH', 'lib/templates/' );
	}

	/**
	 * Load dependencies.
	 *
	 * @return void
	 */
	public function fancify_lite_load_dependencies() {
		require_once FANCIFY_API_PATH . 'class-fancify-loader.php';// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- not a template
		Fancify_Lite_Loader::get_instance();

		// Load the necessary Fancify components.
		Fancify_Lite_Loader::fancify_lite_load_api_components(
			array(
				'model',
				'controller',
				'actions',
				'html',
				'image',
				'template',
				'admin',
				'layout',
				'widget',
				'custom_field',
				'nav_menu',
				'ajax',
			)
		);

		// Add third party styles and scripts compiler support.
		Fancify_Lite_Loader::fancify_lite_add_api_component_support( 'wp_styles_compiler' );
		Fancify_Lite_Loader::fancify_lite_add_api_component_support( 'wp_scripts_compiler' );

		//Fires after Fancify API loads.
		do_action( 'fancify_lite_after_load_api' );
	}

	/**
	 * * Add theme support.
	 *
	 * @return void
	 */
	public function fancify_lite_add_theme_support() {
		add_theme_support( 'title-tag' );
		add_theme_support( 'post-thumbnails' );
		add_theme_support( 'automatic-feed-links' );
		add_theme_support( 'fancify-default-styling' );
		add_theme_support( 'post-formats', 
			array(
				'image', 
				'quote', 
			)
		);

		add_theme_support( 'html5', 
			array(
				'comment-list', 
				'comment-form', 
				'search-form', 
				'caption'
			) 
		);

		add_theme_support( 'offcanvas-menu' );

		/**
		 * Add support for core custom logo.
		 *
		 * @link https://codex.wordpress.org/Theme_Logo
		 */
		$logo_width  = 300;
		$logo_height = 100;

		add_theme_support(
			'custom-logo',
			array(
				'height'               => $logo_height,
				'width'                => $logo_width,
				'flex-width'           => true,
				'flex-height'          => true,
				'unlink-homepage-logo' => true,
			)
		);

		// Custom background color.
		add_theme_support(
			'custom-background',
			array(
				'default-color' => 'ffffff',
			)
		);
	}

	/**
	 * Include framework files.
	 *
	 * @return void
	 */
	public function fancify_lite_includes() {
		/**
		 * * Includes files required for each section as below
		 * * Admin
		 * * Readers
		 * * Customizer
		 * * also added all assets needed
		 */
		// Include assets.
		require_once FANCIFY_ASSETS_PATH . 'assets.php';// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- not a template

		/**
		 * include Reader files
		 */
		$readers_files = array(
			FANCIFY_RENDER_PATH . 'template-parts.php',
			FANCIFY_RENDER_PATH . 'fragments.php',
			FANCIFY_RENDER_PATH . 'widget-area.php',
		);

		foreach ( $readers_files as $file ) {
			require_once $file;// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- not a template
		}
	}

	/**
	 * Load text domain.
	 *
	 * @return void
	 */
	public function fancify_lite_load_textdomain() {
		load_theme_textdomain( 'fancify-lite', FANCIFY_LANGUAGES_PATH );
	}
}
// Intiate our main class
Fancify_Lite_BootStrap::instance();

/**
 * Fires before SplendorStudio Framework loads.
 */
do_action( 'fancify_lite_before_init' );

/**
 * Load SplendorStudio Framework.
 */
do_action( 'fancify_lite_init' );

/**
 * Fires after SplendorStudio Framework loads.
 */
do_action( 'fancify_lite_after_init' );