package inji.utils.testdatamanager;

import inji.models.Vid;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import static io.mosip.testrig.apirig.utils.AdminTestUtil.autoGeneratedIDValueCache;

public class VIDManager {

    private static final BlockingQueue<Vid> availableVIDs = new LinkedBlockingQueue<>();

    private static final String VID_KEY = "Generate_Perpetual_VID_Valid_Smoke_sid_VID";
    private static final String PHONE_KEY = "AddIdentity_withValidParameters_smoke_UIN1_Pos_PHONE";
    private static final String EMAIL_KEY = "AddIdentity_withValidParameters_smoke_UIN1_Pos_EMAIL";

    static {
        availableVIDs.add(new Vid(get(VID_KEY), get(PHONE_KEY), get(EMAIL_KEY)));
    }

    private static String get(String key) {
        return autoGeneratedIDValueCache.get(key);
    }

    //Sample data, data can be passed in this way as well
//    static {
//        availableVIDs.add(new Vid("3084260253624850", "4014236339", "Mimoto_AddIdentity_Binding_smoke_Pos@mosip.net"));
//    }

    public static Vid acquireVID() throws InterruptedException {
        return availableVIDs.take();
    }

    public static void releaseVID(Vid vid) {
        availableVIDs.offer(vid);
    }

    public static Vid acquireVIDWithTimeout(long timeoutSeconds) throws InterruptedException {
        return availableVIDs.poll(timeoutSeconds, TimeUnit.SECONDS);
    }
}
