package com.epson.iprint.storage.box;

import android.content.Context;

import com.box.androidsdk.content.auth.BoxAuthentication;
import com.box.androidsdk.content.models.BoxEntity;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import com.epson.iprint.storage.StorageSecureStore;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import epson.server.utils.Define;

public class BoxAuthStorage extends BoxAuthentication.AuthStorage {
    private static final String SECURE_STORE_KEY = "FolderViewer.BOXNET_AUTO_INFO_V4";

    protected void storeAuthInfoMap(Map<String, BoxAuthentication.BoxAuthenticationInfo> map, Context context) {
        JsonObject jsonObject = new JsonObject();
        for (Map.Entry next : map.entrySet()) {
            jsonObject.add((String) next.getKey(), (JsonValue) ((BoxAuthentication.BoxAuthenticationInfo) next.getValue()).toJsonObject());
        }
        StorageSecureStore.getSharedSecureStore().put("FolderViewer.BOXNET_AUTO_INFO_V4", new BoxEntity(jsonObject).toJson(), StorageSecureStore.EXEC_MODE.OVERWRITE_IF_EXIST);
    }

    protected void clearAuthInfoMap(Context context) {
        StorageSecureStore.getSharedSecureStore().revoke("FolderViewer.BOXNET_AUTO_INFO_V4");
    }

    protected void storeLastAuthenticatedUserId(String str, Context context) {
        StorageSecureStore sharedSecureStore = StorageSecureStore.getSharedSecureStore();
        if (str == null || str.isEmpty()) {
            sharedSecureStore.revoke(Define.BOXNET_LAST_AUTHENTICATED_USER_ID);
        } else {
            sharedSecureStore.put(Define.BOXNET_LAST_AUTHENTICATED_USER_ID, str, StorageSecureStore.EXEC_MODE.OVERWRITE_IF_EXIST);
        }
    }

    protected String getLastAuthentictedUserId(Context context) {
        return StorageSecureStore.getSharedSecureStore().fetch(Define.BOXNET_LAST_AUTHENTICATED_USER_ID);
    }

    protected ConcurrentHashMap<String, BoxAuthentication.BoxAuthenticationInfo> loadAuthInfoMap(Context context) {
        ConcurrentHashMap<String, BoxAuthentication.BoxAuthenticationInfo> concurrentHashMap = new ConcurrentHashMap<>();
        String fetch = StorageSecureStore.getSharedSecureStore().fetch("FolderViewer.BOXNET_AUTO_INFO_V4");
        if (fetch != null && fetch.length() > 0) {
            BoxEntity boxEntity = new BoxEntity();
            boxEntity.createFromJson(fetch);
            for (String next : boxEntity.getPropertiesKeySet()) {
                JsonValue propertyValue = boxEntity.getPropertyValue(next);
                BoxAuthentication.BoxAuthenticationInfo boxAuthenticationInfo = null;
                if (propertyValue.isString()) {
                    boxAuthenticationInfo = new BoxAuthentication.BoxAuthenticationInfo();
                    boxAuthenticationInfo.createFromJson(propertyValue.asString());
                } else if (propertyValue.isObject()) {
                    boxAuthenticationInfo = new BoxAuthentication.BoxAuthenticationInfo();
                    boxAuthenticationInfo.createFromJson(propertyValue.asObject());
                }
                concurrentHashMap.put(next, boxAuthenticationInfo);
            }
        }
        return concurrentHashMap;
    }

    public static void deleteOldData() {
        StorageSecureStore sharedSecureStore = StorageSecureStore.getSharedSecureStore();
        sharedSecureStore.revoke(Define.BOXNET_TOKEN);
        sharedSecureStore.revoke(Define.BOXNET_USERNAME);
        sharedSecureStore.revoke(Define.BOXNET_PASSOWRD);
    }
}
