package HTML::Form::Simple;

use strict;
use warnings;

use XML::Quote;

our $VERSION = '1.00';


sub new {
	bless {}, $_[0];
}

sub quote {
	my $self = shift;
	return XML::Quote::xml_quote_min($_[0]);
}

sub tag {
	my ($self, $tag, $attr) = @_;

	# strip empty if it shouldn't be
	defined $attr->{$_} and $attr->{$_} eq '' and delete $attr->{$_}
		for qw(id type class style);

	my $return = '<' . $tag;

	# add booleans
	delete $attr->{$_} and $return .= ' '.$_
		for qw(selected checked disabled readonly);

	$return .= sprintf ' %s="%s"', $_, $self->quote($attr->{$_})
		for sort grep { defined $attr->{$_} } keys %$attr;

	return $return . '>';
}


sub start {
	my ($self, $attr) = @_;

	return $self->tag(form => $attr);
}

sub stop {
	return '</form>';
}


sub submit {
	my ($self, $value, $attr) = @_;

	if (ref $value eq 'HASH') {
		$attr = $value;
	}
	else {
		$attr ||= {};
		$attr->{value} = $value;
	}

	$attr->{type} = 'submit' unless defined $attr->{type};

	return $self->tag(input => $attr);
}

sub hidden {
	my ($self, $name, $value) = @_;

	#TODO: $attr

	return $self->tag(input => {type => 'hidden', name => $name, value => $value});
}

sub input {
	my ($self, $name, $value, $attr) = @_;

	if (ref $name eq 'HASH') {
		# only attributes provided (first argument)
		$attr = $name;
	}
	elsif (ref $value eq 'HASH') {
		# name shorthand (attributes in value parameter)
		$attr = $value;
		$attr->{name} = $name;
	}
	else {
		# name and value shorthands (all vars keep their assigned values)
		$attr ||= {};
		$attr->{name} = $name;
		$attr->{value} = $value;
	}

	$attr->{id}   = $attr->{name} unless defined $attr->{id};
	$attr->{type} = 'text' unless defined $attr->{type} or defined $attr->{rows};
	$value = delete $attr->{value} if defined $attr->{rows};

	return defined $attr->{rows} ? sprintf(
		'%s%s</textarea>',
		$self->tag(textarea => $attr),
		$self->quote(defined $value ? $value : '')
	) : $self->tag(input => $attr);
}

sub select {
	my ($self, $name, $rows, $value, $attr) = @_;

	if (ref $value eq 'HASH') {
		$attr = $value;
		$value = delete $attr->{value};
	}
	else {
		$attr ||= {};
		delete $attr->{value};
	}
	$attr->{name} = $name;
	$attr->{id}   = $attr->{name} unless defined $attr->{id};
	$attr->{type} = 'select' unless defined $attr->{type};

	my @options = map { ref $_ ? $_ : {value => $_} } @$rows;

	if ($attr->{type} eq 'select') {
		delete $attr->{type};
		if (defined $value) {
			for (@options) {
				$_->{selected} = 1 if defined $_->{value} and $_->{value} eq $value;
			}
		}
		my @return = (
			$self->tag(select => $attr),
			(map { $self->tag(option => $_) } @options),
			'</select>',
		);
		return wantarray ? @return : join('', @return);
	}
	else {
		if (defined $attr->{id}) {
			defined $_->{id} or defined $_->{value} and $_->{id} = $attr->{id}.'_'.$_->{value}
				for @options;
		}
		if (defined $attr->{label}) {
			defined $_->{value} and not defined $_->{label}
				and $_->{label} = $attr->{label}->{$_->{value}}
					for @options;
			delete $attr->{label};
		}
		if (defined $value) {
			for (@options) {
				$_->{checked} = 1 if defined $_->{value} and $_->{value} eq $value;
			}
		}
		$_ = {%$attr, %$_} for @options;
		my @return = map {
			my $label = delete $_->{label};
			defined $label && $label ne ''
				? '<label>'.$self->tag(input => $_)." $label</label>"
				:           $self->tag(input => $_)
		} @options;
		return wantarray ? @return : join('', @return);
	}
}

sub radio {
	my ($self, $name, $value, $attr) = @_;

	if (ref $value eq 'HASH') {
		$attr = $value;
	}
	else {
		$attr ||= {};
		$attr->{value} = $value;
	}

	$self->select($name, [$attr], {type => 'radio'});
}

sub check {
	my ($self, $name, $label, $checked, $attr) = @_;

	if (ref $label eq 'HASH') {
		$attr = $label;
		undef $label;
	}
	elsif (ref $checked eq 'HASH') {
		$attr = $checked;
		$attr->{label} = $label;
	}
	else {
		$attr ||= {};
		$attr->{checked} = $checked;
		$attr->{label} = $label;
	}
	$attr->{value} = '1' unless exists $attr->{value};

	$self->select($name, [$attr], {type => 'checkbox'});
}

1;

=head1 NAME

HTML::Form::Simple

=head1 SYNOPSIS

	my $input = HTML::Form::Simple->new;
	say $input->start; # <form>
	printf "<label>%s: %s</label>\n", @$_ for (
		[ 'Your Name' => $input->text(
			user => 'Mr. Default'
		) ],
		[ Message => $input->text(
			msg => 'Textarea default', {rows => 4, style => 'background:red'}
		) ],
		[ Gender => join ' or ', $input->radio(
			sex => ['m', 'f']
		) ],
		[ Colour => scalar $input->select(
			favcolour => [qw(Blue Green Red)], 'Green'
		) ],
		[ Options => $input->check(
			spam => 'Receive weekly newsletter'
		) ],
	);
	say $input->stop; # </form>

