//
//  RunDetailsInterfaceController.swift
//  TapALap
//
//  Created by Jeff Kelley on 4/28/15.
//  Copyright (c) 2015 Pragmatic Bookshelf. All rights reserved.
//

import WatchKit

class LapDetailRowController: NSObject {
    
    @IBOutlet weak var lapNumberLabel: WKInterfaceLabel!
    @IBOutlet weak var lapDurationLabel: WKInterfaceLabel!
    
    lazy var durationFormatter: NSDateComponentsFormatter = {
        let durationFormatter = NSDateComponentsFormatter()
        durationFormatter.unitsStyle = .Positional
        return durationFormatter
    }()
    
    func configureForLap(lap: NSTimeInterval, atIndex index: UInt) {
        lapDurationLabel.setText(durationFormatter.stringFromTimeInterval(lap))
        lapNumberLabel.setText("\(index + 1)")
    }
    
}

class RunDetailsInterfaceController: WKInterfaceController {

    @IBOutlet weak var lapsTable: WKInterfaceTable!
    
    @IBOutlet weak var runDateLabel: WKInterfaceLabel!
    @IBOutlet weak var runDistanceLabel: WKInterfaceLabel!
    @IBOutlet weak var runPaceLabel: WKInterfaceLabel!
    
    lazy var dateFormatter: NSDateFormatter = {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = .MediumStyle
        dateFormatter.timeStyle = .NoStyle
        return dateFormatter
    }()

    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)
        
        if let run = context as? Run {
            configureForRun(run)
        }
    }
    
    func configureForRun(run: Run) {
        runDateLabel.setText(dateFormatter.stringFromDate(run.startDate))
        
        let lengthFormatter = NSLengthFormatter()
        runDistanceLabel.setText(lengthFormatter.stringFromMeters(run.distance))
        
        lapsTable.setNumberOfRows(run.laps.count, withRowType: "LapRow")
        
        for i in 0 ..< lapsTable.numberOfRows {
            if let rowController = lapsTable.rowControllerAtIndex(i) as?
                LapDetailRowController {
                let lapTime: NSTimeInterval = run.laps[i]
                rowController.configureForLap(lapTime, atIndex: UInt(i))
            }
        }
        
        let paceFormatter = NSDateComponentsFormatter()
        runPaceLabel.setText(paceFormatter.stringFromTimeInterval(run.pace))
    }
    
}
