<commit_before>//
//  GISTAuth.swift
//  GISTFramework
//
//  Created by <NAME> on 07/04/2017.
//  Copyright © 2017 Social Cubix. All rights reserved.
//

import UIKit
import ObjectMapper

private let SIGN_UP_REQUEST = "entity_auth";
private let SIGN_IN_REQUEST = "entity_auth/signin";
private let SOCIAL_SIGN_IN_REQUEST = "entity_auth/social_login";

private let EDIT_PROFILE_REQUEST = "entity_auth/edit_profile";

private let RESEND_CODE_REQUEST = "entity_auth/resend_code";
private let VERIFY_PHONE_REQUEST = "entity_auth/verify_phone";

private let FORGOT_PASSWORD_REQUEST = "entity_auth/forgot_request";
private let CHANGE_PASSWORD_REQUEST = "entity_auth/change_password";

private let RESET_PASSWORD_REQUEST = "entity_auth/reset_password";

private let DELETE_ACCOUNT = "entity_auth/delete_account";

private let CHANGE_LOGIN_ID = "entity_auth/change_id_request";


public class GISTAuth<T:GISTUser>: NSObject {
    
    public typealias GISTAuthCompletion = (T?, Any?) -> Void
    public typealias GISTAuthFailure = (_ error:NSError) -> Void
    
    //PRIVATE init so that singleton class should not be reinitialized from anyother class
    fileprivate override init() {
        super.init();
    } //C.E.
    
    //MARK: - Sign Up
    public static func signUp(fields:[ValidatedTextField], additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        self.request(service: SIGN_UP_REQUEST, fields: fields, additional:params, completion:completion, failure:failure);
    } //F.E.
    
