<commit_before>//
//  ViewController.swift
//  SwiftDemo
//
//  Created by <NAME> on 18/05/16.
//  Copyright © 2016 LoginRadius Inc. All rights reserved.
//

import LoginRadiusSDK
import Eureka
/* Google Native SignIn
import GoogleSignIn
*/
class ViewController: FormViewController
/* Google Native SignIn
, GIDSignInUIDelegate
*/
{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Check if already login
        let defaults = UserDefaults.standard
        let user = defaults.integer(forKey: "isLoggedIn")
        if (user == 1) {
            self.performSegue(withIdentifier: "profile", sender: self);
        }
        
        /* Google Native SignIn
        GIDSignIn.sharedInstance().uiDelegate = self
        */
        
        self.navigationController?.navigationBar.topItem?.title = "Login Radius iOS pod 3.4.0"
        self.form = Form()
        
        //Create UI forms
        form +++ Section("")
            <<< ButtonRow("Login LR API")
            {
                $0.title = "Login"
                }.onCellSelection{ cell, row in
                    self.traditionalLogin()
            }
            <<< ButtonRow("Register LR API")
            {
                $0.title = "Register"
                }.onCellSelection{ cell, row in
                    self.traditionalRegistration()
            }
            <<< ButtonRow("Forgot LR API")
            {
                $0.title = "Forgot Password"
                }.onCellSelection{ row in
                    self.forgotPassword()
            }
            <<< ButtonRow("Social Login Only")
            {
                $0.title = $0.tag
                }.onCellSelection{ cell, row in
                    self.showSocialOnly()
            }
            let socialNativeLoginEnabled = LoginRadiusSDK.sharedInstance().enableGoogleNativeInHosted ||  LoginRadiusSDK.sharedInstance().enableFacebookNativeInHosted
        
            if(socialNativeLoginEnabled)
            {
                let nativeSocialLoginSection = Section("Native Social Login")
                form +++ nativeSocialLoginSection
                
                //WARNING FOR DEV/Testers:
                //whenever the googleNative is set to true or facebookNative to true,
                //it is set to true the safariViewController will save it on a cookie
                //so if you immediately set the googleNative to false in the plist, and run the app
                //it won't reflect the changes on the cookie of the default cdn site. (meaning it still thinks you had googleNative set to true)
                //the workaround is to reset safari's cache, so on the Simulator go to Preferences > Safari > Clear History and Website Data
                
                if (LoginRadiusSDK.sharedInstance().enableGoogleNativeInHosted)
                {
                    NotificationCenter.default.addObserver(self, selector: #selector(self.showProfileController), name: Notification.Name("userAuthenticatedFromNativeGoogle"), object: nil)
                    NotificationCenter.default.addObserver(self, selector: #selector(self.showNativeGoogleLogin), name: Notification.Name("googleNative"), object: nil)

                    nativeSocialLoginSection <<< ButtonRow("Native Google")
                    {
                        $0.title = "Google"
                        }.onCellSelection{ cell, row in
                            self.showNativeGoogleLogin()
                    }
                }
                
                if (LoginRadiusSDK.sharedInstance().enableFacebookNativeInHosted)
                {
                    NotificationCenter.default.addObserver(self, selector: #selector(self.showNativeFacebookLogin), name: Notification.Name("facebookNative"), object: nil)

                    nativeSocialLoginSection <<< ButtonRow("Native Facebook")
                    {
                        $0.title = "Facebook"
                        }.onCellSelection{ cell, row in
                            self.showNativeFacebookLogin()
                    }
                }

                
            }

    }
    
    deinit
    {
        NotificationCenter.default.removeObserver(self)
    }
    
    func traditionalRegistration() {
        LoginRadiusManager.sharedInstance().registration(withAction: "registration", in: self, completionHandler: { (success, error) in
            if (success) {
                print("successfully registered");
                self.showProfileController();
            } else if let err = error {
                self.showAlert(title:"ERROR",message:err.localizedDescription)
            }
        });
    }
    
    func traditionalLogin() {
        LoginRadiusManager.sharedInstance().registration(withAction: "login", in: self, completionHandler: { (success, error) in
            if (success) {
                print("successfully logged in");
                self.showProfileController();
            } else if let err = error {
                self.showAlert(title:"ERROR",message:err.localizedDescription)
            }
        });
    }
    
    func forgotPassword() {
        LoginRadiusManager.sharedInstance().registration(withAction: "forgotpassword", in: self, completionHandler: { (success, error) in
            if (success) {
                print("successfully request forgot password");
                self.showAlert(title: "SUCCESS", message: "Forgot Password Requested, check your email inbox to reset")
            } else if let err = error {
                self.showAlert(title:"ERROR",message:err.localizedDescription)
            }
        });
    }
    
    func showSocialOnly() {
        LoginRadiusManager.sharedInstance().registration(withAction: "social", in: self, completionHandler: { (success, error) in
            if (success) {
                print("successfully logged in with social hosted page");
                self.showProfileController();
            } else if let err = error {
                self.showAlert(title:"ERROR",message:err.localizedDescription)
            }
        });
    }
    
    func showNativeGoogleLogin()
    {
        /*
        GIDSignIn.sharedInstance().signIn()<commit_msg>add swift demo equivalent code change<commit_after>//
//  ViewController.swift
//  SwiftDemo
//
//  Created by <NAME> on 18/05/16.
//  Copyright © 2016 LoginRadius Inc. All rights reserved.
//

import LoginRadiusSDK
import Eureka
/* Google Native SignIn
import GoogleSignIn
*/
class ViewController: FormViewController
/* Google Native SignIn
, GIDSignInUIDelegate
*/
{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Check if already login
        let defaults = UserDefaults.standard
        let user = defaults.integer(forKey: "isLoggedIn")
        if (user == 1) {
            self.performSegue(withIdentifier: "profile", sender: self);
        }
        
        /* Google Native SignIn
        GIDSignIn.sharedInstance().uiDelegate = self
        */
        
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.receiveRegistration), name: NSNotification.Name(rawValue: LoginRadiusRegistrationEvent), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.receiveLogin), name: NSNotification.Name(rawValue: LoginRadiusLoginEvent), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.receiveForgotPassword), name: NSNotification.Name(rawValue: LoginRadiusForgotPasswordEvent), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.receiveSocialLogin), name: NSNotification.Name(rawValue: LoginRadiusSocialLoginEvent), object: nil)
        
        self.navigationController?.navigationBar.topItem?.title = "Login Radius iOS pod 3.4.0"
        self.form = Form()
        
        //Create UI forms
        form +++ Section("")
            <<< ButtonRow("Login LR API")
            {
                $0.title = "Login"
                }.onCellSelection{ cell, row in
                    self.login()
            }
            <<< ButtonRow("Register LR API")
            {
                $0.title = "Register"
                }.onCellSelection{ cell, row in
                    self.registration()
            }
            <<< ButtonRow("Forgot LR API")
            {
                $0.title = "Forgot Password"
                }.onCellSelection{ row in
                    self.forgotPassword()
            }
            <<< ButtonRow("Social Login Only")
            {
                $0.title = $0.tag
                }.onCellSelection{ cell, row in
                    self.socialLoginOnly()
            }
            let socialNativeLoginEnabled = LoginRadiusSDK.sharedInstance().enableGoogleNativeInHosted ||  LoginRadiusSDK.sharedInstance().enableFacebookNativeInHosted
        
            if(socialNativeLoginEnabled)
            {
                let nativeSocialLoginSection = Section("Native Social Login")
                form +++ nativeSocialLoginSection
                
                //WARNING FOR DEV/Testers:
                //whenever the googleNative is set to true or facebookNative to true,
                //it is set to true the safariViewController will save it on a cookie
                //so if you immediately set the googleNative to false in the plist, and run the app
                //it won't reflect the changes on the cookie of the default cdn site. (meaning it still thinks you had googleNative set to true)
                //the workaround is to reset safari's cache, so on the Simulator go to Preferences > Safari > Clear History and Website Data
                
                if (LoginRadiusSDK.sharedInstance().enableGoogleNativeInHosted)
                {
                    NotificationCenter.default.addObserver(self, selector: #selector(self.showProfileController), name: Notification.Name("userAuthenticatedFromNativeGoogle"), object: nil)
                    NotificationCenter.default.addObserver(self, selector: #selector(self.showNativeGoogleLogin), name: Notification.Name("googleNative"), object: nil)

                    nativeSocialLoginSection <<< ButtonRow("Native Google")
                    {
                        $0.title = "Google"
                        }.onCellSelection{ cell, row in
                            self.showNativeGoogleLogin()
                    }
                }
                
                if (LoginRadiusSDK.sharedInstance().enableFacebookNativeInHosted)
                {
                    NotificationCenter.default.addObserver(self, selector: #selector(self.showNativeFacebookLogin), name: Notification.Name("facebookNative"), object: nil)

                    nativeSocialLoginSection <<< ButtonRow("Native Facebook")
                    {
                        $0.title = "Facebook"
                        }.onCellSelection{ cell, row in
                            self.showNativeFacebookLogin()
                    }
                }

                
            }

    }
    
    deinit
    {
        NotificationCenter.default.removeObserver(self)
    }
    
    func registration() {
        LoginRadiusManager.sharedInstance().registration(withAction: "registration", in: self)
    }
    
    func login() {
        LoginRadiusManager.sharedInstance().registration(withAction: "login", in: self)
    }
    
    func forgotPassword() {
        LoginRadiusManager.sharedInstance().registration(withAction: "forgotpassword", in: self);
    }
    
    func socialLoginOnly() {
        LoginRadiusManager.sharedInstance().registration(withAction: "social", in: self);
    }
    
    func receiveRegistration(notification:Notification)
    {
        if let userInfo  = notification.userInfo,
           let error  = userInfo["error"] as? Error
        {
            self.showAlert(title: "ERROR", message: error.localizedDescription)
        }else
        {
            let lrUser = UserDefaults.standard
            let profile = lrUser.integer(forKey: "isLoggedIn")
            if (profile == 1) {
                self.showProfileController()
            }else{
                self.showAlert(title: "SUCCESS", message: "Registration Successful, check your email for verification")
            }
        }
    }
    
    func receiveLogin(notification:Notification)
    {
        if let userInfo  = notification.userInfo,
           let error  = userInfo["error"] as? Error
        {
            self.showAlert(title: "ERROR", message: error.localizedDescription)
        }else
        {
            self.showProfileController()
        }
    }
    
    func receiveForgotPassword(notification:Notification)
    {
        if let userInfo  = notification.userInfo,
           let error  = userInfo["error"] as? Error
        {
            self.showAlert(title: "ERROR", message: error.localizedDescription)
        }else
        {
            self.showAlert(title: "SUCCESS", message: "Forgot password link send to your email id, please reset your password")
        }
    }
    
    func receiveSocialLogin(notification:Notification)
    {
        if let userInfo  = notification.userInfo,
           let error  = userInfo["error"] as? Error
        {
            self.showAlert(title: "ERROR", message: error.localizedDescription)
        }else
        {
            self.showProfileController()
        }
    }
    
    func showNativeGoogleLogin()
    {
        /*
        GIDSignIn.sharedInstance().signIn()
{'fraction_non_alphanumeric': 0.08993655009591264, 'fraction_numerical': 0.003172495204367714, 'mean_word_length': 1.8139921112725763, 'pattern_counts': {'":': 0, '<': 41, '<?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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4884793', 'n_tokens_mistral': 3417, 'n_tokens_neox': 3157, 'n_words': 786}