package inji.utils.testdatamanager;

import inji.models.Uin;

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 UINManager {

    private static final BlockingQueue<Uin> availableUINs = new LinkedBlockingQueue<>();

    static {
        int totalUINs = 5;
        for (int i = 1; i <= totalUINs; i++) {
            String prefix = "AddIdentity_withValidParameters_smoke_UIN" + i + "_Pos_";
            availableUINs.add(new Uin(
                    autoGeneratedIDValueCache.get(prefix + "UIN"),
                    autoGeneratedIDValueCache.get(prefix + "PHONE"),
                    autoGeneratedIDValueCache.get(prefix + "EMAIL")
            ));
        }
    }

    //Sample data, data can be passed in this way as well
//    static {
//        availableUINs.add(new Uin("8495035196", "6167173343", "InjiWallet_AddIdentity_withValidParameters_smoke_Pos@mosip.net"));
//        availableUINs.add(new Uin("2159301603", "2472408794", "InjiWallet_AddIdentity_withValidParameters_smoke_Pos@mosip.net"));
//        availableUINs.add(new Uin("4389467429", "+5598062051", "InjiWallet_AddIdentity_withValidParameters_smoke_Pos@mosip.net"));
//        availableUINs.add(new Uin("2314215780", "4636492043", "Mimoto_AddIdentity_withValidParameters_smoke_Pos@mosip.net"));
//        availableUINs.add(new Uin("7605921836", "++++0416925393", "Mimoto_AddIdentity_Binding_smoke_Pos@mosip.net"));
//    }

    public static Uin acquireUIN() throws InterruptedException {
        return availableUINs.take();
    }

    public static void releaseUIN(Uin uin) {
        availableUINs.offer(uin);
    }

    public static Uin acquireUINWithTimeout(long timeoutSeconds) throws InterruptedException {
        return availableUINs.poll(timeoutSeconds, TimeUnit.SECONDS);
    }
}
