<commit_before>//
//  AddFriend.swift
//  LocationTalk
//
//  Created by 鄭宇翔 on 2016/12/30.
//  Copyright © 2016年 鄭宇翔. All rights reserved.
//

import Foundation
import Firebase

protocol AddFriendDelegate: class {
    func didSearchFriend(email: String, username: String)
    func notSearchFriend()
    func didCheckThisEmail(result: FriendState)
}


class AddFriend: AccountProtocol {
    let ref: FIRDatabaseReference!
    fileprivate var _refHandle: FIRDatabaseHandle!
    weak var delagate: AddFriendDelegate?
    
    init() {
        self.ref = FIRDatabase.database().reference()
    }
    
    func searchFriends(_ target: String) {
        let friendNode = self.emailToNode(target)
        _refHandle = ref.child(friendNode).observe(.value, with: { [weak self] (snapshot) in
            guard let strongSelf = self else {return}
            if (snapshot.exists()) {
                let userInfo = snapshot.value as! Dictionary<String, Any>
                strongSelf.delagate?.didSearchFriend(email: (userInfo[Constants.FirebaseKey.email] as? String)!, username: (userInfo[Constants.FirebaseKey.username] as? String)!)
            } else {
                strongSelf.delagate?.notSearchFriend()
            }
            strongSelf.ref.child(friendNode).removeObserver(withHandle: strongSelf._refHandle)
        })
    }
    
    func check(_ email: String) {
        
        var availed: Bool = true
        
        let myState = MyState.sharedInstance
        if (myState.email! == email) {
            availed = false
        }
        
        let myNode = self.emailToNode(myState.email!)
        let friendNode = self.emailToNode(email)
        _refHandle = ref.child("\(myNode)/friend").observe(.value, with: { [weak self](snapshot) in
            guard let strongSelf = self else {return}
            if (snapshot.exists()) {
                let infos = snapshot.value as! Dictionary<String, Any>
                for (k, v) in infos {
                    if (k == friendNode) {
                        let friendInfo = FriendInfo.init(v as! Dictionary<String, Any>)
                        strongSelf.delagate?.didCheckThisEmail(result: friendInfo.state)
                        availed = false
                    }
                }
            }
            if (availed) {
                strongSelf.delagate?.didCheckThisEmail(result: .none)
            }
            strongSelf.ref.child("\(myNode)/friend").removeObserver(withHandle: strongSelf._refHandle)
        })
        
    }
    
    func invite(_ email: String, username: String) {
        let friendNode = self.emailToNode(email)<commit_msg>Minor fix.<commit_after>//
//  AddFriend.swift
//  LocationTalk
//
//  Created by 鄭宇翔 on 2016/12/30.
//  Copyright © 2016年 鄭宇翔. All rights reserved.
//

import Foundation
import Firebase

protocol AddFriendDelegate: class {
    func didSearchFriend(email: String, username: String)
    func notSearchFriend()
    func didCheckThisEmail(result: FriendState)
}


class AddFriend: AccountProtocol {
    let ref: FIRDatabaseReference!
    fileprivate var _refHandle: FIRDatabaseHandle!
    weak var delagate: AddFriendDelegate?
    
    init() {
        self.ref = FIRDatabase.database().reference()
    }
    
    func searchFriends(_ target: String) {
        let friendNode = self.emailToNode(target)
        _refHandle = ref.child(friendNode).observe(.value, with: { [weak self] (snapshot) in
            guard let strongSelf = self else {return}
            if (snapshot.exists()) {
                let userInfo = snapshot.value as! Dictionary<String, Any>
                strongSelf.delagate?.didSearchFriend(email: (userInfo[Constants.FirebaseKey.email] as? String)!, username: (userInfo[Constants.FirebaseKey.username] as? String)!)
            } else {
                strongSelf.delagate?.notSearchFriend()
            }
            strongSelf.ref.child(friendNode).removeObserver(withHandle: strongSelf._refHandle)
        })
    }
    
    func check(_ email: String) {
        
        var availed: Bool = true
        
        let myState = MyState.sharedInstance
        if (myState.email! == email) {
            availed = false
        }
        
        let myNode = self.emailToNode(myState.email!)
        let friendNode = self.emailToNode(email)
        _refHandle = ref.child("\(myNode)/friend").observe(.value, with: { [weak self](snapshot) in
            guard let strongSelf = self else {return}
            strongSelf.ref.child("\(myNode)/friend").removeObserver(withHandle: strongSelf._refHandle)
            if (snapshot.exists()) {
                let infos = snapshot.value as! Dictionary<String, Any>
                for (k, v) in infos {
                    if (k == friendNode) {
                        let friendInfo = FriendInfo.init(v as! Dictionary<String, Any>)
                        strongSelf.delagate?.didCheckThisEmail(result: friendInfo.state)
                        availed = false
                    }
                }
            }
            if (availed) {
                strongSelf.delagate?.didCheckThisEmail(result: .none)
            }
        })
        
    }
    
    func invite(_ email: String, username: String) {
        let friendNode = self.emailToNode(email)
{'fraction_non_alphanumeric': 0.1019241760335302, 'fraction_numerical': 0.004572299485616308, 'mean_word_length': 2.0120481927710845, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '1525754', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1443, 'n_words': 328}