package com.github.realizm.tmup4j;

import java.io.IOException;
import java.util.HashMap;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

class Room extends Tmup4J {
	
	Room(Request request) {
		super(request);
	}

	@Override
	public JsonObject getRoomList(int team_number) throws IOException {
		
		return request.request(RequestMethod.GET,
				ContentType.application_xwwwformurlencoded,
				EDGE_DOMAIN + "/v3/rooms" + (team_number > -1 ? ("/" + team_number) : ""),
				null);
	}
	
	@Override
	public long createRoom(int team_number, int[] user_numbers) throws IOException {
		
		JsonObject param = new JsonObject();
		JsonArray users = new JsonArray();
		for(int userNumber:user_numbers) {
			users.add(userNumber);
		}
		
		param.add("users", users);
		
		return request.request(RequestMethod.POST,
				ContentType.appliaction_json,
				EDGE_DOMAIN + "/v3/room/" + team_number,
				param.toString()).get("room").getAsLong();
	}
	
	
	private int MY_USER_NUMBER = -1;
	
	private HashMap<Integer, HashMap<Integer, Long>> STORED_ROOM_NUMBER 
			= new HashMap<Integer, HashMap<Integer, Long>>();
	
	@Override
	public long getRoomNumber(int team_number, int user_number) throws IOException {
		
		if(MY_USER_NUMBER == -1){
			MY_USER_NUMBER = new My(request).getMyInfo().get("index").getAsInt();
		}
		
		if(MY_USER_NUMBER == user_number){
			throw new IOException("Do NOT create message room with myself.");
		}
		
		HashMap<Integer, Long> storedRoomNumberMap;
		if(STORED_ROOM_NUMBER.containsKey(team_number)) {
			storedRoomNumberMap = STORED_ROOM_NUMBER.get(team_number);
			if(storedRoomNumberMap.containsKey(user_number)) {
				return storedRoomNumberMap.get(user_number);
			}
		} else {
			storedRoomNumberMap = new HashMap<Integer, Long>();
			STORED_ROOM_NUMBER.put(team_number, storedRoomNumberMap);
		}
		
		JsonObject roomList = getRoomList(team_number); 
		JsonArray roomListArray = roomList.get("rooms").getAsJsonArray();
		
		long roomNumber = -1;
		for(JsonElement je:roomListArray) {
			JsonObject roomInfo = je.getAsJsonObject();
			
			if (roomInfo.get("user").getAsInt() == user_number && roomInfo.get("users").getAsJsonArray().size() == 1) {
				roomNumber = roomInfo.get("room").getAsLong();
			}
		}
		
		if(roomNumber == -1) {
			int[] user_numbers = {user_number};
			roomNumber = createRoom(team_number, user_numbers);
		}
		
		storedRoomNumberMap.put(user_number, roomNumber);
		
		return roomNumber;
	}
	
}

@@ -128,45 +128,8 @@
 y;%0D%0A
-import com.google.gson.JsonElement;%0D%0A
 impo
@@ -1907,454 +1907,63 @@
 %0D%0A%09%09
-JsonObject roomList = getRoomList(team_number); %0D%0A%09%09JsonArray roomListArray = roomList.get(%22rooms%22).getAsJsonArray();%0D%0A%09%09%0D%0A%09%09long roomNumber = -1;%0D%0A%09%09for(JsonElement je:roomListArray) %7B%0D%0A%09%09%09JsonObject roomInfo = je.getAsJsonObject();%0D%0A%09%09%09%0D%0A%09%09%09if (roomInfo.get(%22user%22).getAsInt() == user_number && roomInfo.get(%22users%22).getAsJsonArray().size() == 1) %7B%0D%0A%09%09%09%09roomNumber = roomInfo.get(%22room%22).getAsLong();%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09if(roomNumber == -1) %7B
+//teamup%EC%97%90%EC%84%9C 1:1%EB%8C%80%ED%99%94%EB%B0%A9%EC%9D%B4 %EC%9E%88%EC%9D%84%EA%B2%BD%EC%9A%B0 createRoom%EC%9D%84 %ED%95%98%EB%8D%94%EB%9D%BC%EB%8F%84 %EA%B8%B0%EC%A1%B4%EB%B0%A9%EB%B2%88%ED%98%B8%EB%A5%BC %EB%B0%98%ED%99%98%ED%95%9C%EB%8B%A4.
 %0D%0A
-%09
 %09%09in
@@ -1999,17 +1999,21 @@
 er%7D;%0D%0A%09%09
-%09
+long 
 roomNumb
@@ -2057,25 +2057,16 @@
 bers);%0D%0A
-%09%09%7D%0D%0A%09%09%0D%0A
 %09%09stored

Optimize getRoomNumber method