package cokr.xit.ens.modules.common.ctgy.nicedici.support.rest.utils;

import cokr.xit.ens.modules.common.ctgy.nicedici.support.rest.model.conf.DataBodySymkeyResp;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class SymmetricKey {


    private volatile static SymmetricKey instance;
    private static Map<String, DataBodySymkeyResp> data = new HashMap<>();
   
//    private static String version;
//    private static Long expireDt;

//    private SymkeyInfo() {
//
//    }
//
//    public static SymkeyInfo getInstance() {
//        if (instance == null)
//            synchronized (SymkeyInfo.class) {
//                if (instance == null)
//                    instance = new SymkeyInfo();
//            }
//
//        return instance;
//    }

    private SymmetricKey(String clientId, DataBodySymkeyResp data) {
        this.data.put(clientId, data);
    }

    public static SymmetricKey getInstance() {

        return instance;
    }

    public static SymmetricKey getInstance(String clientId, DataBodySymkeyResp data) {
        if (!isValidStat(clientId))
            synchronized (SymmetricKey.class) {
                if (!isValidStat(clientId))
                    instance = new SymmetricKey(clientId, data);
            }

        return instance;
    }


    public static boolean isValidStat(String clientId) {
        if (instance == null)
            return false;
        if (data == null)
            return false;
        if (data.getOrDefault(clientId, null) == null)
            return false;
       
        if (Long.parseLong(data.get(clientId).getSymkeyStatInfo().getCurValidDtim()) < Long.parseLong(LocalDateTime.now().plusDays(1).format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))))
            return false;

        return true;
    }


    public DataBodySymkeyResp getData(String clientId) {
        return data.get(clientId);
    }

}
