<commit_before>//
//  InformationPostingViewController.swift
//  onTheMap
//
//  Created by <NAME> on 28/02/16.
//  Copyright © 2016 mcberros. All rights reserved.
//

import UIKit
import MapKit
import CoreLocation
import AddressBookUI

class InformationPostingViewController: UIViewController {

    @IBOutlet weak var whereLabel: UILabel!
    @IBOutlet weak var whereTextField: UITextField!
    @IBOutlet weak var mediaURLTextField: UITextField!
    @IBOutlet weak var findOnTheMapButton: UIButton!
    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var submitButton: UIButton!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    var appDelegate: AppDelegate!
    var mapString: String?
    var coordinate: CLLocationCoordinate2D?
    var latitude: Double?
    var longitude: Double?

    var tapRecognizer: UITapGestureRecognizer? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        prepareUIForFirstStep()

        tapRecognizer = UITapGestureRecognizer(target: self, action: "handleSingleTap:")
        tapRecognizer?.numberOfTapsRequired = 1

        whereTextField.delegate = self
        mediaURLTextField.delegate = self
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        self.addKeyboardDismissRecognizer()
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)

        self.removeKeyboardDismissRecognizer()
    }

    @IBAction func findOnTheMapButtonTouch(sender: AnyObject) {
        if whereTextField.text!.isEmpty {
            appDelegate.showAlert(self, message: "Empty Location")
        } else {
            forwardGeocoding(whereTextField.text!)
        }
    }

    @IBAction func submitButtonTouch(sender: AnyObject) {
        if mediaURLTextField.text!.isEmpty {
            appDelegate.showAlert(self, message: "The URL is empty")
        } else {
            ParseApiClient.sharedInstance().postStudentInformation(mediaURLTextField.text!, latitude: latitude!, longitude: longitude!, mapString: mapString!) { (success, errorString) in
                if success {
                    dispatch_async(dispatch_get_main_queue()){
                        self.dismissViewControllerAnimated(true, completion: nil)
                    }
                } else {
                    dispatch_async(dispatch_get_main_queue()){
                        self.appDelegate.showAlert(self, message: errorString!)
                    }
                }
            }
        }
    }

    @IBAction func cancelButtonTouch(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    private func prepareUIForFirstStep() {
        whereLabel.hidden = false
        whereTextField.hidden = false
        findOnTheMapButton.hidden = false

        mediaURLTextField.hidden = true
        mapView.hidden = true
        submitButton.hidden = true
    }

    private func prepareUIForSecondtStep() {
        whereLabel.hidden = true
        whereTextField.hidden = true
        findOnTheMapButton.hidden = true

        mediaURLTextField.hidden = false
        mapView.hidden = false
        submitButton.hidden = false
    }

    private func forwardGeocoding(address: String) {
        CLGeocoder().geocodeAddressString(address) { (placemarks, error) in

            dispatch_async(dispatch_get_main_queue()){
                self.activityIndicator.startAnimating()
            }

            guard error == nil else {
                dispatch_async(dispatch_get_main_queue()){
                    self.appDelegate.showAlert(self, message: "Geocoding failed")
                    self.activityIndicator.stopAnimating()
                }
                return
            }

            guard (placemarks?.count > 0) else {
                dispatch_async(dispatch_get_main_queue()){
                    self.appDelegate.showAlert(self, message: "No placemarks found")
                    self.activityIndicator.stopAnimating()
                }
                return
            }

            dispatch_async(dispatch_get_main_queue()){
                self.activityIndicator.stopAnimating()
            }

            let placemark = placemarks?[0]
            let location = placemark?.location
            self.coordinate = location?.coordinate
            self.mapString = self.whereTextField!.text
            self.latitude = self.coordinate!.latitude
            self.longitude = self.coordinate!.longitude
            self.nextStep()

        }
    }

    private func nextStep(){
        guard let _ = self.longitude else {
            dispatch_async(dispatch_get_main_queue()){self.appDelegate.showAlert(self, message: "There are no coordinates for this place")}
            return
        }

        let pointAnnotation:MKPointAnnotation = MKPointAnnotation()
        pointAnnotation.coordinate = coordinate!
        mapView?.addAnnotation(pointAnnotation)
        mapView?.centerCoordinate = coordinate!

        prepareUIForSecondtStep()

    }

//    private func postStudentInformation(){
//        let urlString = ParseApiClient.Constants.BaseParseURL + ParseApiClient.Methods.GetStudentLocationsMethod
//        let url = NSURL(string: urlString)
//        let request = NSMutableURLRequest(URL: url!)
//        request.HTTPMethod = "POST"
//        request.addValue(ParseApiClient.Constants.ParseApplicationID, forHTTPHeaderField: "X-Parse-Application-Id")
//        request.addValue(ParseApiClient.Constants.RestApiKey, forHTTPHeaderField: "X-Parse-REST-API-Key")
//        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
//        let uniqueKey = UdacityApiClient.sharedInstance().userID!
//        let firstName = UdacityApiClient.sharedInstance().firstName!
//        let lastName = UdacityApiClient.sharedInstance().lastName!
//        let mediaURL = mediaURLTextField.text!
//
//        request.HTTPBody = "{\"uniqueKey\": \"\(uniqueKey)\", \"firstName\": \"\(firstName)\", \"lastName\": \"\(lastName)\",\"mapString\": \"\(mapString!)\", \"mediaURL\": \"\(mediaURL)\",\"latitude\": \(latitude!), \"longitude\": \(longitude!)}".dataUsingEncoding(NSUTF8StringEncoding)
//
//
//        let session = NSURLSession.sharedSession()
//        let task = session.dataTaskWithRequest(request) { data, response, error in
//            guard error == nil else {
//                dispatch_async(dispatch_get_main_queue()){
//                    self.appDelegate.showAlert(self, message: "Connection error")
//                }
//                return
//            }
//
//            guard let statusCode = (response as? NSHTTPURLResponse)?.statusCode where statusCode >= 200 && statusCode <= 299 else {
//                print((response as? NSHTTPURLResponse)?.statusCode)
//                dispatch_async(dispatch_get_main_queue()){self.appDelegate.showAlert(self, message: "Your request returned an invalid response")}
//                return
//            }
//
//            guard let data = data else {
//                dispatch_async(dispatch_get_main_queue()){self.appDelegate.showAlert(self, message: "No data was returned by the request")}
//                return
//            }
//
//            let parsedResult: AnyObject
//
//            do {
//                parsedResult = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
//            } catch {
//                dispatch_async(dispatch_get_main_queue()) {
//                    self.appDelegate.showAlert(self, message: "The data could not be parsed")
//                }
//                return
//            }
//
//            dispatch_async(dispatch_get_main_queue()){
//                self.dismissViewControllerAnimated(true, completion: nil)
//            }
//        }
//        task.resume()
//    }
}

