<?php

if ( ! defined( 'ABSPATH' ) ) {
	die( 'I’m a theme.' );
}

if ( ! function_exists( 'trigger_pfadfinden_plugin_error' ) ) {
	/**
	 * Show an error message.
	 * 
	 * @see http://www.squarepenguin.com/wordpress/?p=6 Inspiration
	 * 
	 * @param string $message
	 * @param int    $type    optional
	 * @return bool
	 */
	function trigger_pfadfinden_plugin_error( $message, $type = 0 )
	{
		if ( isset( $_GET['action'] ) && 'error_scrape' === $_GET['action'] ) {
			echo $message;
			return true;
		}

		if ( ! $type ) {
			$type = E_USER_WARNING;
		}

		return trigger_error( $message, $type );
	}
}


// Check for suitable environment
if ( defined( 'PHP_VERSION_ID' ) && PHP_VERSION_ID >= 50400 ) {
	// Register autoloader if updater plugin missing
	if ( ! class_exists( 'plugins\buena\use\shy-wordpress\src\Shy\WordPress\Theme' ) ) {
		if ( ! include_once __DIR__ . '/use/shy-wordpress/src/autoloader.php' ) {
			trigger_pfadfinden_plugin_error(
				__( 'Das Theme ist unvollständig und konnte nicht geladen werden. Neuinstallation müsste helfen.', 'buena-theme' ),
				E_USER_ERROR
			);
			return;
		}
	}

	// Register our autoloader
	if ( ! include_once __DIR__ . '/src/autoloader.php' ) {
		trigger_pfadfinden_plugin_error(
			__( 'Das Theme ist unvollständig und konnte nicht geladen werden. Neuinstallation müsste helfen.', 'buena-theme' ),
			E_USER_ERROR
		);
		return;
	}

	/**
	 * @return \plugins\buena\src\Pfadfinden\WordPress\BuenaTheme
	 */
	function buena_get_theme()
	{
		static $theme = null;
		if (!$theme) {
			$theme = new ReflectionClass( 'plugins\buena\src\Pfadfinden\WordPress\BuenaTheme' );
			$theme = $theme->newInstance();
		}

		return $theme;
	}

	/**
	 * @param string $method
	 * @return callable
	 */
	function buena_get_callback( $method )
	{
		return array( buena_get_theme(), (string) $method );
	}

	return buena_get_theme();
}


// Display error message
trigger_pfadfinden_plugin_error(
	sprintf(
		__( 'You need at least PHP 5.4 to use the Buena theme. Your are using %s.', 'buena-theme' ),
		PHP_VERSION
	),
	E_USER_ERROR
);