package donnu.zolotarev.SpaceShip.Scenes;

import android.content.Context;
import com.google.gson.Gson;
import donnu.zolotarev.SpaceShip.GameData.HeroFeatures;
import donnu.zolotarev.SpaceShip.GameData.UserData;
import donnu.zolotarev.SpaceShip.GameState.IParentScene;
import donnu.zolotarev.SpaceShip.Levels.LevelController;
import donnu.zolotarev.SpaceShip.Levels.WaveContainer;
import donnu.zolotarev.SpaceShip.Scenes.Interfaces.IHardKey;
import donnu.zolotarev.SpaceShip.SpaceShipActivity;
import org.andengine.entity.scene.Scene;

public abstract class MyScene extends Scene implements IHardKey {

    protected static final String FILE_GAME_DATA = "file_game_data";
    protected static final String FILE_LEVELS = "file_levels";

    protected static final String PREF_USER_STATS = "pref_user_stats";
    protected static final String PREF_LEVELS = "pref_levels";

    public MyScene(IParentScene parentScene) {
    }

    public void saveGameState(){
        UserData data =  UserData.get();
        Gson gson = new Gson();

        SpaceShipActivity shipActivity =  SpaceShipActivity.getInstance();
        shipActivity.getSharedPreferences(FILE_GAME_DATA, Context.MODE_PRIVATE)
                .edit().putString(PREF_USER_STATS,gson.toJson(data))
                .commit();
    }

    public void loadGame(){
        SpaceShipActivity shipActivity =  SpaceShipActivity.getInstance();
        UserData.create(shipActivity.getSharedPreferences(FILE_GAME_DATA, Context.MODE_PRIVATE).
                getString(PREF_USER_STATS,""));

        // ToDo загрузка данных
        HeroFeatures.create("");

    }

    public void saveLevels(LevelController levels){
        SpaceShipActivity shipActivity =  SpaceShipActivity.getInstance();
        shipActivity.getSharedPreferences(FILE_LEVELS, Context.MODE_PRIVATE)
                .edit().putString(PREF_LEVELS,levels.toJson())
                .commit();
    }

    public boolean haveCurrentGame(){
        SpaceShipActivity shipActivity =  SpaceShipActivity.getInstance();
        String levelsJson =  shipActivity.getSharedPreferences(FILE_LEVELS, Context.MODE_PRIVATE)
                .getString(PREF_LEVELS,"");
        return !levelsJson.isEmpty();
    }

    public void clearCurrentGame(){
        SpaceShipActivity shipActivity =  SpaceShipActivity.getInstance();
        shipActivity.getSharedPreferences(FILE_GAME_DATA, Context.MODE_PRIVATE)
                .edit().putString(PREF_USER_STATS,"")
                .commit();
        shipActivity.getSharedPreferences(FILE_LEVELS, Context.MODE_PRIVATE)
                .edit().putString(PREF_LEVELS,"")
                .commit();
    }


    public LevelController loadLevels(){
        LevelController levels;
        SpaceShipActivity shipActivity =  SpaceShipActivity.getInstance();
        String levelsJson =  shipActivity.getSharedPreferences(FILE_LEVELS, Context.MODE_PRIVATE)
                .getString(PREF_LEVELS,"");
        if (!levelsJson.isEmpty()){
            levels = new LevelController(levelsJson);
        } else {
            levels = new LevelController();
            levels.addLevel(WaveContainer.LEVEL_INFINITY, 100, 100, true);
            levels.addLevel(WaveContainer.LEVEL_1, 200,300, false);
            levels.addLevel(WaveContainer.LEVEL_2, 300,350, false);
            levels.addLevel(WaveContainer.LEVEL_3, 400,180, false);
            levels.addLevel(WaveContainer.LEVEL_4, 500,180, false);
            levels.addLevel(WaveContainer.LEVEL_5, 600,300, false);
            levels.addLevel(WaveContainer.LEVEL_6, 800,400, false);
            levels.addLevel(WaveContainer.LEVEL_TEST, 700,180, false);
            levels.changeEnabled();
        }

        return levels;
    }

}

@@ -800,16 +800,87 @@
 stats%22;%0A
+    protected static final String PREF_HERO_STATS = %22pref_hero_stats%22;%0A
     prot
@@ -930,24 +930,24 @@
 ef_levels%22;%0A
-
 %0A    public 
@@ -1026,49 +1026,8 @@
 ()%7B%0A
-        UserData data =  UserData.get();%0A
     
@@ -1225,32 +1225,49 @@
          .edit()
+%0A                
 .putString(PREF_
@@ -1289,20 +1289,106 @@
 .toJson(
-data
+UserData.get()))%0A                .putString(PREF_HERO_STATS,gson.toJson(HeroFeatures.get()
 ))%0A     
@@ -1677,63 +1677,147 @@
     
-// ToDo %D0%B7%D0%B0%D0%B3%D1%80%D1%83%D0%B7%D0%BA%D0%B0 %D0%B4%D0%B0%D0%BD%D0%BD%D1%8B%D1%85%0A        HeroFeatures.create(
+HeroFeatures.create(shipActivity.getSharedPreferences(FILE_GAME_DATA, Context.MODE_PRIVATE).%0A                getString(PREF_HERO_STATS,
 %22%22)
+)
 ;%0A%0A 
@@ -2629,32 +2629,49 @@
          .edit()
+%0A                
 .putString(PREF_
@@ -2681,24 +2681,71 @@
 R_STATS,%22%22)%0A
+                .putString(PREF_HERO_STATS,%22%22)%0A
             

save hero stats