#!/usr/bin/perl
# This verb works for both the is and the set command, since they both
# parse out to the same form.
#use Mooix::Thing;
run sub {
	my $this=shift;
	%_=@_;

	# Antispoofing, just in case.
	if ($_{avatar} != $this) {
		fail "No!";
	}
	
	my $object=$_{direct_object} or $this->usage("bad direct object");  
	my $field=$_{field} or $this->usage("bad field");
	my $index=$_{number} || 1; # index is 1-based
	
	my $setstr="Set.";
	my $val;

	if (exists $_{quote}) {
		$val=$_{quote};
	}
	else {
		# This supports the form where a value is not given,
		# just a statement that an object has a field or not, which
		# translates into setting the field to a true or false
		# value.
		$val=exists $_{negated_verb} ? 0 : 1;
		$setstr ="Unset." if ! $val;
	}

	my @vals;
	@vals=$object->safegetfield($field);
	# Any index larger than the current number of values just adds to
	# the end. It wouldn't do to insert some item at position one
	# million or something..
	if ($index > @vals) {
		push @vals, $val;
	}
	else {
		$vals[$index - 1]=$val; # perl arrays are 0-based
	}
	
	my ($stat, $msg) = $this->safechange(
		object => $object,
		field => $field,
		(map { (value => $_) } @vals),
	);

	if (! $stat) {
		fail $msg;
	}
	else {
		$_{session}->write($setstr);
	}
}
