package de.sopa.scene.game;


import de.sopa.helper.LevelFileService;
import de.sopa.manager.ResourcesManager;
import de.sopa.model.GameService;
import de.sopa.model.GameServiceImpl;
import de.sopa.model.Level;
import de.sopa.observer.Observer;
import de.sopa.scene.BaseScene;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ContinuousHoldDetector;
import org.andengine.util.color.Color;

import java.io.IOException;

/**
 * David Schilling - davejs92@gmail.com
 */
public abstract class GameScene extends BaseScene implements Observer {

    protected GameService gameService;
    private Sprite solvedSprite;
    private Sprite unsolvedSprite;
    private ContinuousHoldDetector continuousHoldDetector;
    private float spacePerTile;
    private Text scoreText;
    private GameFieldView gameFieldView;
    private Level level;
    protected Level levelBackup;

    public GameScene(Object o) {
        super(o);
    }

    @Override
    public void createScene(Object o) {
        initializeLogic(o);
        calculateSpacePerTile(gameService.getLevel().getField().length);
        levelBackup = gameService.getLevel().copy();
        addBackground();
        addTiles();
        addSolvedIcon();
        addButtons();
        addScoreText();
        registerTouchHandler();
        gameService.attach(this);
        resourcesManager.menuMusic.stop();
        resourcesManager.musicIsPlaying = false;
    }

    @Override
    public void update() {
        updateTiles();
        scoreText.setText(String.valueOf(gameService.getLevel().getMovesCount()));
        if (gameService.solvedPuzzle()) {
            gameService.detach(this);
            this.clearTouchAreas();
            this.clearUpdateHandlers();
            baseScene.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() {
                public void onTimePassed(final TimerHandler pTimerHandler) {
                    baseScene.unregisterUpdateHandler(pTimerHandler);
                    onSolvedGame();
                }
            }));


        }
    }


    private void addTiles() {
        float tilesSceneStartY = getTileSceneStartY();
        gameFieldView = new GameFieldView(0, tilesSceneStartY, spacePerTile,
                gameService, resourcesManager.regionTileMap, vbom, resourcesManager.tilesBorderRegion);
        gameFieldView.addTiles();
        attachChild(gameFieldView);

    }

    private void updateTiles() {
        detachChild(gameFieldView);
        gameFieldView.addTiles();
        attachChild(gameFieldView);
    }


    private void calculateSpacePerTile(int width) {
        spacePerTile = camera.getWidth() / width;
    }

    private float getTileSceneStartY() {
        return (camera.getHeight() - (spacePerTile * gameService.getLevel().getField().length)) / 2;
    }

    private void addScoreText() {
        scoreText = new Text(camera.getWidth() * 0.7f, camera.getHeight() * 0.01f, resourcesManager.scoreFont, String.valueOf(gameService.getLevel().getMovesCount()), 4, vbom);
        attachChild(scoreText);
        Text minimumMovesScore = new Text(0, camera.getHeight() * 0.01f, resourcesManager.scoreFont, String.valueOf(gameService.getLevel().getMinimumMovesToSolve()), vbom);
        attachChild(minimumMovesScore);
    }

    private void registerTouchHandler() {
        final float widthPerTile = camera.getWidth() / gameService.getLevel().getField().length;
        GameSceneHoldDetector gameSceneHoldDetector = new GameSceneHoldDetector(widthPerTile, getTileSceneStartY() + widthPerTile, widthPerTile, gameFieldView, gameService, camera.getWidth());
        continuousHoldDetector = new ContinuousHoldDetector(0, 100, 0.01f, gameSceneHoldDetector);
        registerUpdateHandler(continuousHoldDetector);
        setOnSceneTouchListener(continuousHoldDetector);
    }


    private void addSolvedIcon() {

        solvedSprite = new Sprite(0, 0, 50, 50, resourcesManager.regionTileMap.get('s'), vbom);
        unsolvedSprite = new Sprite(0, 0, 50, 50, resourcesManager.regionTileMap.get('i'), vbom);
        attachChild(solvedSprite);
        attachChild(unsolvedSprite);
    }

    protected abstract void addButtons();


    private void addBackground() {
        setBackground(new Background(Color.BLACK));
    }

    private void initializeLogic(Object o) {
        if (o != null && o instanceof Level) {
            level = (Level) o;
        }
        gameService = new GameServiceImpl();
        gameService.startGame(level);

    }

    @Override
    public abstract void onBackKeyPressed();

    @Override
    public void disposeScene() {
        final GameScene gameScene = this;
        engine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() {
            public void onTimePassed(final TimerHandler pTimerHandler) {
                engine.unregisterUpdateHandler(pTimerHandler);
                gameScene.detachChildren();
            }
        }));
    }

    /**
     * is called when the game is solved
     */
    public abstract void onSolvedGame();
}

@@ -27,89 +27,8 @@
 ;%0A%0A%0A
-import de.sopa.helper.LevelFileService;%0Aimport de.sopa.manager.ResourcesManager;%0A
 impo
@@ -392,153 +392,16 @@
 ity.
-sprite.ButtonSprite;%0Aimport org.andengine.entity.sprite.Sprite;%0Aimport org.andengine.entity.text.Text;%0Aimport org.andengine.input.touch.TouchEven
+text.Tex
 t;%0Ai
@@ -509,37 +509,8 @@
 r;%0A%0A
-import java.io.IOException;%0A%0A
 /**%0A
@@ -669,76 +669,8 @@
 ce;%0A
-    private Sprite solvedSprite;%0A    private Sprite unsolvedSprite;%0A
     
@@ -1199,33 +1199,8 @@
 ();%0A
-        addSolvedIcon();%0A
     
@@ -3804,317 +3804,8 @@
 %7D%0A%0A%0A
-    private void addSolvedIcon() %7B%0A%0A        solvedSprite = new Sprite(0, 0, 50, 50, resourcesManager.regionTileMap.get('s'), vbom);%0A        unsolvedSprite = new Sprite(0, 0, 50, 50, resourcesManager.regionTileMap.get('i'), vbom);%0A        attachChild(solvedSprite);%0A        attachChild(unsolvedSprite);%0A    %7D%0A%0A
     

remove solved icon from game scene