import com.crowdlang.exceptions.CrowdLangException;
import com.crowdlang.messages.Answer;
import com.crowdlang.messages.AnswerSet;
import com.crowdlang.messages.CrowdMarketEnum;

import com.crowdlang.messages.MturkGeneralComponents;
import com.crowdlang.messages.MturkQuestionSetType;
import com.crowdlang.messages.QuestionSet;
import com.crowdlang.messages.SelectionIdentifier;
import com.crowdlang.messages.SelectionQuestion;
import com.crowdlang.messages.SelectionStyleSuggestion;
import com.mturk.MTurkConnection;
import com.mturk.MTurkReceiver;
import com.mturk.MTurkSender;


public class Images {
	
	private static String questionID1 = "q1";

	
	public static void main(String[] args) throws CrowdLangException, InterruptedException{
		
		System.out.println("Start");
		
		//Create a Connection to the Amazon's Mechanical Turk Market Place
		//true => sends to the sandbox environment for testing
		//false => sends to the real market place
		boolean sandbox = true;
				
		//TODO: insert your AWS key here
		String accessKeyID = "";
		String secretAccessKey = "";
		

		MTurkConnection connection = MTurkConnection.prepareMTurkConnection(accessKeyID, secretAccessKey, sandbox);
		System.out.println("Connection available");
		
		//The creation of a QuestionSetType (This is the container which
		//can be reused for submitting HIT's to the same place
		//for Step 1
		//TODO: define reward, assignment duration etc.
		double reward = 0.02;
		long assignmentDurationInSeconds = 60 * 5;
		String containerName = "Select the older person";
		String desc = "Description of the task";
		String keywords = "";
		long autoApprovalInSeconds = 60 *60* 5; // 5 h
		MturkQuestionSetType type = new MturkQuestionSetType(containerName, desc, keywords, reward, assignmentDurationInSeconds, autoApprovalInSeconds, null);
		connection.registerHITType(type);
		System.out.println("Link to the group: https://workersandbox.mturk.com/mturk/preview?groupId=" + type.getQuestionTypeID());
		
		
		MTurkSender sender = connection.generateSender();
		MTurkReceiver receiver = connection.generateReceiver();

		

		/**
		 * Compare two pictures
		 */
		int numAssignments = 3;	
		String pic1 = "1";
		String pic2 = "2";
		
		QuestionSet qs = generateQuestion(type, numAssignments, pic1, pic2);
		qs.setHitID(sender.sendQuestion(qs).getHITId());
		System.out.println("Hit-ID: " + qs.getHitID());
		
		boolean done = false;
		AnswerSet answerSet1 = null;
		
		while(!done){
			Thread.sleep(10000l);
			done = receiver.isCompleted(qs);
			System.out.println("State: " + qs.getHitID() + ";" + done);
			System.out.println("Fraction solved: "+receiver.getPercentageSolved(qs));

			//If all assignments of a task are solved
			if(done){
				answerSet1 = receiver.checkForAnswers(qs);				
			}
		}
		//answers 1 older
		int c1 = 0;
		//answers 2 older
		int c2 = 0;
		//count how many people voted for which option
		for (Answer answer : answerSet1.getSolutionsForQuestion(questionID1)) {
			receiver.approveAssignment(answer.getAssignmentID(), "Thank you very much");
			
			if (answer.getAnswerValue().equals("1")) c1++;
			else if (answer.getAnswerValue().equals("2")) c2++; 
		}
		
		//do simple majority voting
		if (c1>c2) 
			System.out.println("The Person in image 1 is older.");

		//else break
		else 
			System.out.println("The Person in image 2 is older.");
	
		
	}
	
	/*The URLs of the pictures are
	 * http://www.ifi.uzh.ch/ce/teaching/spring2012/picture1.jpg George Bush
	 * http://www.ifi.uzh.ch/ce/teaching/spring2012/picture2.jpg Mother Theresa
	 * http://www.ifi.uzh.ch/ce/teaching/spring2012/picture3.jpg Diego Maradona
	 * http://www.ifi.uzh.ch/ce/teaching/spring2012/picture4.jpg Madonna
	 * http://www.ifi.uzh.ch/ce/teaching/spring2012/picture5.jpg Lady Diana
	 * http://www.ifi.uzh.ch/ce/teaching/spring2012/picture6.jpg Nicole Kidman
	 * http://www.ifi.uzh.ch/ce/teaching/spring2012/picture7.jpg Mahatma Ghandi
	 * http://www.ifi.uzh.ch/ce/teaching/spring2012/picture8.jpg Albert Einstein
	 * */
	
	private static QuestionSet generateQuestion(MturkQuestionSetType type, int numberOfAssignments, String pic1, String pic2) throws CrowdLangException{
		String p1 = "http://www.ifi.uzh.ch/ce/teaching/spring2012/picture"+pic1+".jpg";
		String p2 = "http://www.ifi.uzh.ch/ce/teaching/spring2012/picture"+pic2+".jpg";
		SelectionQuestion q = new SelectionQuestion("Select the older person", MturkGeneralComponents.createText("Select the person that is older according to their date of birth."), questionID1, true, SelectionStyleSuggestion.radiobutton);
		SelectionIdentifier si1 = new SelectionIdentifier(MturkGeneralComponents.createImageReference("jpg", p1, "1"), "1");
		SelectionIdentifier si2 = new SelectionIdentifier(MturkGeneralComponents.createImageReference("jpg", p2, "2"), "2");
		q.addSuggestion(si1, 0);
		q.addSuggestion(si2, 1);
		
		//adding more pictures to choose from is easy
		//String p3 = "http://www.ifi.uzh.ch/ce/teaching/spring2012/picture3.jpg";
		//SelectionIdentifier si3 = new SelectionIdentifier(MturkGeneralComponents.createImageReference("jpg", p3, "3"), "3");
		//q.addSuggestion(si3, 2);
			
		QuestionSet questionSet = new QuestionSet("processID", "taskID", CrowdMarketEnum.MTURK, type, numberOfAssignments, 26100, "Select the older person", "Select the person that is older according to their date of birth.");
		questionSet.addQuestion(q);
		
		return questionSet;
	}
}
