package xWord;
use strict;
use warnings;
use xKey;

sub new {
   my $self = {
      __word => undef,  #vector of the pressed characters
      __PP => [],    #vector of Press(i+1)-Press(i) timings
      __RR => [],    #vector of Release(i+1)-Release(i) timings
      __PR => [],    #vector of Press(i+1)-Release(i) timings
      __RP => [],    #vector of Release(i)-Press(i) timings
      __start => undef, #start time
      __end => undef,   #end time
      __last_press =>undef,
      __last_release =>undef,
   };
   bless $self, 'xWord';
   return $self;
}

sub add_key {
   my ($self,$key,$ch) =@_;
   if(!(defined $self->{__word})) {
# if the 1st key hasn't been added add the key and 
# make the start=key press
      $self->{__word}=$ch || $key->key;
      $self->{__start}=$key->p;
   } else {
      $self->{__word}.=$ch || $key->ch;
      
      push(@{$self->{__PP}},$key->p - $self->{__last_press});
      push(@{$self->{__RR}},$key->r - $self->{__last_release});
      push(@{$self->{__PR}},$key->p - $self->{__last_release});
# add any additional freatures here
   }
# or here:
   push(@{$self->{__RP}}, $key->r - $key->p);
# update the last press and release
   $self->{__last_press}=$key->p;
   $self->{__last_release}=$key->r;
# if this is the last key then the release is the end of the
# typed word
   $self->{__end}=$key->r;
}

sub vec {
   my ($self) = @_;
   my @tmp=();
   push(@tmp,($self->{__end} - $self->{__start}));
   push(@tmp,@{$self->{__RP}}); 
   push(@tmp,@{$self->{__PP}}); 
   push(@tmp,@{$self->{__RR}}); 
   push(@tmp,@{$self->{__PR}}); 
# add any additional features here
   return @tmp;
}

sub get_str_vec_names {
   my $self=shift;
   return "duration, "
      .("RP, "x($#{$self->{__RP}}+1))
      .("PP, "x($#{$self->{__PP}}+1))
      .("RR, "x($#{$self->{__RR}}+1))
      .("PR, "x($#{$self->{__PR}}))."PR"
      ."\n";
#modify the above for the optinal features added
}

sub get_str_vec {
   my ($self) = @_;
   my @tmp=$self->vec;
   my $vec=join(', ',@tmp);
   return "\n".$self->{__word}.": $vec\n";
}


sub print {
   my ($self) = @_;
   my $i=0;
   if(defined $self->{__word}) {
      print "xWord: ".($self->{__word});
   }

   print "\ntotal time to type word: ".($self->{__end} - $self->{__start});
   print "\nkey duration = R(i+1) - P(i) = "; 
   foreach my $k (@{$self->{__RP}}) { printf("%2d\t",$k); }
   print "\nP(i+1) - P(i) = "; 
   foreach my $k (@{$self->{__PP}}) { printf("%2d\t",$k); }
   print "\nR(i+1) - R(i) = "; 
   foreach my $k (@{$self->{__RR}}) { printf("%2d\t",$k); }
   print "\nP(i+1) - R(i) = "; 
   foreach my $k (@{$self->{__PR}}) { printf("%2d\t",$k); }
   print "\n";
}

1;

