package net.aicomp.game_engine_tester;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonVerifier {

	private String lastKey;
	private Object lastActualValue;
	private Object lastExpectedValue;

	public JsonVerifier() {
	}

	public boolean verify(File file, int aiCount) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			Map<String, Object> gameResult = mapper.readValue(file, Map.class);
			return verify(gameResult);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	private boolean verify(Map<String, Object> gameResult) {
		if (!checkType(gameResult, "log", ArrayList.class)) {
			return false;
		}
		if (!checkValue(gameResult, "winner", -1)) {
			return false;
		}
		if (!checkType(gameResult, "replay", String.class)) {
			return false;
		}
		Map<String, Object> log = ((ArrayList<Map<String, Object>>)gameResult.get("log")).get(0);
		if (!checkType(log, "target", Integer.class)) {
			return false;
		}
		if (!checkType(log, "message", String.class)) {
			return false;
		}
		return true;
	}

	public boolean checkType(Map<String, Object> map, String key, Class<?> klass) {
		lastKey = key;
		lastExpectedValue = klass;
		Object object = map.get(key);
		if (object == null) {
			return false;
		}
		lastActualValue = object.getClass();
		return klass.equals(lastActualValue);
	}

	public boolean checkValue(Map<String, Object> map, String key, Object value) {
		lastKey = key;
		lastExpectedValue = value;
		lastActualValue = map.get(key);
		return value.equals(lastActualValue);
	}

	public String getLastKey() {
		return lastKey;
	}

	public Object getLastActualValue() {
		return lastActualValue;
	}

	public Object getLastExpectedValue() {
		return lastExpectedValue;
	}
}

@@ -553,16 +553,50 @@
 %09%09try %7B%0A
+%09%09%09@SuppressWarnings(%22unchecked%22)%0A
 %09%09%09Map%3CS

Remove a warning.