# Chariot Stop
# ============
#
# A chariot stop in some region in a telling in ATITD. Has a name (same as the
# region's), a set of coordinates and links to other chariot stops.

package ATITD::Chariot;

use strict;
use warnings;

use ATITD::Chariot::Link;

# Creates a chariot stop in some region. Doesn't repair it, though!
sub new {
	my ($class, $telling, $region, $x, $y) = @_;
	my $self = bless {
		telling => $telling,
		region => $region,
		coords => [$x, $y],
		links => {},
	}, $class;
	
	return $self;
}

# Telling we are in.
sub telling { $_[0]->{telling} }

# Our region.
sub region { $_[0]->{region} }

# Our name (same as the region's).
sub name { $_[0]->region->name }

# Our coordinates.
sub coords { wantarray ? @{$_[0]->{coords}} : $_[0]->{coords} }

# All links we have.
sub links {
	# Need to filter out non-existing links
	my %links = map {
		$_ => $_[0]->{links}->{$_}
	} grep { 
		$_[0]->{links}->{$_}->exists
	} keys %{$_[0]->{links}};
	
	wantarray ? values %links : \%links
}

# Our X-Coordinate.
sub x { $_->{coords}->[0] }

# Our Y-Coordinate.
sub y { $_->{coords}->[1] }

# A single link to another chariot. Accepts a region, chariot or the name of
# either as parameter.
sub link {
	my ($self, $target) = @_;
	my $region = 
		ref $target eq 'ATITD::Region' ? $target :
		ref $target eq 'ATITD::Chariot' ? $target->region :
		$self->telling->region($target);
	
	return unless defined $region;
	
	# If we don't have that link already, create it... it won't exist till
	# someone modifies it (see ATITD::Chariot::Link)
	unless (defined $self->{links}->{$region->name}) {
		$self->{links}->{$region->name} = ATITD::Chariot::Link->new(
			$self, $region->chariot);
	}
	
	return $self->{links}->{$region->name};
}

1;
