<commit_before>//
//  LoginViewController.swift
//  Rocket.Chat.iOS
//
//  Created by <NAME> on 8/8/15.
//  Copyright © 2015 Rocket.Chat. All rights reserved.
//

import UIKit
import JSQCoreDataKit
import MMDrawerController

class LoginViewController: UIViewController {

    @IBOutlet var userNameTextField: UITextField!
    @IBOutlet var passwordTextField: UITextField!
    

    
    //variable to get the logged in user
    var currentUser = User?()
    var users = [User]()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //set password textfield to secure entry textfield
        passwordTextField.secureTextEntry = true
        
        //Add listener for the textinput for when input changes
        userNameTextField.addTarget(self, action: "textFieldDidChange", forControlEvents: UIControlEvents.EditingChanged)
        passwordTextField.addTarget(self, action: "textFieldDidChange", forControlEvents: UIControlEvents.EditingChanged)
        
        //Prefill text inputs to make login easier for developing
//        userNameTextField.text = "<EMAIL>"
//        passwordTextField.text = "<PASSWORD>"
        
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let context = delegate.stack!.context
        
        //Check for already logged in user
        let ent = entity(name: "User", context: context)
        
        let request = FetchRequest<User>(entity: ent)
        //Users that we have password for only
        request.predicate = NSPredicate(format: "password != nil")
        
        
        users = [User]()
        do{
            users = try fetch(request: request, inContext: context)
        }catch{
            print("Error fetching users \(error)")
        }
        
//        if exists {
//            loginButtonTapped(userNameTextField.text!)
//        }
        
//        if !users.isEmpty {
//            userNameTextField.text = users[0].username
//            passwordTextField.text = users[0].password
//            loginButtonTapped(users)
//        }
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    //Login action
    @IBAction func loginButtonTapped(sender: AnyObject) {
        
        
        //Check if username is empty
        
        if(userNameTextField.text == ""){
            
            //if empty change username textfield border color to red
            userNameTextField.layer.borderColor = UIColor.redColor().CGColor
            userNameTextField.layer.borderWidth = 1.0
            
        }
            
        //Check if password is empty
        
        else if(passwordTextField.text == ""){
            
            //if empty change password textfield border color to red
            passwordTextField.layer.borderColor = UIColor.redColor().CGColor
            passwordTextField.layer.borderWidth = 1.0
            
        }
            
        //if username and password is OK
            
        else if(userAndPassVerify(userNameTextField.text!, passWord:passwordTextField.text!))
        {
        
            self.view.endEditing(true)
            //get the appdelegate and store it in a variable
            let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            
            
            //THIS NEEDS TO MOVE (?)
            
            
//            let context = appDelegate.stack!.context
//            
//            let user = User(context: context, id: "NON-YET", username: userNameTextField.text!, avatar: "avatar.png", status: .ONLINE, timezone: NSTimeZone.systemTimeZone())
//            user.password = <PASSWORD>!
//            //User is automatically is added to CoreData, but not saved, so we need to call
//            //save context next.
//            //This is dump, because it writes the same user again, and again
//            
//            saveContext(context, wait: true, completion:{(error: NSError?) -> Void in
//                if let err = error {
//                    let alert = UIAlertController(title: "Alert", message: "Error \(err.userInfo)", preferredStyle: UIAlertControllerStyle.Alert)
//                    alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
//                    self.presentViewController(alert, animated: true, completion: nil)
//                }
//                
//            //set the logged in user
//            self.currentUser = user
//            
//                
//            })
            
            
            
            
            //let rootViewController = appDelegate.window!.rootViewController
            
            //get the storyboard an store it in a variable
            let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            
            
            //Create and store the center the left and the right views and keep them in variables
            
            //center view
            let centerViewController = mainStoryboard.instantiateViewControllerWithIdentifier("viewController") as! ViewController
            
            //left view
            let leftViewController = mainStoryboard.instantiateViewControllerWithIdentifier("leftView") as! LeftViewController
            
            //right view
            let rightViewController = mainStoryboard.instantiateViewControllerWithIdentifier("rightView") as! RightViewController
            
            
            //send the logged in user in the ViewController
            centerViewController.currentUser = currentUser
            
            //Set the left, right and center views as the rootviewcontroller for the navigation controller (one rootviewcontroller at a time)
            
            //Set the left view as the rootview for the navigation controller and keep it in a variable
            let leftSideNav = UINavigationController(rootViewController: leftViewController)

            //Set the center view as the rootview for the navigation controller and keep it in a variable
            let centerNav = UINavigationController(rootViewController: centerViewController)
            
            //Set the right view as the rootview for the navigation controller and keep it in a variable
            let rightNav = UINavigationController(rootViewController: rightViewController)
            
            //Create the MMDrawerController and keep it in a variable named center container
            let centerContainer:MMDrawerController = MMDrawerController(centerViewController: centerNav, leftDrawerViewController: leftSideNav,rightDrawerViewController:rightNav)
            
            
            //Open and Close gestures for the center container
            
            //Set the open gesture for the center container
            centerContainer.openDrawerGestureModeMask = MMOpenDrawerGestureMode.PanningCenterView;
            //Set the close gesture for the center container
            centerContainer.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView;
            
            
            
            
            //Set the centerContainer in the appDelegate.swift as the center container
            appDelegate.centerContainer = centerContainer
            
            //Set the rootViewController as the center container
            appDelegate.window!.rootViewController = appDelegate.centerContainer
            
            
            appDelegate.window!.makeKeyAndVisible()
            
        }
        
        //if username or password is wrong
        
        else
        {
            //create an alert
            let alert = UIAlertView(title: "Warning!", message: "Check your username / password combination", delegate: self, cancelButtonTitle: "Dismiss")
            
            //empty textfields
            userNameTextField.text = ""
            passwordTextField.text = ""

            
            //show the alert
            alert.show()
            
            //userNameTextField gets the focus
            userNameTextField.becomeFirstResponder()
            
        }
        
        
        
    }
    
    
    //Function to check username and password
    func userAndPassVerify(userName:String, passWord:String) -> Bool {
        
        
        var exists = false
        
        //If there are registered users
        if !self.users.isEmpty{
            
            for i in self.users {
                
                if i.username == userNameTextField.text{
                    exists = true
                    self.currentUser = i
                    print("CurrentUser set " + i.username)
                }
            }
            
        }
        //if user and pass is OK return true
//        if(userName == "komic" && passWord == "<PASSWORD>"){
//            
//            return true
//            
//        }
        
        
        //if user and pass exists return true
        
        if exists {
            
            return true
            
        }
            
        //if user doesn't exist
        else
        {
            
            return false
        
        }
        
    }
    
