 import FBSDKCoreKit
 import FBSDKLoginKit
 import FBSDKShareKit
+import SwiftyJSON
 
 class ViewController: UIViewController, FBSDKLoginButtonDelegate {
     @IBOutlet weak var txtLogin: UITextField!

    
    @IBAction func loginButtonAction(sender: UIButton) {
        
        if ((txtLogin.text != nil && txtPassword.text != nil)) {
            let alertController = UIAlertController(title: "On The Map", message:
                "Please fill both the username and password fields", preferredStyle: UIAlertControllerStyle.Alert)
            alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
            
            self.presentViewController(alertController, animated: true, completion: nil)

        let session = NSURLSession.sharedSession()
        
        let task = session.dataTaskWithRequest(request) { data, response, error in

            let newData = data?.subdataWithRange(NSMakeRange(5, data!.length - 5)) /* subset response data! */
            print(NSString(data: newData!, encoding: NSUTF8StringEncoding))
            
            if error != nil { // Handle error
                let alertController = UIAlertController(title: "On The Map", message:
                    "There was an error loggin in.", preferredStyle: UIAlertControllerStyle.Alert)
                alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
                
                self.presentViewController(alertController, animated: true, completion: nil)
                
                return
                
            }
            
            let json = JSON(data: newData!)
            
            if (json["status"] == 403) {
                let alertController = UIAlertController(title: "On The Map", message:
                    json["error"].string, preferredStyle: UIAlertControllerStyle.Alert)
                alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
                NSOperationQueue.mainQueue().addOperationWithBlock {
                    self.presentViewController(alertController, animated: true, completion: nil)
                }


            }
            
        }
        
