package HTML::Form::Simple;

use strict;
use warnings;

use XML::Quote;

our $VERSION = '1.00';


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

sub _attr {
	my $self = shift;
	my $expect = shift;
	my $attr = ref $_[-1] eq 'HASH' ? pop : {};
	push @_, undef for @_+1 .. $expect;
	push @_, $attr;
	return @_;
}

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 = shift;
	my ($value, $attr) = $self->_attr(1, @_);

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

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

sub hidden {
	my $self = shift;
	my ($name, $value, $attr) = $self->_attr(2, @_);

	$attr = {type => 'hidden', name => $name, value => $value};
	#TODO: $attr

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

sub input {
	my $self = shift;
	my ($name, $value, $attr) = $self->_attr(2, @_);

	$attr->{name } = $name  if defined $name;
	$attr->{value} = $value if defined $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 = shift;
	my ($name, $rows, $default, $attr) = $self->_attr(3, @_);

	$default = $attr->{value} unless defined $default;
	delete $attr->{value};  # never a parent attribute

	$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 $default) {
			for (@options) {
				$_->{selected} = 1 if defined $_->{value} and $_->{value} eq $default;
			}
		}
		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 $default) {
			for (@options) {
				$_->{checked} = 1 if defined $_->{value} and $_->{value} eq $default;
			}
		}
		$_ = {%$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 = shift;
	my ($name, $value, $attr) = $self->_attr(2, @_);

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

sub check {
	my $self = shift;
	my ($name, $label, $checked, $attr) = $self->_attr(3, @_);

	$attr->{label  } = $label   if defined $label;
	$attr->{checked} = $checked if defined $checked;
	$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>

