use 5.010;
use utf8;
use MooseX::Declare;

BEGIN
{
	$WWW::DataWiki::ErrorHandler::Standard::AUTHORITY = 'cpan:TOBYINK';
	$WWW::DataWiki::ErrorHandler::Standard::VERSION   = '0.000_01';
}

class WWW::DataWiki::ErrorHandler::Standard
	with WWW::DataWiki::Trait::Negotiator
{
	use HTML::HTML5::Builder qw[:standard link];
	use HTML::HTML5::Writer;
		
	method available_formats
	{
		return (
			['html',  1.0, 'text/html',   undef, 'utf-8'],
			['xhtml', 1.0, 'application/xhtml+xml', undef, 'utf-8'],
			['text',  0.5, 'text/plain', undef, 'utf-8'],
			);
	}
	
	method extension_map
	{
		return;
	}
	
	method emit ($ctx, $output)
	{
		use Data::Dumper;
		warn Dumper($self);
		
		my ($pref, $media, $charset, $gzip) = $self->negotiate($ctx);
		
		my ($error) = @{ $ctx->error // [] };
		my ($body, $writer);
		if ($pref eq 'text')
		{
			$body = $output;
		}
		else
		{
			$writer = HTML::HTML5::Writer->new(markup=>$pref, polyglot=>1);
		}
		
		$body //= $writer->document(
			html(
				head(
					title(-lang=>'en', $error->code, ': ', $error->message),
					&link(-rel=>'stylesheet', -type=>'text/css', -href=>'/static/styles/error.css', -media=>'screen,projection'),
					),
				body(
					h1(-lang=>'en', $error->message),
					p($error->explanation),
					($error->code >= 500 ? pre($output) : ''),
					),
				),
			);
		
		$ctx->res->status($error->code);
		$ctx->res->content_type("$media; charset=$charset");
		$ctx->res->body($body);
		$ctx->detach;
	}
}

1;