package EPrints::KlarasConnect::UI;

# Small XHTML helpers shared by the admin screens. Everything goes through
# EPrints' DOM builders (make_element / make_text), which escape text, so
# a value from the database can never become markup.
#
# CSRF: EPrints 3.4 protects forms when csrf_token_salt is set; 3.3 has no
# protection at all. Every form here carries its own token as well
# (HMAC of the user id and the hour, keyed by a per-repository secret),
# checked before any action changes anything.

use strict;
use warnings;

use EPrints::KlarasConnect::Settings;
use EPrints::KlarasConnect::Util qw( hmac_hex secure_equals );

use constant CSRF_FIELD => 'kc_csrf';

my $CSS = <<'CSS';
.kc { max-width: 960px; font-size: 14px; line-height: 1.5; }
.kc h2 { font-size: 1.15em; margin: 1.6em 0 .5em; }
.kc .kc-card { border: 1px solid #d0d7de; border-radius: 6px; padding: 12px 16px; margin: 0 0 12px; background: #fff; }
.kc .kc-muted { color: #57606a; }
.kc .kc-badge { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: .85em; font-weight: bold; }
.kc .kc-good { background: #dafbe1; color: #116329; }
.kc .kc-warn { background: #fff8c5; color: #7d4e00; }
.kc .kc-bad  { background: #ffebe9; color: #a40e26; }
.kc .kc-info { background: #ddf4ff; color: #0550ae; }
.kc table.kc-table { border-collapse: collapse; width: 100%; }
.kc table.kc-table th, .kc table.kc-table td { text-align: left; vertical-align: top; padding: 6px 8px; border-bottom: 1px solid #eaeef2; }
.kc .kc-score { font-size: 2.4em; font-weight: bold; line-height: 1; }
.kc .kc-fix { margin-top: 4px; }
.kc .kc-bar { display: inline-block; width: 10px; margin-right: 2px; background: #0969da; vertical-align: bottom; }
.kc input[type=text], .kc input[type=password] { width: 100%; max-width: 560px; padding: 4px 6px; box-sizing: border-box; }
.kc label { display: block; margin: 8px 0 2px; font-weight: bold; }
.kc .kc-check { font-weight: normal; }
.kc .kc-actions { margin-top: 10px; }
.kc .kc-actions input { margin-right: 6px; }
CSS

sub root
{
	my( $session ) = @_;
	my $frag = $session->make_doc_fragment;
	my $style = $session->make_element( 'style', type => 'text/css' );
	$style->appendChild( $session->make_text( $CSS ) );
	$frag->appendChild( $style );
	my $div = $session->make_element( 'div', class => 'kc' );
	$frag->appendChild( $div );
	return ( $frag, $div );
}

sub el
{
	my( $session, $parent, $name, $text, %attr ) = @_;
	my $e = $session->make_element( $name, %attr );
	$e->appendChild( $session->make_text( $text ) ) if defined $text;
	$parent->appendChild( $e ) if defined $parent;
	return $e;
}

sub text
{
	my( $session, $parent, $text ) = @_;
	$parent->appendChild( $session->make_text( defined $text ? $text : '' ) );
}

sub badge
{
	my( $session, $parent, $status, $label ) = @_;
	$status = 'info' if !defined $status || $status !~ /\A(?:good|warn|bad|info)\z/;
	return el( $session, $parent, 'span', $label, class => "kc-badge kc-$status" );
}

sub card
{
	my( $session, $parent, $title ) = @_;
	my $c = el( $session, $parent, 'div', undef, class => 'kc-card' );
	el( $session, $c, 'strong', $title ) if defined $title;
	return $c;
}

sub table
{
	my( $session, $parent, $head, $rows ) = @_;
	my $t = el( $session, $parent, 'table', undef, class => 'kc-table' );
	if( $head )
	{
		my $tr = el( $session, $t, 'tr' );
		el( $session, $tr, 'th', $_ ) for @$head;
	}
	foreach my $row ( @$rows )
	{
		my $tr = el( $session, $t, 'tr' );
		foreach my $cell ( @$row )
		{
			my $td = el( $session, $tr, 'td' );
			if( ref $cell ) { $td->appendChild( $cell ) }
			else { text( $session, $td, $cell ) }
		}
	}
	return $t;
}

# ------------------------------------------------------------------ CSRF

sub _csrf_value
{
	my( $session, $hour ) = @_;
	my $user = $session->current_user or return undef;
	my $key = EPrints::KlarasConnect::Settings::get_or_create_random( $session, 'csrf_key', 32 );
	return hmac_hex( 'csrf|' . $user->id . '|' . $hour, $key );
}

sub csrf_input
{
	my( $session ) = @_;
	my $v = _csrf_value( $session, int( time / 3600 ) );
	return $session->make_element( 'input', type => 'hidden', name => CSRF_FIELD, value => defined $v ? $v : '' );
}

# Valid for the current and the previous hour.
sub csrf_ok
{
	my( $session ) = @_;
	return 0 if ( $ENV{REQUEST_METHOD} || '' ) ne 'POST';
	my $sent = $session->param( CSRF_FIELD );
	return 0 if !defined $sent || $sent !~ /\A[0-9a-f]{64}\z/;
	my $hour = int( time / 3600 );
	foreach my $h ( $hour, $hour - 1 )
	{
		my $v = _csrf_value( $session, $h );
		return 1 if defined $v && secure_equals( $sent, $v );
	}
	return 0;
}

sub ago
{
	my( $t ) = @_;
	return 'belum pernah' if !$t;
	my $d = time - $t;
	return 'baru saja' if $d < 90;
	return int( $d / 60 ) . ' menit lalu' if $d < 5400;
	return int( $d / 3600 ) . ' jam lalu' if $d < 2 * 86400;
	return int( $d / 86400 ) . ' hari lalu';
}

1;
