package de.sopa.scene.score;

import de.sopa.model.game.Level;
import de.sopa.scene.BaseScene;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;

/**
 * @author Raphael Schilling
 */
public class ScoreScreen extends BaseScene {

    public ScoreScreen(Level level) {
        super(level);
    }

    @Override
    public void createScene(final Object o) {
        final Level level = (Level) o;
        String levelCompleteString = level.getLevelInfo().getLevelId() + ". Level\nComplete";
        Text levelCompleteTextShape = new Text((float) (camera.getWidth() * 0.05), (float) (camera.getHeight() * 0.05), resourcesManager.movesScoreFont, levelCompleteString, vbom);
        levelCompleteTextShape.setScaleCenter(0, 0);
        levelCompleteTextShape.setScale(2);
        attachChild(levelCompleteTextShape);
        attachChild(new Text((float) (camera.getWidth() * 0.05), (float) (camera.getHeight() * 0.4), resourcesManager.movesScoreFont, "You're moves: \t\t\t" + level.getMovesCount() + "\nMoves for 3 Stars: " + level.getMinimumMovesToSolve(), vbom));
        attachChild(new Sprite(0, (float) (camera.getHeight() * 0.55), 400, 400, resourcesManager.starRegion, vbom));
        attachChild(new Sprite((float) (camera.getWidth() * 0.64), (float) (camera.getHeight() * 0.55), 400, 400, (level.getLevelInfo().getStars() == 3) ? resourcesManager.starRegion : resourcesManager.starSWRegion, vbom));
        attachChild(new Sprite((camera.getWidth() / 2 - 200), (float) (camera.getHeight() * 0.6), 400, 400, (level.getLevelInfo().getStars() >= 2) ? resourcesManager.starRegion : resourcesManager.starSWRegion, vbom));
        ButtonSprite choiceLevelButton = new ButtonSprite(0, (camera.getHeight() - 400), resourcesManager.backToChoiceRegion, vbom, new ButtonSprite.OnClickListener() {
            @Override
            public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
                sceneService.loadLevelChoiceSceneFromScoreScene();
            }
        });
        ButtonSprite nextLevelButton = new ButtonSprite((float) (camera.getWidth() * 0.64), (camera.getHeight() - 400), resourcesManager.nextLevelRegion, vbom, new ButtonSprite.OnClickListener() {
            @Override
            public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
                int nextLevelId = level.getId() + 1;
                if (nextLevelId > levelService.getLevelCount()) {
                    sceneService.loadLevelChoiceSceneFromScoreScene();
                } else {
                    sceneService.loadGameSceneFromScoreScene(levelService.getLevelById(nextLevelId));
                }
            }
        });

        ButtonSprite backToMenuButton = new ButtonSprite((camera.getWidth() / 2 - 200), (camera.getHeight() - 400), resourcesManager.backToMenuRegionA, vbom, new ButtonSprite.OnClickListener() {
            @Override
            public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
                sceneService.loadMenuSceneFromScoreScene();
            }
        });
        registerTouchArea(choiceLevelButton);
        registerTouchArea(nextLevelButton);
        registerTouchArea(backToMenuButton);
        attachChild(choiceLevelButton);
        attachChild(backToMenuButton);
        attachChild(nextLevelButton);
    }

    @Override
    public void onBackKeyPressed() {
        sceneService.loadMenuSceneFromGameScene();
    }

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

@@ -2915,26 +2915,26 @@
 nSprite 
-backToMenu
+levelAgain
 Button =
@@ -3246,36 +3246,36 @@
 ceneService.load
-Menu
+Game
 SceneFromScoreSc
@@ -3270,32 +3270,72 @@
 eFromScoreScene(
+levelService.getLevelById(level.getId())
 );%0A            %7D
@@ -3463,26 +3463,26 @@
 uchArea(
-backToMenu
+levelAgain
 Button);
@@ -3542,26 +3542,26 @@
 chChild(
-backToMenu
+levelAgain
 Button);

change middle button to load last played level again