package org.gamefolk.roomfullofcats;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import com.arcadeoftheabsurd.absurdengine.DeviceUtility;
import com.eclipsesource.json.JsonObject;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

class Level
{
	int number;
	int mapWidth;
	int mapHeight;
	int levelTime;
	int fallTime;  // interval after which cats fall, in seconds
	int catsLimit; // the target number of cats of the same type to collect
	String message;
	String title;
}

public class CatsGameManager 
{
	static int curLevel = 1;
	private static Context context;
	private static CatsGame game;
	
	private static final String TAG = "RoomFullOfCats";
	
	static class ToastView extends TextView
	{
		public ToastView(Context context, String message) {
			super(context);
			setTextSize(DeviceUtility.isIOS() ? 12 : 20);
			setText(message);
		}
	}
	
	static void initialize(Context context, CatsGame game) {
		CatsGameManager.context = context;
		CatsGameManager.game = game;
	}
	
	static void displayLevelMessage(final String message) {
		((Activity)context).runOnUiThread(new Runnable() {
	        public void run() {
	        	Toast toast = new Toast(context.getApplicationContext());
	    		toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
	    		toast.setDuration(Toast.LENGTH_LONG);
	    		toast.setView(new ToastView(context.getApplicationContext(), message));
	    		toast.show();
	        }
	    });
	}
	
	static void startLevel() {
		Level level = loadLevel();
		displayLevelMessage(level.message);
		game.makeLevel(level);
		game.setupGraphics();
		game.startGame();
	}
	
	static Level loadLevel() {
		Level level = new Level();
		
		level.number = curLevel;
		
		if (curLevel == 1) {
			InputStream input = context.getResources().openRawResource(context.getResources().getIdentifier("level1", "raw", "org.gamefolk.roomfullofcats"));
			Writer writer = new StringWriter();
			char[] buffer = new char[1024];
			try {
				try {
					Reader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
					int n;
					while ((n = reader.read(buffer)) != -1) {
						writer.write(buffer, 0, n);
					}
				} finally {
					input.close();
				}
			}
			catch (IOException e) {
				Log.e(TAG, "error reading level json");
			}			
			Log.v(TAG, "json: " + writer.toString());
			JsonObject mainObject = JsonObject.readFrom(writer.toString());
			
			Log.v(TAG, "level: " + mainObject.get("levelTitle").asString());
			
			level.mapWidth = mainObject.get("columns").asInt();
			level.mapHeight = mainObject.get("rows").asInt();
			level.levelTime = mainObject.get("timeLimit").asInt();
			level.fallTime = 1;
			level.catsLimit = 3;
			level.message = mainObject.get("levelDescription").asString();
			level.title = mainObject.get("levelTitle").asString();
		} else if (curLevel == 2) {
			level.mapWidth = 5;
			level.mapHeight = 5;
			level.levelTime = 10;
			level.fallTime = 1;
			level.catsLimit = 3;
			level.message = "... but those people are fucking stupid.";
			level.title = "Level 2";
		}
		return level;
	}
}

@@ -288,50 +288,8 @@
 ity;
-%0Aimport com.eclipsesource.json.JsonObject;
 %0A%0Aim
@@ -463,16 +463,76 @@
 Toast;%0A%0A
+import org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0A
 class Le
@@ -2108,37 +2108,32 @@
 w%22, 
-%22org.gamefolk.roomfullofcats%22
+context.getPackageName()
 ));%0A
@@ -2580,54 +2580,75 @@
 ));%0A
+%0A
 %09%09%09J
-sonObject mainObject = JsonObject.readFrom
+SONObject mainObject;%0A%09%09%09try %7B%0A%09%09%09%09mainObject = new JSONObject
 (wri
@@ -2667,19 +2667,16 @@
 ());%0A%09%09%09
-%0A%09%09
 %09Log.v(T
@@ -2697,32 +2697,38 @@
 + mainObject.get
+String
 (%22levelTitle%22).a
@@ -2729,28 +2729,14 @@
 le%22)
-.asString()
 );%0A%09%09%09
-%0A%09%09
 %09lev
@@ -2763,16 +2763,19 @@
 ject.get
+Int
 (%22column
@@ -2777,26 +2777,19 @@
 olumns%22)
-.asInt()
 ;%0A
+%09
 %09%09%09level
@@ -2819,16 +2819,19 @@
 .get
+Int
 (%22rows%22)
 .asI
@@ -2826,26 +2826,19 @@
 (%22rows%22)
-.asInt()
 ;%0A
+%09
 %09%09%09level
@@ -2864,16 +2864,19 @@
 ject.get
+Int
 (%22timeLi
@@ -2884,65 +2884,11 @@
 it%22)
-.asInt();%0A%09%09%09level.fallTime = 1;%0A%09%09%09level.catsLimit = 3
 ;%0A
+%09
 %09%09%09l
@@ -2912,24 +2912,30 @@
 inObject.get
+String
 (%22levelDescr
@@ -2942,29 +2942,19 @@
 iption%22)
-.asString()
 ;%0A
+%09
 %09%09%09level
@@ -2976,16 +2976,22 @@
 ject.get
+String
 (%22levelT
@@ -3000,19 +3000,150 @@
 le%22)
-.asString()
+;%0A%09%09%09%7D catch (JSONException e) %7B%0A%09%09%09%09throw new RuntimeException(%22Could not read JSON%22, e);%0A%09%09%09%7D%0A%0A%09%09%09level.fallTime = 1;%0A%09%09%09level.catsLimit = 3
 ;%0A%09%09

remove minimaljson dependency