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

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

    static {
        String uin = autoGeneratedIDValueCache.get("AddIdentity_Valid_Parameters_smoke_MOCK_Pos_UIN");
        String phone = autoGeneratedIDValueCache.get("AddIdentity_Valid_Parameters_smoke_MOCK_Pos_PHONE");
        String email = autoGeneratedIDValueCache.get("AddIdentity_Valid_Parameters_smoke_MOCK_Pos_EMAIL");

        for (int i = 0; i < 5; i++) {
            availableUINs.add(new Uin(uin, phone, email));
        }
    }


    //Sample data, data can be passed in this way as well
//    static {
//        availableUINs.add(new Uin("9261481024", "6167173343", "InjiWallet_AddIdentity_withValidParameters_smoke_Pos@mosip.net"));
//        availableUINs.add(new Uin("9261481024", "6167173343", "InjiWallet_AddIdentity_withValidParameters_smoke_Pos@mosip.net"));
//        availableUINs.add(new Uin("9261481024", "6167173343", "InjiWallet_AddIdentity_withValidParameters_smoke_Pos@mosip.net"));
//        availableUINs.add(new Uin("9261481024", "6167173343", "InjiWallet_AddIdentity_withValidParameters_smoke_Pos@mosip.net"));
//        availableUINs.add(new Uin("9261481024", "6167173343", "InjiWallet_AddIdentity_withValidParameters_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);
    }
}
