<commit_before>//
//  Customerly.swift
//  Customerly
//
//  Created by <NAME> on 10/10/16.
//
//

import Kingfisher

open class Customerly: NSObject {
    
    open static let sharedInstance = Customerly()
    var customerlySecretKey : String = ""
    
    /*
     * Enable verbose logging, that is useful for debugging. By default is enabled.
     */
    open var verboseLogging : Bool = true
    
    //MARK: Init
    override init() {
        super.init()
    }
    
    //MARK: - Configuration
    open func configure(secretKey: String, widgetColor: UIColor? = nil){
        customerlySecretKey = secretKey
        user_color_template = widgetColor
        
        //Image cache expiration after one day
        ImageCache.default.maxCachePeriodInSecond = 86400
        
        //If user is not stored, ping ghost, else ping registered. Connect to socket
        ping(success: { () in
            CySocket.sharedInstance.configure()
            CySocket.sharedInstance.openConnection()
        })
    }
    
    //MARK: - Register user and update with attributes (alias ping)
    
    /*
     * If you want to register a user_id, you have to insert also an email.
     */
    open func registerUser(email: String, user_id: String? = nil, name: String? = nil, attributes:Dictionary<String, Any?>? = nil){
        ping(email: email, user_id: user_id, name: name, attributes:attributes, success:{ () in
            CySocket.sharedInstance.reconfigure()
        })
    }
    
    /*
     * If you want to logout a user, call logoutUser() to delete all local data and de-authenticate the user
     */
    open func logoutUser(){
        CyStorage.deleteCyDataModel()
        CySocket.sharedInstance.closeConnection()
        ping()
    }
    
    /*
     * Send an update to Customerly, with optionals attributes.
     * Attributes need to be only on first level.
     * Ex: ["Params1": 1, "Params2: "Hello"].
     */
    open func update(attributes:Dictionary<String, Any?>? = nil){
        if CyStorage.getCyDataModel()?.user?.is_user == 1{
            ping(attributes: attributes)
        }
    }
    
    
    //Private method
    func ping(email: String? = nil, user_id: String? = nil, name: String? = nil, attributes:Dictionary<String, Any?>? = nil, success: (() -> Void)? = nil, failure: (() -> Void)? = nil){
        let pingRequestModel = CyRequestPingModel(JSON: [:])
        pingRequestModel?.params = [:]
        
        //if some cookies are stored, CyRequestPingModel containes these cookies and user informations
        if let dataStored = CyStorage.getCyDataModel(){
            pingRequestModel?.settings?.user_id = dataStored.user?.user_id
            pingRequestModel?.settings?.email = dataStored.user?.email
            pingRequestModel?.settings?.name = dataStored.user?.name
            pingRequestModel?.settings?.attributes = attributes
            pingRequestModel?.cookies?.customerly_lead_token = dataStored.cookies?.customerly_lead_token
            pingRequestModel?.cookies?.customerly_temp_token = dataStored.cookies?.customerly_temp_token
            pingRequestModel?.cookies?.customerly_user_token = dataStored.cookies?.customerly_user_token
        }
        
        //if user_id != nil, email != nil, name != nil, the api call send this data, otherwise the data stored
        if email != nil{
            pingRequestModel?.settings?.email = email<commit_msg>log “Only registered users may have attributes.”<commit_after>//
//  Customerly.swift
//  Customerly
//
//  Created by <NAME> on 10/10/16.
//
//

import Kingfisher

open class Customerly: NSObject {
    
    open static let sharedInstance = Customerly()
    var customerlySecretKey : String = ""
    
    /*
     * Enable verbose logging, that is useful for debugging. By default is enabled.
     */
    open var verboseLogging : Bool = true
    
    //MARK: Init
    override init() {
        super.init()
    }
    
    //MARK: - Configuration
    open func configure(secretKey: String, widgetColor: UIColor? = nil){
        customerlySecretKey = secretKey
        user_color_template = widgetColor
        
        //Image cache expiration after one day
        ImageCache.default.maxCachePeriodInSecond = 86400
        
        //If user is not stored, ping ghost, else ping registered. Connect to socket
        ping(success: { () in
            CySocket.sharedInstance.configure()
            CySocket.sharedInstance.openConnection()
        })
    }
    
    //MARK: - Register user and update with attributes (alias ping)
    
    /*
     * If you want to register a user_id, you have to insert also an email.
     */
    open func registerUser(email: String, user_id: String? = nil, name: String? = nil, attributes:Dictionary<String, Any?>? = nil){
        ping(email: email, user_id: user_id, name: name, attributes:attributes, success:{ () in
            CySocket.sharedInstance.reconfigure()
        })
    }
    
    /*
     * If you want to logout a user, call logoutUser() to delete all local data and de-authenticate the user
     */
    open func logoutUser(){
        CyStorage.deleteCyDataModel()
        CySocket.sharedInstance.closeConnection()
        ping()
    }
    
    /*
     * Send an update to Customerly, with optionals attributes.
     * Attributes need to be only on first level.
     * Ex: ["Params1": 1, "Params2: "Hello"].
     */
    open func update(attributes:Dictionary<String, Any?>? = nil){
        if CyStorage.getCyDataModel()?.user?.is_user == 1{
            ping(attributes: attributes)
        }
        else{
            cyPrint("Only registered users may have attributes.")
        }
    }
    
    
    //Private method
    func ping(email: String? = nil, user_id: String? = nil, name: String? = nil, attributes:Dictionary<String, Any?>? = nil, success: (() -> Void)? = nil, failure: (() -> Void)? = nil){
        let pingRequestModel = CyRequestPingModel(JSON: [:])
        pingRequestModel?.params = [:]
        
        //if some cookies are stored, CyRequestPingModel containes these cookies and user informations
        if let dataStored = CyStorage.getCyDataModel(){
            pingRequestModel?.settings?.user_id = dataStored.user?.user_id
            pingRequestModel?.settings?.email = dataStored.user?.email
            pingRequestModel?.settings?.name = dataStored.user?.name
            pingRequestModel?.settings?.attributes = attributes
            pingRequestModel?.cookies?.customerly_lead_token = dataStored.cookies?.customerly_lead_token
            pingRequestModel?.cookies?.customerly_temp_token = dataStored.cookies?.customerly_temp_token
            pingRequestModel?.cookies?.customerly_user_token = dataStored.cookies?.customerly_user_token
        }
        
        //if user_id != nil, email != nil, name != nil, the api call send this data, otherwise the data stored
        if email != nil{
            pingRequestModel?.settings?.email = email
{'fraction_non_alphanumeric': 0.10336364948751263, 'fraction_numerical': 0.0043308791684712, 'mean_word_length': 2.4382133995037223, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 15, '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': '915678', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1881, 'n_words': 532}