package robliterator::robot;
use strict;
use overload '""' => \&id, fallback => 1;
1;

our @properties = qw(alive kills lifetime distance errors);
our @firstValues = (1, 0, 0, 0, 0);
our $firstAction = 'appear';

our $actionAppeared = 'appeared';
our $actionIdled = 'idled';
our $actionMoved = 'moved';
our $actionCollided = 'collided';
our $actionMissed = 'missed';
our $actionKilled = 'killed';
our $actionSuicide = 'suicide';

sub new {
    my($class, $id, $url, $data, @values) = @_;
    my $self = {
        id => $id,
        url => $url,
        data => $data,
    };
    die unless scalar(@properties) == scalar(@values);
    foreach (@properties) {
        $self->{$_} = shift(@values);
    }
    bless $self, $class;
    return $self;
}

sub id {
    return $_[0]->{id};
}

sub url {
    return $_[0]->{url};
}

sub domain {
    my($self) = @_;
    if ($self->{url} =~ m|^http://([^/]+)|osi) {
        return $1;
    }
    die;
}

sub path {
    my($self) = @_;
    if ($self->{url} =~ m|^http://[^/]+([^#]*)|osi) {
        return $1;
    }
    die;
}

sub data {
    return $_[0]->{data};
}


sub setXY {
    $_[0]->{x} = $_[1];
    $_[0]->{y} = $_[2];
}

sub x {
    $_[0]->{x};
}

sub y {
    $_[0]->{y};
}


sub setMovement {
    $_[0]->{dx} = $_[1];
    $_[0]->{dy} = $_[2];
}

sub dx {
    $_[0]->{dx};
}

sub dy {
    $_[0]->{dy};
}


sub setFire {
    $_[0]->{fx} = $_[1];
    $_[0]->{fy} = $_[2];
}

sub fx {
    $_[0]->{fx};
}

sub fy {
    $_[0]->{fy};
}


sub values {
    my($self) = @_;
    my @result = ();
    foreach (@properties) {
        push(@result, $self->{$_});
    }
    return @result;
}

sub value {
    return $_[0]->{$_[1]};
}

sub setValue {
    $_[0]->{$_[1]} = $_[2];
}
