//
//  GMFGRequestHandler.swift
//  MVM5G
//
//  Created by Gujuluva Santharam, Ajai Prabhu on 07/07/20.
//  Copyright © 2020 Kyle. All rights reserved.
//

import UIKit

class GMFGStorageManager {

    //MARK - user default base methods
    class func store(dictionary: [AnyHashable : Any],for key:String) {
       // MFUtility.save(dictionary, toUserDefaultsForKey: key)
    }
    
    class func retrieve(key: String) -> [String: Any]? {
        return UserDefaults.standard.dictionary(forKey: key)
    }
    
    //Use this to append entries from dictionary to existing one
    class func addEntries(dictionary: [String : Any], key: String = GMFGConstant.fiveGParams) {
        let storedDict = GMFGStorageManager.retrieve(key: key) ?? [:]
        let appendedDict = storedDict.merging(dictionary) { $1 }
        GMFGStorageManager.store(dictionary: appendedDict, for: key)
    }

    class func extraRequestParams() -> [String: Any] {
        var storedDict = GMFGStorageManager.retrieve(key: GMFGConstant.fiveGParams) ?? [:]
        if let currentLocation = GMFGLocationManager.shared.currentLocation {
            storedDict[GMFGConstant.AR.deviceLocation] = currentLocation.array
        }
        return GMFGTestScreenData.shared.isHardcodedValues ? GMFGTestScreenData.shared.hardcodeDict : storedDict
    }
    
    //MARK: - methods to remove from user defaults
    class func remove(key: String) {
        if var storedDict = GMFGStorageManager.retrieve(key: GMFGConstant.fiveGParams) {
            storedDict.removeValue(forKey: key)
            GMFGStorageManager.store(dictionary: storedDict, for: GMFGConstant.fiveGParams)
        }
    }
    
    class func removeAll() {
        UserDefaults.standard.removeObject(forKey: GMFGConstant.fiveGParams)
        UserDefaults.standard.removeObject(forKey: "selfInstallNextStep")
    }
}