    func textFieldDidChange() {
        
        //Reset textField's border color and width
        userNameTextField.layer.borderColor = UIColor.blackColor().CGColor
        userNameTextField.layer.borderWidth = 0.0
        passwordTextField.layer.borderColor = UIColor.blackColor().CGColor
        passwordTextField.layer.borderWidth = 0.0
        
    }
    
    
    
    //Register a new acoount action<commit_msg>removed if with return exists on LoginViewController.swift when checking if user is registered<commit_after>//
//  LoginViewController.swift
//  Rocket.Chat.iOS
//
//  Created by <NAME> on 8/8/15.
//  Copyright © 2015 Rocket.Chat. All rights reserved.
//

import UIKit
import JSQCoreDataKit
import MMDrawerController

class LoginViewController: UIViewController {

    @IBOutlet var userNameTextField: UITextField!
    @IBOutlet var passwordTextField: UITextField!
    

    
    //variable to get the logged in user
    var currentUser = User?()
    var users = [User]()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //set password textfield to secure entry textfield
        passwordTextField.secureTextEntry = true
        
        //Add listener for the textinput for when input changes
        userNameTextField.addTarget(self, action: "textFieldDidChange", forControlEvents: UIControlEvents.EditingChanged)
        passwordTextField.addTarget(self, action: "textFieldDidChange", forControlEvents: UIControlEvents.EditingChanged)
        
        //Prefill text inputs to make login easier for developing
//        userNameTextField.text = "<EMAIL>"
//        passwordTextField.text = "<PASSWORD>"
        
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let context = delegate.stack!.context
        
        //Check for already logged in user
        let ent = entity(name: "User", context: context)
        
