<commit_before>//
//  StatisticsViewController.swift
//  Mr-Ride
//
//  Created by <NAME> on 5/23/16.
//  Copyright © 2016 AppWorks School <NAME>. All rights reserved.
//

import UIKit
import MapKit
import CoreData
import Social

class StatisticsViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
    
    var coordToUse = [CLLocationCoordinate2D]()
    
    let locationManager = CLLocationManager()
    
    let recordModel = DataManager.sharedDataManager
    
    var timestamp: NSDate?
    var distance: Double?
    var averageSpeed: Double?
    var calories: Double?
    var duration: Double?
    var locations: [Locations]=[]
    
    var isFromHistory = false
    
    weak var delegate: NewRecordViewControllerDelegate?  //NewRecordViewController的 Delegate
    
    @IBOutlet var statisticsView: StatisticsView!
    
    @IBAction func BackOrDoneButtonTapped(sender: AnyObject) {
        
        if isFromHistory == true {
            print("BackButtonTapped")
            self.navigationController?.popToRootViewControllerAnimated(true)
        } else {
            print("DoneButtonTapped")
            
            delegate?.showLabel()
            
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }
    
    @IBAction func shareButtonTapped(sender: UIButton) {
        share()
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        print("____StatisticsViewDidLoad")
        
        recordModel.fetchRecordsCoreData()
        statisticsView.mapView.delegate = self
        
        uploadRecord()
        showRoute()
        
        if isFromHistory == false {
            navigationItem.leftBarButtonItem?.title = "Done"
            
        } else {
            navigationItem.leftBarButtonItem?.title = "< Back"
        }
        
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        print("____StatisticsViewWillAppear")
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        statisticsView.gradient.frame = self.view.bounds
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        
        print("____StatisticViewWillDisappear")
        
    }
    
    deinit {
        
        print("____Leave Statistics Page")
    }
    
    func uploadRecord() {
        
        statisticsView.distanceValue.text = "\(Int(distance!)) m"
        statisticsView.caloriesValue.text = "\(Int(calories!)) kcal"
        statisticsView.totalTimeValue.text = "\(timerString(duration!))"
        statisticsView.averageSpeedValue.text = "\(Int(averageSpeed!)) km/hr"
        navigationItem.title = "\(dateString(timestamp!))"
    }
    
    //MARK: Helper Method<commit_msg>handling upwrapping distance, calories, averageSpeed, durations<commit_after>//
//  StatisticsViewController.swift
//  Mr-Ride
//
//  Created by <NAME> on 5/23/16.
//  Copyright © 2016 AppWorks School <NAME>. All rights reserved.
//

import UIKit
import MapKit
import CoreData
import Social

class StatisticsViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
    
    var coordToUse = [CLLocationCoordinate2D]()
    
    let locationManager = CLLocationManager()
    
    let recordModel = DataManager.sharedDataManager
    
    var timestamp: NSDate?
    var distance: Double = 0.0
    var averageSpeed: Double = 0.0
    var calories: Double = 0.0
    var duration: Double = 0.0
    var locations: [Locations]=[]
    
    var isFromHistory = false
    
    weak var delegate: NewRecordViewControllerDelegate?  //NewRecordViewController的 Delegate
    
    @IBOutlet var statisticsView: StatisticsView!
    
    @IBAction func BackOrDoneButtonTapped(sender: AnyObject) {
        
        if isFromHistory == true {
            print("BackButtonTapped")
            self.navigationController?.popToRootViewControllerAnimated(true)
        } else {
            print("DoneButtonTapped")
            
            delegate?.showLabel()
            
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }
    
    @IBAction func shareButtonTapped(sender: UIButton) {
        share()
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        print("____StatisticsViewDidLoad")
        
        recordModel.fetchRecordsCoreData()
        statisticsView.mapView.delegate = self
        
        uploadRecord()
        showRoute()
        
        if isFromHistory == false {
            navigationItem.leftBarButtonItem?.title = "Done"
            
        } else {
            navigationItem.leftBarButtonItem?.title = "< Back"
        }
        
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        print("____StatisticsViewWillAppear")
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        statisticsView.gradient.frame = self.view.bounds
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        
        print("____StatisticViewWillDisappear")
        
    }
    
    deinit {
        
        print("____Leave Statistics Page")
    }
    
    func uploadRecord() {
        
        statisticsView.distanceValue.text = "\(Int(distance)) m"
        statisticsView.caloriesValue.text = "\(Int(calories)) kcal"
        statisticsView.totalTimeValue.text = "\(timerString(duration))"
        statisticsView.averageSpeedValue.text = "\(Int(averageSpeed)) km/hr"
        navigationItem.title = "\(dateString(timestamp!))"
    }
    
    //MARK: Helper Method
{'fraction_non_alphanumeric': 0.08787983425414364, 'fraction_numerical': 0.004834254143646409, 'mean_word_length': 2.193495038588754, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '128237', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1580, 'n_words': 304}