    public static func signUp(arrData:NSMutableArray, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {

        self.request(service: SIGN_UP_REQUEST, arrData: arrData, additional:params, ignore:nil, completion:completion, failure:failure);
    } //F.E.
    
    public static func signUp(params:[String:Any], completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        self.request(service: SIGN_UP_REQUEST, params: params, completion:completion, failure:failure);
    } //F.E.
    
    public static func signUp(user:T, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        self.request(service: SIGN_UP_REQUEST, params: GISTSocialUtils.formate(user: user, additional: params), completion:completion, failure:failure);
    } //F.E.
    
    //MARK: - Sign In
    public static func signIn(fields:[ValidatedTextField], additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        self.request(service: SIGN_IN_REQUEST, fields: fields, additional:params, completion:completion, failure:failure);
    } //F.E.
    
    public static func signIn(arrData:NSMutableArray, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        
        self.request(service: SIGN_IN_REQUEST, arrData: arrData, additional:params, ignore:nil, completion:completion, failure:failure);
    } //F.E.
    
    public static func signIn(params:[String:Any], completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        self.request(service: SIGN_IN_REQUEST, params: params, completion:completion, failure:failure);
    } //F.E.
    
    public static func signIn(user:T, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        
        let service:String
        
        if let platformType:String = user.platformType, platformType != "custom" {
            service = SOCIAL_SIGN_IN_REQUEST;
        } else {
            service = SIGN_IN_REQUEST;
        }
        
        self.request(service: service, params: GISTSocialUtils.formate(user: user, additional: params), completion:completion, failure:failure);
    } //F.E.
    
    //MARK: - Edit Profile
    
    public static func editProfile(arrData:NSMutableArray, additional params:[String:Any]?, ignore iParams:[String]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        
        guard let entityId:Int = GIST_GLOBAL.userData?["entity_id"] as? Int else {
            return;
        }
        
        var aParams:[String:Any] = params ?? [:];
        aParams["entity_id"] = entityId;
        
        self.request(service: EDIT_PROFILE_REQUEST, arrData: arrData, additional:aParams, ignore:iParams, completion:completion, failure:failure);
    } //F.E.
    
    public static func editProfile(params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        
        guard let entityId:Int = GIST_GLOBAL.userData?["entity_id"] as? Int else {
            return;
        }
        
        var aParams:[String:Any] = params ?? [:];
        aParams["entity_id"] = entityId;
        
        self.request(service: EDIT_PROFILE_REQUEST, params: aParams, completion:completion, failure:failure);
    } //F.E.
    
    public static func editProfile(updated user:T, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        self.request(service: EDIT_PROFILE_REQUEST, params: GISTSocialUtils.formate(user: user, additional: params), completion:completion, failure:failure);
    } //F.E.
    
    public static func changeLoginId(new loginId:String, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        
        guard let entityId:Int = GIST_GLOBAL.userData?["entity_id"] as? Int else {
            return;
        }
        
        var aParams:[String:Any] = params ?? [:];
        aParams["entity_id"] = entityId;
        aParams["new_login_id"] = loginId;
        
        self.request(service: CHANGE_LOGIN_ID, params: aParams, completion:completion, failure:failure);
    } //F.E.
    
    //MARK: - Verify Phone
    public static func verifyPhone(code:String, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {

        guard let usrData:[String:Any] = GIST_GLOBAL.userData, let mobileNo:String = (usrData["new_mobile_no"] as? String ?? usrData["mobile_no"] as? String), let verificationToken:String = usrData["verification_token"] as? String else {
            return;
        }
        
        let isVerified:Bool = (usrData["is_verified"] as? Bool) ?? false;
        let entityId:Int? = usrData["entity_id"] as? Int;
        
        let verificationMode:String = (entityId == nil) ? "forgot" : (isVerified ? "change_mobile_no" : "signup");
        
        var aParams:[String:Any] = params ?? [:];
        
        aParams["mobile_no"] = mobileNo;<commit_msg>Removing an overloaded method<commit_after>//
//  GISTAuth.swift
//  GISTFramework
//
//  Created by <NAME> on 07/04/2017.
//  Copyright © 2017 Social Cubix. All rights reserved.
//

import UIKit
import ObjectMapper

private let SIGN_UP_REQUEST = "entity_auth";
private let SIGN_IN_REQUEST = "entity_auth/signin";
private let SOCIAL_SIGN_IN_REQUEST = "entity_auth/social_login";

private let EDIT_PROFILE_REQUEST = "entity_auth/edit_profile";

private let RESEND_CODE_REQUEST = "entity_auth/resend_code";
private let VERIFY_PHONE_REQUEST = "entity_auth/verify_phone";

private let FORGOT_PASSWORD_REQUEST = "entity_auth/forgot_request";
private let CHANGE_PASSWORD_REQUEST = "entity_auth/change_password";

private let RESET_PASSWORD_REQUEST = "entity_auth/reset_password";

private let DELETE_ACCOUNT = "entity_auth/delete_account";

private let CHANGE_LOGIN_ID = "entity_auth/change_id_request";


public class GISTAuth<T:GISTUser>: NSObject {
    
    public typealias GISTAuthCompletion = (T?, Any?) -> Void
    public typealias GISTAuthFailure = (_ error:NSError) -> Void
    
    //PRIVATE init so that singleton class should not be reinitialized from anyother class
    fileprivate override init() {
        super.init();
    } //C.E.
    
    //MARK: - Sign Up
    public static func signUp(fields:[ValidatedTextField], additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        self.request(service: SIGN_UP_REQUEST, fields: fields, additional:params, completion:completion, failure:failure);
    } //F.E.
    
    public static func signUp(arrData:NSMutableArray, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {

        self.request(service: SIGN_UP_REQUEST, arrData: arrData, additional:params, ignore:nil, completion:completion, failure:failure);
    } //F.E.
    
    public static func signUp(params:[String:Any], completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        self.request(service: SIGN_UP_REQUEST, params: params, completion:completion, failure:failure);
    } //F.E.
    
    public static func signUp(user:T, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        self.request(service: SIGN_UP_REQUEST, params: GISTSocialUtils.formate(user: user, additional: params), completion:completion, failure:failure);
    } //F.E.
    
    //MARK: - Sign In
    public static func signIn(fields:[ValidatedTextField], additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        self.request(service: SIGN_IN_REQUEST, fields: fields, additional:params, completion:completion, failure:failure);
    } //F.E.
    
    public static func signIn(arrData:NSMutableArray, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        
        self.request(service: SIGN_IN_REQUEST, arrData: arrData, additional:params, ignore:nil, completion:completion, failure:failure);
    } //F.E.
    
    public static func signIn(params:[String:Any], completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        self.request(service: SIGN_IN_REQUEST, params: params, completion:completion, failure:failure);
    } //F.E.
    
    public static func signIn(user:T, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        
        let service:String
        
        if let platformType:String = user.platformType, platformType != "custom" {
            service = SOCIAL_SIGN_IN_REQUEST;
        } else {
            service = SIGN_IN_REQUEST;
        }
        
        self.request(service: service, params: GISTSocialUtils.formate(user: user, additional: params), completion:completion, failure:failure);
    } //F.E.
    
    //MARK: - Edit Profile
    
    public static func editProfile(arrData:NSMutableArray, additional params:[String:Any]?, ignore iParams:[String]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        
        guard let entityId:Int = GIST_GLOBAL.userData?["entity_id"] as? Int else {
            return;
        }
        
        var aParams:[String:Any] = params ?? [:];
        aParams["entity_id"] = entityId;
        
        self.request(service: EDIT_PROFILE_REQUEST, arrData: arrData, additional:aParams, ignore:iParams, completion:completion, failure:failure);
    } //F.E.
    
    public static func editProfile(params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        
        guard let entityId:Int = GIST_GLOBAL.userData?["entity_id"] as? Int else {
            return;
        }
        
        var aParams:[String:Any] = params ?? [:];
        aParams["entity_id"] = entityId;
        
        self.request(service: EDIT_PROFILE_REQUEST, params: aParams, completion:completion, failure:failure);
    } //F.E.
    
    /*
     Should not be called
    public static func editProfile(updated user:T, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        self.request(service: EDIT_PROFILE_REQUEST, params: GISTSocialUtils.formate(user: user, additional: params), completion:completion, failure:failure);
    } //F.E.
    */
    
    public static func changeLoginId(new loginId:String, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {
        
        guard let entityId:Int = GIST_GLOBAL.userData?["entity_id"] as? Int else {
            return;
        }
        
        var aParams:[String:Any] = params ?? [:];
        aParams["entity_id"] = entityId;
        aParams["new_login_id"] = loginId;
        
        self.request(service: CHANGE_LOGIN_ID, params: aParams, completion:completion, failure:failure);
    } //F.E.
    
    //MARK: - Verify Phone
    public static func verifyPhone(code:String, additional params:[String:Any]?, completion:@escaping GISTAuthCompletion, failure:GISTAuthFailure?) {

        guard let usrData:[String:Any] = GIST_GLOBAL.userData, let mobileNo:String = (usrData["new_mobile_no"] as? String ?? usrData["mobile_no"] as? String), let verificationToken:String = usrData["verification_token"] as? String else {
            return;
        }
        
        let isVerified:Bool = (usrData["is_verified"] as? Bool) ?? false;
        let entityId:Int? = usrData["entity_id"] as? Int;
        
        let verificationMode:String = (entityId == nil) ? "forgot" : (isVerified ? "change_mobile_no" : "signup");
        
        var aParams:[String:Any] = params ?? [:];
        
        aParams["mobile_no"] = mobileNo;
{'fraction_non_alphanumeric': 0.11464872944693573, 'fraction_numerical': 0.0017937219730941704, 'mean_word_length': 3.7500887468938586, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '676926', 'n_tokens_mistral': 4036, 'n_tokens_neox': 3902, 'n_words': 850}