/***
 * 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.
***/
//
//  TrackConfigurationInterfaceController.m
//  TapALap
//
//  Created by Jeff Kelley on 5/8/15.
//  Copyright (c) 2015 Pragmatic Bookshelf. All rights reserved.
//

#import "TrackConfigurationInterfaceController.h"

@interface TrackConfigurationInterfaceController ()

@property (weak, nonatomic) IBOutlet WKInterfaceButton *trackNameButton;
@property (weak, nonatomic) IBOutlet WKInterfaceLabel *lapCountLabel;
@property (weak, nonatomic) IBOutlet WKInterfaceLabel *distanceUnitLabel;

@property NSUInteger lapCount;
@property NSUInteger distanceUnitCount;

@property (readonly, nonatomic) double lapDistance;
@property (copy, nonatomic) NSString *trackName;

@end


@implementation TrackConfigurationInterfaceController

- (instancetype)init
{
    self = [super init];
    
    if (self) {
        self.lapCount = 1;
        self.distanceUnitCount = 1;
    }
    
    return self;
}

- (BOOL)usesMetricSystem
{
    return [[[NSLocale currentLocale] objectForKey:
             NSLocaleUsesMetricSystem] boolValue];
}

- (double)lapDistance
{
    double metersPerDistanceUnit = [self usesMetricSystem] ? 1000.0 : 1609.34;
    
    return (metersPerDistanceUnit * self.distanceUnitCount) / self.lapCount;
}

- (void)setTrackName:(NSString *)trackName
{
    _trackName = [trackName copy];
    
    [self updateTrackNameButton:trackName];
}

- (void)updateTrackNameButton:(NSString *)trackName
{
    [self.trackNameButton setTitle:
     [NSString stringWithFormat:@"Selected Track: %@", trackName ?: @"Gym"]];
}

- (void)awakeWithContext:(id)context
{
    [super awakeWithContext:context];
    
    self.trackName = [[NSUserDefaults standardUserDefaults]
                      stringForKey:@"TrackName"];
    
    [self distanceUnitCountSliderAdjusted:1.0f];
}

- (IBAction)trackNameButtonPressed
{
    NSArray *suggestions = @[@"Gym", @"Indoor Track", @"Outdoor Track"];
    
    [self presentTextInputControllerWithSuggestions:suggestions
                                   allowedInputMode:WKTextInputModePlain
                                         completion:^(NSArray *results) {
                                             self.trackName = results.firstObject;
                                         }];
}

- (IBAction)lapCountSliderAdjusted:(float)value
{
    self.lapCount = value;
    
    [self.lapCountLabel setText:[NSString stringWithFormat:@"%lu lap%@ equal to",
                                 (unsigned long)self.lapCount,
                                 self.lapCount > 1 ? @"s are" : @" is"]];
}

- (IBAction)distanceUnitCountSliderAdjusted:(float)value
{
    self.distanceUnitCount = value;
    
    [self.distanceUnitLabel setText:[NSString stringWithFormat:@"%lu %@%@",
                                     (unsigned long)self.distanceUnitCount,
                                     [self usesMetricSystem] ? @"km" : @"mile",
                                     (![self usesMetricSystem] &&
                                      self.distanceUnitCount > 1) ? @"s" : @"" ]];
}

- (IBAction)doneButtonPressed
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults setObject:self.trackName forKey:@"TrackName"];
    [defaults setDouble:self.lapDistance forKey:@"LapDistance"];
    
    [self dismissController];
}

@end
