//
//  FacebookGraphAPIManager.swift
//  Vendoo
//
//  Created by Okechi Onyeje on 5/26/16.
//  Copyright © 2016 Okechi Onyeje. All rights reserved.
//

import Foundation
import OAuthSwift
import FBSDKCoreKit
import FBSDKLoginKit
import AeroGearHttp
import AeroGearOAuth2
//import OAuthSwift


/*
 NOTES:
 I am able to authorize application for use with facebook but cannot deathorize and completion block of authorization code is not being called -> need to figure this out
 */
class FacebookGraphAPIManager: NSObject {

    //API Manager class variables
    //----------------------------------------------//
    static let sharedInstance = FacebookGraphAPIManager()
    
    let graphBaseURL = "graph.facebook.com"
    
    private var apiKey: String!
    private var apiSecret: String!
    var isAuthorized: Bool = NSUserDefaults.standardUserDefaults().boolForKey("fbAuthorized")
    //---------------------------------------------//
    
    override init(){
        if let path = NSBundle.mainBundle().pathForResource("Services", ofType: "plist"), dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject] {
            // use swift dictionary as normal
            
            self.apiKey = ((dict["Facebook"] as! Dictionary<String, AnyObject>)["consumerKey"] as! String)
            self.apiSecret = ((dict["Facebook"] as! Dictionary<String, AnyObject>)["consumerSecret"] as! String)
        }
    }

    
}


//MARK: - OAuth Methods
extension FacebookGraphAPIManager {
    
    func authorizeApp(viewcontroller: UIViewController){
        
        let http = Http(baseURL: self.graphBaseURL)                       // [1]
        let facebookConfig = FacebookConfig(    // [2]
            clientId: self.apiKey,
            clientSecret: self.apiSecret,
            scopes:["publish_actions"])
        let oauth2Module = AccountManager.addFacebookAccount(facebookConfig)  // [3]
        
        
        http.authzModule = oauth2Module         // [4]
        
        //dispatch_async(dispatch_main(), <#T##block: dispatch_block_t##dispatch_block_t##() -> Void#>)
        http.request(.GET, path: "/get", completionHandler: {
            (response, error) in   // [5]
            // handle response
            
            //print(response)
            /*if((error != nil)){
                
                //once everything is authorized save true value to the authorization boolean
                
                 NSUserDefaults.standardUserDefaults().setBool(true, forKey: "fbAuthorized")
                 self.isAuthorized = NSUserDefaults.standardUserDefaults().boolForKey("fbAuthorized")
                
            }*/
        })
        
       // self.isAuthorized = oauth2Module.isAuthorized()
 
        
        /*
        oauth2Module.revokeAccess({(response, error) in
            if (error != nil) {
                // do something with error
                print("accessrevoked")
            }
            // do domething
        })
 */
        
        
    }
    
    func deAuthorizeApp(viewcontroller: UIViewController){
        let facebookConfig = FacebookConfig(    // [2]
            clientId: self.apiKey,
            clientSecret: self.apiSecret,
            scopes:["publish_actions"])
        let oauth2Module = AccountManager.addFacebookAccount(facebookConfig)  // [3]
        oauth2Module.revokeAccess({(response, error) in
            if (error != nil) {
                // do something with error
            }
            // do domething
        })
        
        
    }

}
