<commit_before>//
//  MessageViewController.swift
//  LocationTalk
//
//  Created by 鄭宇翔 on 2017/1/5.
//  Copyright © 2017年 鄭宇翔. All rights reserved.
//

import UIKit
import GooglePlaces

class MessageViewController: UIViewController, SendMessageHeaderViewDataSource, MyPlaceServicesDelegate, CLLocationManagerDelegate, LocationViewControllerDelegate {

    @IBOutlet weak var headerView: SendMessageHeaderView! {
        didSet {
            headerView.delegate = self
        }
    }
    @IBOutlet weak var contentTextView: UITextView! {
        didSet {
            contentTextView.text = ""
        }
    }
    
    var friendSelected: FriendInfo!
    let userPermission = UserPermission()
    var placeSelected: GMSPlace!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        userPermission.location()
        
        let myPlaceServices = MyPlaceServices.sharedInstance
        myPlaceServices.delegate = self
        myPlaceServices.currentPlace()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    deinit {
        print("MessageViewController deinit")
    }
    
    @IBAction func sendButtonPressed(_ sender: UIBarButtonItem) {
        let message = Message.init(target: friendSelected,
                                   place: placeSelected,
                                   time: headerView.timeLabel.text!,
                                   content: contentTextView.text)
        let messageUtility = Database().message()
        messageUtility?.send(message: message)
        
        dismiss(animated: true, completion: nil)
    }
    
    @IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
        dismiss(animated: true, completion: nil)
    }


    // MARK: - Navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        if segue.identifier == Constants.Segue.messageToLocation {
            let locationVC = segue.destination as! LocationViewController
            locationVC.placeSelected = placeSelected
            locationVC.delegate = self
        }
    }
}

// MARK: - SendMessageHeaderViewDataSource
extension MessageViewController {
    func userNameInTheHeader() -> String {
        return friendSelected.username
    }
    
    func didLocationChangeBtnPressed() {
        performSegue(withIdentifier: Constants.Segue.messageToLocation, sender: nil)
    }
}

// MARK: - MyPlaceServiceProtocol
extension MessageViewController {
    func get(currentPlace: GMSPlace?, error: Error?) {
        if let error = error {
            print("\(error.localizedDescription)")
            return
        }
        
        placeSelected = currentPlace
        DispatchQueue.main.async {
            self.headerView.locationLabel.text = currentPlace!.formattedAddress
        }
    }
}

// MARK: - LocationViewControllerDelegate
extension MessageViewController {
    func didChangeLocation(newPlace: GMSPlace) {
        placeSelected = newPlace
        self.headerView.locationLabel.text = newPlace.formattedAddress
    }<commit_msg>Modify the place statement.<commit_after>//
//  MessageViewController.swift
//  LocationTalk
//
//  Created by 鄭宇翔 on 2017/1/5.
//  Copyright © 2017年 鄭宇翔. All rights reserved.
//

import UIKit
import GooglePlaces

class MessageViewController: UIViewController, SendMessageHeaderViewDataSource, MyPlaceServicesDelegate, CLLocationManagerDelegate, LocationViewControllerDelegate {

    @IBOutlet weak var headerView: SendMessageHeaderView! {
        didSet {
            headerView.delegate = self
        }
    }
    @IBOutlet weak var contentTextView: UITextView! {
        didSet {
            contentTextView.text = ""
        }
    }
    
    var friendSelected: FriendInfo!
    let userPermission = UserPermission()
    var placeSelected: GMSPlace!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        userPermission.location()
        
        let myPlaceServices = MyPlaceServices.sharedInstance
        myPlaceServices.delegate = self
        myPlaceServices.currentPlace()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    deinit {
        print("MessageViewController deinit")
    }
    
    @IBAction func sendButtonPressed(_ sender: UIBarButtonItem) {
        let message = Message.init(target: friendSelected,
                                   place: placeSelected,
                                   time: headerView.timeLabel.text!,
                                   content: contentTextView.text)
        let messageUtility = Database().message()
        messageUtility?.send(message: message)
        
        dismiss(animated: true, completion: nil)
    }
    
    @IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
        dismiss(animated: true, completion: nil)
    }


    // MARK: - Navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        if segue.identifier == Constants.Segue.messageToLocation {
            let locationVC = segue.destination as! LocationViewController
            locationVC.placeSelected = placeSelected
            locationVC.delegate = self
        }
    }
}

// MARK: - SendMessageHeaderViewDataSource
extension MessageViewController {
    func userNameInTheHeader() -> String {
        return friendSelected.username
    }
    
    func didLocationChangeBtnPressed() {
        performSegue(withIdentifier: Constants.Segue.messageToLocation, sender: nil)
    }
}

// MARK: - MyPlaceServiceProtocol
extension MessageViewController {
    func get(currentPlace: GMSPlace?, error: Error?) {
        if let error = error {
            print("\(error.localizedDescription)")
            return
        }
        
        placeSelected = currentPlace
        DispatchQueue.main.async {
            self.headerView.locationLabel.text = currentPlace!.formattedAddress
        }
    }
}

// MARK: - LocationViewControllerDelegate
extension MessageViewController {
    func didChangeLocation(newPlace: GMSPlace) {
        placeSelected = newPlace
        self.headerView.locationLabel.text = "\(newPlace.name)(\(newPlace.formattedAddress!))"
    }
{'fraction_non_alphanumeric': 0.07714115189495672, 'fraction_numerical': 0.0029841838257236644, 'mean_word_length': 2.3785282258064515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3506185', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1684, 'n_words': 403}