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


#import "RunLogInterfaceController.h"

#import "Run.h"

#import "RunLogRowController.h"

@interface RunLogInterfaceController()

@property (strong) NSArray *runs;
@property (weak, nonatomic) IBOutlet WKInterfaceTable *runTable;

// Formatters
@property (readonly, nonatomic) NSDateFormatter *dateFormatter;
@property (readonly, nonatomic) NSLengthFormatter *distanceFormatter;
@property (readonly, nonatomic) NSDateComponentsFormatter *durationFormatter;

@end


@implementation RunLogInterfaceController

- (instancetype)init
{
    self = [super init];
    
    if (self ) {
        Run *run = [[Run alloc] initWithStartDate:[NSDate date]
                                             laps:@[@(6.0 * 60.0),
                                                    @(6.0 * 60.0),
                                                    @(6.0 * 60.0),
                                                    @(6.0 * 60.0),
                                                    @(6.0 * 60.0)]
                                         distance:5000];
        
        self.runs = @[run, run, run, run];
    }
    
    return self;
}

- (void)willActivate
{
    [super willActivate];
    
    [self.runTable setNumberOfRows:self.runs.count 
                       withRowType:@"RunRow"];
    
    for (NSInteger i = 0; i < [self.runTable numberOfRows]; i++) { 
        [self configureRowController:[self.runTable rowControllerAtIndex:i]
                              forRun:self.runs[i]];
    }
}

@synthesize dateFormatter = _dateFormatter;

- (NSDateFormatter *)dateFormatter
{
    if (_dateFormatter == nil) {
        _dateFormatter = [[NSDateFormatter alloc] init];
        _dateFormatter.dateStyle = NSDateFormatterShortStyle;
    }
    
    return _dateFormatter;
}

@synthesize distanceFormatter = _distanceFormatter;

- (NSLengthFormatter *)distanceFormatter
{
    if (_distanceFormatter == nil) {
        _distanceFormatter = [[NSLengthFormatter alloc] init];
    }
    
    return _distanceFormatter;
}

@synthesize durationFormatter = _durationFormatter;

- (NSDateComponentsFormatter *)durationFormatter
{
    if (_durationFormatter == nil) {
        _durationFormatter = [[NSDateComponentsFormatter alloc] init];
        
        _durationFormatter.unitsStyle =
        NSDateComponentsFormatterUnitsStylePositional;
    }
    
    return _durationFormatter;
}

- (void)configureRowController:(RunLogRowController *)rowController
                        forRun:(Run *)run
{
    rowController.dateFormatter = self.dateFormatter;
    rowController.distanceFormatter = self.distanceFormatter;
    rowController.durationFormatter = self.durationFormatter;
    
    [rowController configureForDate:run.startDate
                           distance:run.distance
                           duration:run.duration];
}

- (void)awakeWithContext:(id)context
{
    [super awakeWithContext:context];
    
    if ([context isKindOfClass:[Run class]]) {
        [self becomeCurrentPage];
        
        self.runs = [@[context] arrayByAddingObjectsFromArray:self.runs];
        [self.runTable insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:0]
                               withRowType:@"RunRow"];
        
        RunLogRowController *rowController = [self.runTable rowControllerAtIndex:0];
        [self configureRowController:rowController
                              forRun:context];
    }
}

- (id)contextForSegueWithIdentifier:(NSString *)segueIdentifier
                            inTable:(WKInterfaceTable *)table
                           rowIndex:(NSInteger)rowIndex
{
    id context = nil;
    
    if ([segueIdentifier isEqualToString:@"RunDetails"]) {
        context = self.runs[rowIndex];
    }
    
    return context;
}

- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex
{
    NSLog(@"User tapped on row %lu", (unsigned long)rowIndex);
}

@end