extension InformationPostingViewController {
<commit_msg>Removed not necessary comment<commit_after>//
//  InformationPostingViewController.swift
//  onTheMap
//
//  Created by <NAME> on 28/02/16.
//  Copyright © 2016 mcberros. All rights reserved.
//

import UIKit
import MapKit
import CoreLocation
import AddressBookUI

class InformationPostingViewController: UIViewController {

    @IBOutlet weak var whereLabel: UILabel!
    @IBOutlet weak var whereTextField: UITextField!
    @IBOutlet weak var mediaURLTextField: UITextField!
    @IBOutlet weak var findOnTheMapButton: UIButton!
    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var submitButton: UIButton!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    var appDelegate: AppDelegate!
    var mapString: String?
    var coordinate: CLLocationCoordinate2D?
    var latitude: Double?
    var longitude: Double?

    var tapRecognizer: UITapGestureRecognizer? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        prepareUIForFirstStep()

        tapRecognizer = UITapGestureRecognizer(target: self, action: "handleSingleTap:")
        tapRecognizer?.numberOfTapsRequired = 1

        whereTextField.delegate = self
        mediaURLTextField.delegate = self
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        self.addKeyboardDismissRecognizer()
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)

        self.removeKeyboardDismissRecognizer()
    }

    @IBAction func findOnTheMapButtonTouch(sender: AnyObject) {
        if whereTextField.text!.isEmpty {
            appDelegate.showAlert(self, message: "Empty Location")
        } else {
            forwardGeocoding(whereTextField.text!)
        }
    }

    @IBAction func submitButtonTouch(sender: AnyObject) {
        if mediaURLTextField.text!.isEmpty {
            appDelegate.showAlert(self, message: "The URL is empty")
        } else {
            ParseApiClient.sharedInstance().postStudentInformation(mediaURLTextField.text!, latitude: latitude!, longitude: longitude!, mapString: mapString!) { (success, errorString) in
                if success {
                    dispatch_async(dispatch_get_main_queue()){
                        self.dismissViewControllerAnimated(true, completion: nil)
                    }
                } else {
                    dispatch_async(dispatch_get_main_queue()){
                        self.appDelegate.showAlert(self, message: errorString!)
                    }
                }
            }
        }
    }

    @IBAction func cancelButtonTouch(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    private func prepareUIForFirstStep() {
        whereLabel.hidden = false
        whereTextField.hidden = false
        findOnTheMapButton.hidden = false

        mediaURLTextField.hidden = true
        mapView.hidden = true
        submitButton.hidden = true
    }

    private func prepareUIForSecondtStep() {
        whereLabel.hidden = true
        whereTextField.hidden = true
        findOnTheMapButton.hidden = true

        mediaURLTextField.hidden = false
        mapView.hidden = false
        submitButton.hidden = false
    }

    private func forwardGeocoding(address: String) {
        CLGeocoder().geocodeAddressString(address) { (placemarks, error) in

            dispatch_async(dispatch_get_main_queue()){
                self.activityIndicator.startAnimating()
            }

            guard error == nil else {
                dispatch_async(dispatch_get_main_queue()){
                    self.appDelegate.showAlert(self, message: "Geocoding failed")
                    self.activityIndicator.stopAnimating()
                }
                return
            }

            guard (placemarks?.count > 0) else {
                dispatch_async(dispatch_get_main_queue()){
                    self.appDelegate.showAlert(self, message: "No placemarks found")
                    self.activityIndicator.stopAnimating()
                }
                return
            }

            dispatch_async(dispatch_get_main_queue()){
                self.activityIndicator.stopAnimating()
            }

            let placemark = placemarks?[0]
            let location = placemark?.location
            self.coordinate = location?.coordinate
            self.mapString = self.whereTextField!.text
            self.latitude = self.coordinate!.latitude
            self.longitude = self.coordinate!.longitude
            self.nextStep()

        }
    }

    private func nextStep(){
        guard let _ = self.longitude else {
            dispatch_async(dispatch_get_main_queue()){self.appDelegate.showAlert(self, message: "There are no coordinates for this place")}
            return
        }

        let pointAnnotation:MKPointAnnotation = MKPointAnnotation()
        pointAnnotation.coordinate = coordinate!
        mapView?.addAnnotation(pointAnnotation)
        mapView?.centerCoordinate = coordinate!

        prepareUIForSecondtStep()

    }
}

extension InformationPostingViewController {

{'fraction_non_alphanumeric': 0.09161808132051184, 'fraction_numerical': 0.0026501097902627392, 'mean_word_length': 2.263652087966395, 'pattern_counts': {'":': 7, '<': 6, '<?xml version=': 0, '>': 8, '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': '2759422', 'n_tokens_mistral': 3569, 'n_tokens_neox': 3356, 'n_words': 684}