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, @_);

	if (ref $name eq 'HASH') {
		my @return = map { $self->hidden($_, $name->{$_}, $attr) } sort keys %$name;
		return wantarray ? @return : join(defined $, ? $, : '', @return);
	}

	if (ref $value eq 'ARRAY') {
		my @return = map { $self->hidden($name, $_, $attr) } @$value;
		return wantarray ? @return : join(defined $, ? $, : '', @return);
	}

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

	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;

	my @return;

	if ($attr->{type} eq 'select') {
		delete $attr->{type};
		if (defined $default) {
			for (@options) {
				$_->{selected} = 1 if defined $_->{value} and $_->{value} eq $default;
			}
		}
		@return = (
			$self->tag(select => $attr),
			(map { $self->tag(option => $_) } @options),
			'</select>',
		);
	}
	else {
		if (defined $attr->{id} and $attr->{id} ne '') {
			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;
		@return = map {
			my $label = delete $_->{label};
			defined $label && $label ne ''
				? '<label>'.$self->tag(input => $_)." $label</label>"
				:           $self->tag(input => $_)
		} @options;
	}

	return wantarray ? @return : join(defined $, ? $, : '', @return);
}

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

	if (not defined $value) {
		if (defined $label) {
			$value = ref $label eq 'ARRAY' ? [1 .. $#$label+1] : [1];
		}
		else {
			$value = [{}];
		}
	}
	elsif (ref $value ne 'ARRAY') {
		$value = [$value];
	}

	if (defined $label) {
		$_ = ref $_ eq 'HASH' ? {%$_} : {value => $_} for @$value;
		$_->{label} = ref $label eq 'ARRAY' ? shift @$label : $label for @$value;
	}

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

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

	my $rows = defined $label ? ref $label eq 'ARRAY' ? $label : [$label] : [{}];
	ref $_ eq 'HASH' or $_ = {label => $_} for @$rows;
	if (defined $checked) {
		if (ref $checked eq 'ARRAY') {
			$_->{checked} = shift @$checked for @$rows;
			push @$rows, map { {checked => $_} } @$checked;
		}
		else {
			$_->{checked} = $checked for @$rows;
		}
	}
	exists $rows->[$_]->{value} or $rows->[$_]->{value} = $_ + 1 for 0 .. $#$rows;

	$self->select($name, $rows, {%$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>

