package JIRA::REST::Class::Issue::TimeTracking;
use base qw( JIRA::REST::Class::Abstract );
use strict;
use warnings;
use v5.10;

our $VERSION = '0.02';

# ABSTRACT: A helper class for C<JIRA::REST::Class> that represents the time tracking for a JIRA issue as an object.

use Contextual::Return;

sub init {
    my $self = shift;
    $self->SUPER::init(@_);

    my $data = $self->issue->get(q{}, { fields => 'timetracking' });
    $self->{data} = $data->{fields}->{timetracking};
}


sub originalEstimate {
    my $self = shift;
    return
        NUM { $self->data->{originalEstimateSeconds} }
        STR { $self->data->{originalEstimate} }
    ;
}


sub remainingEstimate {
    my $self = shift;
    return
        NUM { $self->data->{remainingEstimateSeconds} }
        STR { $self->data->{remainingEstimate} }
    ;
}


sub timeSpent {
    my $self = shift;
    return
        NUM { $self->data->{timeSpentSeconds} }
        STR { $self->data->{timeSpent} }
    ;
}


sub set_originalEstimate {
    my $self = shift;
    my $est  = shift;
    $self->update( { originalEstimate => $est } );
}


sub set_remainingEstimate {
    my $self = shift;
    my $est  = shift;
    $self->update( { remainingEstimate => $est } );
}


sub update {
    my $self   = shift;
    my $update = shift;

    foreach my $key (qw/ originalEstimate remainingEstimate /) {
        # if we're updating the key, don't change it
        next if exists $update->{$key};

        # since we're not updating the key, copy the original value
        # into the update, because the REST interface has an annoying
        # tendency to reset those values if you don't explicitly set them
        if (defined $self->data->{$key}) {
            $update->{$key} = $self->data->{$key};
        }
    }

    $self->issue->put_field( timetracking => $update );
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

JIRA::REST::Class::Issue::TimeTracking - A helper class for C<JIRA::REST::Class> that represents the time tracking for a JIRA issue as an object.

=head1 VERSION

version 0.02

=head1 READ-ONLY ACCESSORS

=head2 B<originalEstimate>

Returns the original estimate as a number of seconds in numeric context, and as a w/d/h/m/s string in a string context.

=head2 B<remainingEstimate>

Returns the remaining estimate as a number of seconds in numeric context, and as a w/d/h/m/s string in a string context.

=head2 B<timeSpent>

Returns the time spent as a number of seconds in numeric context, and as a w/d/h/m/s string in a string context.

=head1 METHODS

=head2 B<set_originalEstimate>

Sets the original estimate to the amount of time given.  Accepts any time format that JIRA uses.

=head2 B<set_remainingEstimate>

Sets the remaining estimate to the amount of time given.  Accepts any time format that JIRA uses.

=head2 B<update>

Accepts a hashref of timetracking fields to update. The acceptable fields are determined by JIRA, but I think they're originalEstimate and remainingEstimate.

=head1 AUTHOR

Packy Anderson <packy@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2016 by Packy Anderson.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

=cut
