package de.sopa.scene.justplay;

import de.sopa.scene.BaseScene;
import de.sopa.model.justplay.JustPlayResult;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.text.Text;

import static org.andengine.util.color.Color.*;

/**
 * @author Raphael Schilling
 * @author David Schilling - davejs92@gmail.com
 */
public class JustPlayScoreScene extends BaseScene {


    public JustPlayScoreScene(final JustPlayResult justPlayResult) {
        super();

        Rectangle rectangleScore = new Rectangle(0, (float) (camera.getHeight() * 0.45), camera.getWidth(), camera.getHeight() / 10, vbom);
        rectangleScore.setColor(0, 102/255f, 255/255f);
        attachChild(rectangleScore);

        Rectangle rectangleTime = new Rectangle(0, (float) (camera.getHeight() * 0.6), camera.getWidth(), (float) (camera.getHeight() * 0.2), vbom);
        rectangleTime.setColor(153/255f,0 , 0);
        attachChild(rectangleTime);


        String levelCompleteString = justPlayResult.getLevelAnzahl() +
                ".  Level\nComplete";
        Text levelCompleteTextShape = new Text((float) (camera.getWidth() * 0.12), (float) (camera.getHeight() * 0.1), resourcesManager.levelCompleteFont, levelCompleteString, 20, vbom);
        attachChild(levelCompleteTextShape);
        Text scoreText = new Text((float) (camera.getWidth() * 0.05), (float) (camera.getHeight() * 0.45), resourcesManager.justPlayScoreFont,
                "Score:      ", vbom);
        scoreText.setColor(BLACK);
        attachChild(scoreText);
        final int[] currentScore = {justPlayResult.getLastScore()};
        final Text score = new Text((float) (camera.getWidth() * 0.6), (float) (camera.getHeight() * 0.45), resourcesManager.justPlayScoreFont, "" + currentScore[0], 8, vbom);
        score.setColor(BLACK);
        attachChild(score);

        Text timeText = new Text((float) (camera.getWidth() * 0.05), (float) (camera.getHeight() * 0.605), resourcesManager.justPlayScoreFont,
                "Left Time:  \t" + justPlayResult.getLeftTime() + "\n" +
                "Extra Time:\t+" + justPlayResult.getExtraTime(), vbom);
        attachChild(timeText);
        timeText.setColor(WHITE);
        ButtonSprite nextLevelButton = new ButtonSprite((camera.getWidth() / 2 - 200), (camera.getHeight() - 400), resourcesManager.nextJustPlayLevel, vbom, new ButtonSprite.OnClickListener() {
            @Override
            public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
                storyService.loadJustPlaySceneFromJustPlayScoreScene();
            }
        });

        attachChild(nextLevelButton);
        registerTouchArea(nextLevelButton);

        engine.registerUpdateHandler(new TimerHandler(0.0005f, true, new ITimerCallback() {
            @Override
            public void onTimePassed(TimerHandler pTimerHandler) {
                if(currentScore[0] >= justPlayResult.getScore()) {
                    score.setText(String.valueOf(justPlayResult.getScore()));
                    engine.unregisterUpdateHandler(pTimerHandler);
                } else {
                    currentScore[0]++;
                    score.setText(String.valueOf(currentScore[0]));
                }
            }
        }));
    }

    @Override
    public void onBackKeyPressed() {
        storyService.loadMenuSceneFromJustPlayScoreScene();
    }

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

@@ -544,16 +544,49 @@
 ene %7B%0A%0A%0A
+    private boolean leaveScene;%0A%0A
     publ
@@ -660,24 +660,58 @@
   super();%0A%0A
+        this.leaveScene = false;%0A%0A
         Rect
@@ -1119,16 +1119,18 @@
 (153
-/
+ / 
 255f,
-0
  
+0
 , 0)
@@ -2739,24 +2739,25 @@
 eaLocalY) %7B%0A
+%0A
             
@@ -2925,17 +2925,16 @@
 utton);%0A
-%0A
         
@@ -3098,32 +3098,70 @@
 TimerHandler) %7B%0A
+                if(!leaveScene) %7B%0A    
                 
@@ -3207,24 +3207,28 @@
 tScore()) %7B%0A
+    
             
@@ -3305,32 +3305,36 @@
                 
+ 
+   
 engine.unregiste
@@ -3380,16 +3380,20 @@
         
+    
 %7D else %7B
@@ -3393,16 +3393,20 @@
  else %7B%0A
+    
         
@@ -3448,32 +3448,36 @@
                 
+    
 score.setText(St
@@ -3504,24 +3504,138 @@
 Score%5B0%5D));%0A
+                    %7D%0A                %7D else %7B%0A                    engine.unregisterUpdateHandler(pTimerHandler);%0A
             
@@ -3721,24 +3721,59 @@
 Pressed() %7B%0A
+            leaveScene = true;%0A    
         stor

Fix crash in JustPlayScoreScene