package donnu.zolotarev.SpaceShip.Levels;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class LevelController {

    private static transient LevelController instance = new LevelController();

    private HashMap<Integer,LevelInfo> levels;
    private int excessCounter = 0;

    public static LevelController getInstance() {
        return instance;
    }

    public void load(String s){
        Gson gson = new Gson();
        excessCounter = 0;
        levels = gson.fromJson(s,new TypeToken<Map<Integer,LevelInfo>>(){}.getType());
        changeEnabled();
    }

    private LevelController() {
        excessCounter = 0;
        levels = new HashMap<Integer, LevelInfo>();
    }

    public void addLevel(int levelId, boolean isInfinity){
        if( !(WaveContainer.LEVEL_1 <= levelId && levelId <= WaveContainer.LEVEL_19)){
            excessCounter++;
        }
        levels.put(levelId, new LevelInfo(levelId, isInfinity)) ;
    }

    public void changeStateById(int id, boolean win){
        LevelInfo item = levels.get(id);
        if (!item.isInfinity()  ){
            item.setWin(win || item.isWin());
            item.setNew(false);
        }
    }

    public boolean newestById(int id){
        LevelInfo item = levels.get(id);
        return item.isNew() && !item.isInfinity();
    }

    public String toJson(){
        Gson gson = new Gson();
        return gson.toJson(levels);
    }

    public void changeEnabled(){
        Iterator<Integer> iter =  levels.keySet().iterator();
        Boolean isWin = true;
        while (iter.hasNext()) {
            LevelInfo item = levels.get(iter.next());
            item.setEnabled(isWin);
            isWin = item.isWin();
        }
    }

    public LevelInfo getById(int type) {
        return levels.get(type);
    }

    public int getSize(){
       return levels.size() - excessCounter;
    }

    public LevelInfo getLevelInfoForAdapter(int pos){
        return levels.get(WaveContainer.LEVEL_1+pos);
    }

}

@@ -106,16 +106,75 @@
 peToken;
+%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;
 %0A%0Aimport
@@ -275,16 +275,148 @@
 ller %7B%0A%0A
+    private static final String KEY_ITEMS = %22KEY_ITEMS%22;%0A    private static final String KEY_EXCESS_COUNTER = %22KEY_EXCESS_COUNTER%22;%0A
     priv
@@ -755,32 +755,184 @@
     
-levels = gson.fromJson(s
+try %7B%0A            JSONObject json = new JSONObject(s);%0A            excessCounter = json.getInt(KEY_EXCESS_COUNTER);%0A            levels = gson.fromJson(json.getString(KEY_ITEMS)
 ,new
@@ -978,24 +978,25 @@
 getType());%0A
+%0A
         chan
@@ -1004,24 +1004,103 @@
 eEnabled();%0A
+        %7D catch (JSONException e) %7B%0A            e.printStackTrace();%0A        %7D%0A
     %7D%0A%0A    p
@@ -1905,33 +1905,251 @@
     
-return gson.toJson(levels
+JSONObject object = new JSONObject();%0A        try %7B%0A            object.put(KEY_ITEMS,gson.toJson(levels));%0A            object.put(KEY_EXCESS_COUNTER, excessCounter);%0A        %7D catch (JSONException e) %7B%0A        %7D%0A        return object.toString(
 );%0A 

fix level bug size