package com.kinancity.core.captcha;

import java.util.ArrayDeque;
import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.Getter;

public class CaptchaQueue {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Getter
	public ArrayDeque<CaptchaRequest> queue = new ArrayDeque<>();

	private OverFlowCaptchaCollector overFlowCollector;

	public CaptchaQueue() {
		// Empty constructor
	}

	public CaptchaQueue(OverFlowCaptchaCollector overFlowCollector) {
		this.overFlowCollector = overFlowCollector;
	}

	public boolean isEmpty() {
		return queue.isEmpty();
	}

	public int size() {
		return queue.size();
	}

	/**
	 * Add request to queue
	 * 
	 * @param request
	 * @return
	 * @throws InterruptedException
	 */
	public CaptchaRequest addRequest(CaptchaRequest request) {
		queue.add(request);
		return request;
	}

	/**
	 * Add Multiple Captchas
	 * 
	 * @param responses
	 */
	public void addCaptchas(Collection<String> responses) {
		responses.forEach(this::addCaptcha);
	}

	/**
	 * Add a single captcha
	 * 
	 * @param response
	 */
	public void addCaptcha(String response) {
		CaptchaRequest firstInQueue = queue.poll();

		if (firstInQueue == null) {
			logger.debug("No first in queue, go to overflow");
			overFlowCollector.manageCaptchaOverflow(response);
		} else {
			logger.debug("First in queue is {}", firstInQueue);
			firstInQueue.setResponse(response);
			logger.debug("Queue has now {} elements", queue.size());
		}
	}
}

@@ -828,16 +828,122 @@
 uest) %7B%0A
+%09%09if(request == null)%7B%0A%09%09%09throw new IllegalArgumentException(%22Should not add null request in queue%22);%0A%09%09%7D%0A
 %09%09queue.

add failsafe for captcha provider