#!/usr/bin/perl
# This can be run stackless.
#use Mooix::Thing;
#use Mooix::Root;
run sub {
	my $this=shift;
	%_=@_;
	
	my $loc=$this->location;
	unless ($loc->isa($Mooix::Root->concrete->room)) {
		fail "The switch won't do anything if it is not on a wall.";
	}
	
	my $newstate = ($_{do_preposition} eq 'on') ? 1 : 0;
	if ($this->state == $newstate) {
		fail "It is already ".($newstate ? 'on' : 'off').".";
	}
	if ($this->state($newstate) != $newstate) {
		fail "For some reason it won't switch.";
	}
	
	if ($newstate != $this->filterstate) {
		# Turn on lights before showing message.
		if (! $loc->messagefilters->remove(object => $this->filter)) {
			fail "The switch doesn't do anything.";
		}
	}
	
	$this->msg('switch', %_,
		state => $newstate ? 'on' : 'off');
	
	if ($newstate == $this->filterstate) {
		if (! $loc->messagefilters->add(object => $this->filter)) {
			fail "The switch doesn't do anything.";
		}
	}
}
