/*
 *   Copyright 2014 oddlydrawn
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.tumblr.oddlydrawn.nahlc;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;

/** @author oddlydrawn */
public class SavedStuff {
	private final int LEVEL = 0;
	private final int SCORE = 1;
	private String[][] allTheScoresStrings = { {"9", "10000"}, {"8", "9000"}, {"7", "8000"}, {"6", "7000"}, {"5", "6000"},
		{"4", "5000"}, {"3", "4000"}, {"2", "3000"}, {"1", "2000"}, {"0", "1000"}};
	private int previousScore;
	private int scoreToReplace;
	private boolean newRecord;
	private boolean upsideDown = false;

	public SavedStuff () {
		try {
			String scoresString;
			FileHandle scoresHandle;
			if (Gdx.files.local("scores.txt").exists()) {
				scoresHandle = Gdx.files.local("scores.txt");
				scoresString = scoresHandle.readString();
				String scoresArray[] = scoresString.split(",");
				int counter = 0;
				for (int x = 0; x < 10; x++) {
					for (int y = 0; y < 2; y++) {
						allTheScoresStrings[x][y] = scoresArray[counter];
						counter++;
					}
				}
			}
		} catch (Exception e) {
		}
	}

	public void updateLevelAndScore (int level, int score) {
		newRecord = false;
		previousScore = score;
		for (int x = 0; x < 10; x++) {
			if (score > getScoreStringToInt(x)) {
				newRecord = true;

				moveScoresDown(x);
				allTheScoresStrings[x][LEVEL] = String.valueOf(level);
				allTheScoresStrings[x][SCORE] = String.valueOf(score);
				return;
			}
		}

	}

	public void saveScoresToFile () {
		String scoresString = "";
		FileHandle scoresHandle = Gdx.files.local("scores.txt");
		for (int x = 0; x < 10; x++) {
			for (int y = 0; y < 2; y++) {
				scoresString += allTheScoresStrings[x][y];
				scoresString += ",";
			}
		}
		scoresHandle.writeString(scoresString, false);
	}

	public void checkScoreToTable () {
		newRecord = false;
		for (int x = 0; x < 10; x++) {
			if (previousScore > getScoreStringToInt(x)) {
				newRecord = true;
				scoreToReplace = x;
				return;
			}
		}
	}

	public int getScoreStringToInt (int index) {
		return Integer.valueOf(allTheScoresStrings[index][SCORE]);
	}

	public void moveScoresDown (int index) {
		for (int x = 9; x > index; x--) {
			allTheScoresStrings[x][LEVEL] = allTheScoresStrings[x - 1][LEVEL];
			allTheScoresStrings[x][SCORE] = allTheScoresStrings[x - 1][SCORE];
		}
	}

	public void setScore (int score) {
		previousScore = score;
		checkScoreToTable();
	}

	public String getHighScore () {
		return allTheScoresStrings[0][SCORE];
	}

	public String getLevel (int x, int y) {
		return allTheScoresStrings[x][y];
	}

	public String getScore (int x, int y) {
		return allTheScoresStrings[x][y];
	}

	public int getScoreToReplace () {
		return scoreToReplace;
	}

	public String getPreviousScore () {
		return String.valueOf(previousScore);
	}

	public boolean isUpsideDown () {
		return upsideDown;
	}

	public boolean isPreviousScoreInTopScore () {
		return newRecord;
	}
}

@@ -770,16 +770,92 @@
 Stuff %7B%0A
+%09private final int SCORES_HEIGHT = 10;%0A%09private final int SCORES_WIDTH = 2;%0A
 %09private
@@ -876,16 +876,16 @@
 EL = 0;%0A
-
 %09private
@@ -1545,34 +1545,45 @@
 (int x = 0; x %3C 
-10
+SCORES_HEIGHT
 ; x++) %7B%0A%09%09%09%09%09fo
@@ -1592,33 +1592,44 @@
 (int y = 0; y %3C 
-2
+SCORES_WIDTH
 ; y++) %7B%0A%09%09%09%09%09%09a
@@ -1864,34 +1864,45 @@
 (int x = 0; x %3C 
-10
+SCORES_HEIGHT
 ; x++) %7B%0A%09%09%09if (
@@ -2261,34 +2261,45 @@
 (int x = 0; x %3C 
-10
+SCORES_HEIGHT
 ; x++) %7B%0A%09%09%09for 
@@ -2314,17 +2314,28 @@
  0; y %3C 
-2
+SCORES_WIDTH
 ; y++) %7B
@@ -2548,18 +2548,29 @@
  0; x %3C 
-10
+SCORES_HEIGHT
 ; x++) %7B
@@ -2858,17 +2858,33 @@
 int x = 
-9
+SCORES_HEIGHT - 1
 ; x %3E in

Remove magic numbers, create final ints