        let request = FetchRequest<User>(entity: ent)
        //Users that we have password for only
        request.predicate = NSPredicate(format: "password != nil")
        
        
        users = [User]()
        do{
            users = try fetch(request: request, inContext: context)
        }catch{
            print("Error fetching users \(error)")
        }
        
//        if exists {
//            loginButtonTapped(userNameTextField.text!)
//        }
        
//        if !users.isEmpty {
//            userNameTextField.text = users[0].username
//            passwordTextField.text = users[0].password
//            loginButtonTapped(users)
//        }
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    //Login action
    @IBAction func loginButtonTapped(sender: AnyObject) {
        
        
        //Check if username is empty
        
        if(userNameTextField.text == ""){
            
            //if empty change username textfield border color to red
            userNameTextField.layer.borderColor = UIColor.redColor().CGColor
            userNameTextField.layer.borderWidth = 1.0
            
        }
            
        //Check if password is empty
        
        else if(passwordTextField.text == ""){
            
            //if empty change password textfield border color to red
            passwordTextField.layer.borderColor = UIColor.redColor().CGColor
            passwordTextField.layer.borderWidth = 1.0
            
        }
            
        //if username and password is OK
            
        else if(userAndPassVerify(userNameTextField.text!, passWord:passwordTextField.text!))
        {
        
            self.view.endEditing(true)
            //get the appdelegate and store it in a variable
            let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            
            
            //THIS NEEDS TO MOVE (?)
            
            
//            let context = appDelegate.stack!.context
//            
//            let user = User(context: context, id: "NON-YET", username: userNameTextField.text!, avatar: "avatar.png", status: .ONLINE, timezone: NSTimeZone.systemTimeZone())
//            user.password = <PASSWORD>!
//            //User is automatically is added to CoreData, but not saved, so we need to call
//            //save context next.
//            //This is dump, because it writes the same user again, and again
//            
//            saveContext(context, wait: true, completion:{(error: NSError?) -> Void in
//                if let err = error {
//                    let alert = UIAlertController(title: "Alert", message: "Error \(err.userInfo)", preferredStyle: UIAlertControllerStyle.Alert)
//                    alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
//                    self.presentViewController(alert, animated: true, completion: nil)
//                }
//                
//            //set the logged in user
//            self.currentUser = user
//            
//                
//            })
            
            
            
            
            //let rootViewController = appDelegate.window!.rootViewController
            
            //get the storyboard an store it in a variable
            let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            
            
            //Create and store the center the left and the right views and keep them in variables
            
            //center view
            let centerViewController = mainStoryboard.instantiateViewControllerWithIdentifier("viewController") as! ViewController
            
            //left view
            let leftViewController = mainStoryboard.instantiateViewControllerWithIdentifier("leftView") as! LeftViewController
            
            //right view
            let rightViewController = mainStoryboard.instantiateViewControllerWithIdentifier("rightView") as! RightViewController
            
            
            //send the logged in user in the ViewController
            centerViewController.currentUser = currentUser
            
            //Set the left, right and center views as the rootviewcontroller for the navigation controller (one rootviewcontroller at a time)
            
            //Set the left view as the rootview for the navigation controller and keep it in a variable
            let leftSideNav = UINavigationController(rootViewController: leftViewController)

            //Set the center view as the rootview for the navigation controller and keep it in a variable
            let centerNav = UINavigationController(rootViewController: centerViewController)
            
            //Set the right view as the rootview for the navigation controller and keep it in a variable
            let rightNav = UINavigationController(rootViewController: rightViewController)
            
            //Create the MMDrawerController and keep it in a variable named center container
            let centerContainer:MMDrawerController = MMDrawerController(centerViewController: centerNav, leftDrawerViewController: leftSideNav,rightDrawerViewController:rightNav)
            
            
            //Open and Close gestures for the center container
            
            //Set the open gesture for the center container
            centerContainer.openDrawerGestureModeMask = MMOpenDrawerGestureMode.PanningCenterView;
            //Set the close gesture for the center container
            centerContainer.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView;
            
            
            
            
            //Set the centerContainer in the appDelegate.swift as the center container
            appDelegate.centerContainer = centerContainer
            
            //Set the rootViewController as the center container
            appDelegate.window!.rootViewController = appDelegate.centerContainer
            
            
            appDelegate.window!.makeKeyAndVisible()
            
        }
        
        //if username or password is wrong
        
        else
        {
            //create an alert
            let alert = UIAlertView(title: "Warning!", message: "Check your username / password combination", delegate: self, cancelButtonTitle: "Dismiss")
            
            //empty textfields
            userNameTextField.text = ""
            passwordTextField.text = ""

            
            //show the alert
            alert.show()
            
            //userNameTextField gets the focus
            userNameTextField.becomeFirstResponder()
            
        }
        
        
        
    }
    
    
    //Function to check username and password
    func userAndPassVerify(userName:String, passWord:String) -> Bool {
        
        
        var exists = false
        
        //If there are registered users
        if !self.users.isEmpty{
            
            for i in self.users {
                
                if i.username == userNameTextField.text{
                    exists = true
                    self.currentUser = i
                    print("CurrentUser set " + i.username)
                }
            }
            
        }
        //if user and pass is OK return true
//        if(userName == "komic" && passWord == "<PASSWORD>"){
//            
//            return true
//            
//        }
        
        
        //Return if exists or not
        return exists
        
    }
    
    func textFieldDidChange() {
        
        //Reset textField's border color and width
        userNameTextField.layer.borderColor = UIColor.blackColor().CGColor
        userNameTextField.layer.borderWidth = 0.0
        passwordTextField.layer.borderColor = UIColor.blackColor().CGColor
        passwordTextField.layer.borderWidth = 0.0
        
    }
    
    
    
    //Register a new acoount action
{'fraction_non_alphanumeric': 0.07225926500232595, 'fraction_numerical': 0.00186075360521011, 'mean_word_length': 1.6406441927118876, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, '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': '7390453', 'n_tokens_mistral': 4606, 'n_tokens_neox': 4439, 'n_words': 1322}