<?php
use DebugBar\DebugBar;
use DebugBar\DataCollector\ExceptionsCollector;
use DebugBar\DataCollector\MemoryCollector;
use DebugBar\DataCollector\MessagesCollector;
use DebugBar\DataCollector\PhpInfoCollector;
use DebugBar\DataCollector\RequestDataCollector;
use DebugBar\DataCollector\TimeDataCollector;

require_once('queries-collector.php');
require_once('warning-collector.php');
require_once('info-collector.php');
require_once('wordpress-collector.php');

global $bar;
$bar = new DebugBar();

$bar->addCollector(new PhpInfoCollector());
$bar->addCollector(new MessagesCollector());
$bar->addCollector(new RequestDataCollector());
$bar->addCollector(new MemoryCollector());

// Return bar assets
function debug_boot () {
	global $bar;
	$renderer = $bar->getJavascriptRenderer();

	if (WP_ENV == 'development') {
		// Dumb js
		echo('<script>');
		print_r($renderer->dumpJsAssets());
		echo('</script>');

		// Dump CSS
		echo('<style>');
		print_r($renderer->dumpCssAssets());
		echo('</style>');

		// Dump custom CSS
		echo('<style>');
		print_r(file_get_contents(__DIR__ . '/debug-bar.css'));
		echo('</style>');

		// Call FontAwesome from CDN
		echo('<script src="https://kit.fontawesome.com/d4be948bd3.js" crossorigin="anonymous"></script>');

		// Render bar
		return $renderer->render();
	}
};

// Add message to message log
function debug_log($message, $path = true, $label = 'message') {
	global $bar;

	$bar["messages"]->addMessage($message, $label);

	if($path) {
		$backtrace = debug_backtrace();
		$backtrace_file = '';
		$backtrace_line = '';
		if(isset($backtrace)) {
			if(isset(debug_backtrace()[0])) {
				$backtrace_file = debug_backtrace()[0]['file'];
				$backtrace_line = debug_backtrace()[0]['line'];
			}
		}
		$backtrace_label = $backtrace_file . ' - line ' . $backtrace_line;
		$bar["messages"]->addMessage($backtrace_label, 'stack trace');
	}
	return 0;
};

// Add message to info log
function debug_info($message, $label = 'info') {
	global $bar;

	$bar["info"]->addMessage($message, $label);
}

?>
