/***
 * Excerpted from "Developing for Apple Watch",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/jkwatch for more book information.
***/
//
//  Track.m
//  TapALap
//
//  Created by Jeff Kelley on 5/8/15.
//  Copyright (c) 2015 Pragmatic Bookshelf. All rights reserved.
//

#import "Track.h"

@implementation Track

+ (instancetype)trackWithDictionary:(NSDictionary *)dictionary
{
    NSString *name = dictionary[@"name"];
    NSNumber *lapDistance = dictionary[@"lapDistance"];
    
    if (name != nil && lapDistance != nil) {
        return [[self alloc] initWithName:name
                              lapDistance:lapDistance.doubleValue];
    }
    
    return nil;
}

- (instancetype)initWithName:(NSString *)name lapDistance:(double)lapDistance
{
    self = [super init];
    
    if (self) {
        self.name = name;
        self.lapDistance = lapDistance;
    }
    
    return self;
}

- (NSDictionary *)dictionaryRepresentation
{
    if (self.name != nil) {
        return @{ @"name": self.name, @"lapDistance": @(self.lapDistance) };
    }
    
    return nil;
}

@end
