/***
 * 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.
***/
//
//  RunDetailsInterfaceController.m
//  tapalap
//
//  Created by Jeff Kelley on 2/27/15.
//  Copyright (c) 2015 Pragmatic Bookshelf. All rights reserved.
//


#import "RunDetailsInterfaceController.h"

#import "Run.h"

#import "LapDetailRowController.h"


@interface RunDetailsInterfaceController ()

@property (weak) IBOutlet WKInterfaceTable *lapsTable;
@property (weak) IBOutlet WKInterfaceGroup *lapsTableContainer;

@property (weak) IBOutlet WKInterfaceLabel *runDateLabel;
@property (weak) IBOutlet WKInterfaceLabel *runDistanceLabel;
@property (weak) IBOutlet WKInterfaceLabel *runPaceLabel;

@end


@implementation RunDetailsInterfaceController

- (void)awakeWithContext:(id)context
{
    [super awakeWithContext:context];
    
    if ([context isKindOfClass:[Run class]]) {
        [self configureForRun:(Run *)context];
    }
}

- (void)configureForRun:(Run *)run
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = NSDateFormatterMediumStyle;
    dateFormatter.timeStyle = NSDateFormatterNoStyle;
    
    [self.runDateLabel setText:[dateFormatter stringFromDate:run.startDate]];
    
    NSLengthFormatter *lengthFormatter = [[NSLengthFormatter alloc] init];
    [self.runDistanceLabel setText:[lengthFormatter stringFromMeters:run.distance]];
    
    [self.lapsTable setNumberOfRows:run.laps.count withRowType:@"Lap"];
    
    for (NSUInteger i = 0; i < [self.lapsTable numberOfRows]; i++) {
        LapDetailRowController *rowController =
        [self.lapsTable rowControllerAtIndex:i];
        
        [rowController configureForLap:[run.laps[i] doubleValue] atIndex:i];
    }
    
    NSDateComponentsFormatter *paceFormatter =
    [[NSDateComponentsFormatter alloc] init];
    
    [self.runPaceLabel setText:[paceFormatter stringFromTimeInterval:run.pace]];
}

@end
