package de.heatup.highscore;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import com.sun.org.apache.xml.internal.serializer.SerializationHandler;

import de.heatup.cfg.Config;
import de.heatup.logging.Logger;
/**
 * 
 * @author daniel
 *
 */
public class HighscoreHandler {
	private int mapHash;
	private MapHighscores mapHighscores;
	/**
	 * Es muss vorher eine Karte geladen sein
	 */
	public HighscoreHandler(String mapString) {
		mapHash = mapString.hashCode();
		
		try {
			FileInputStream fileIn = new FileInputStream("highscore.dat");
			ObjectInputStream in = new ObjectInputStream(fileIn);
			mapHighscores = (MapHighscores) in.readObject();
			in.close();
			fileIn.close();
			Logger.write("loaded highscore.dat");
		} catch (FileNotFoundException fe) {
			mapHighscores = new MapHighscores();
			FileOutputStream fileOut = null;
			try {
				fileOut = new FileOutputStream("highscore.dat");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ObjectOutputStream out = null;
			try {
				out = new ObjectOutputStream(fileOut);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				out.writeObject(mapHighscores);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				fileOut.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Logger.write("created new highscore.dat");
			
			
			
			FileInputStream fileIn = null;
			try {
				fileIn = new FileInputStream("highscore.dat");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ObjectInputStream in = null;
			try {
				in = new ObjectInputStream(fileIn);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				mapHighscores = (MapHighscores) in.readObject();
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				fileIn.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Logger.write("loaded highscore.dat");
		} catch (IOException e) {
			
			e.printStackTrace();
			return;
		} catch (ClassNotFoundException ce) {
			Logger.write("MapHighscore class not found");
			ce.printStackTrace();
			return;
		} 
		
		
	}
	public int getCurrentMapHash() {
		return mapHash;
	}
	public void setHighscore(int mapHash, String name, int score) {
		mapHighscores.setHighscore(mapHash, name, score);
	}
	public ArrayList<ScoreEntry> getHighscores(int mapHash) {
		return mapHighscores.getHighscores(mapHash);
	}
	public boolean saveHighscores() {
		try {
			FileOutputStream fileOut = new FileOutputStream("highscore.dat");
			ObjectOutputStream out = new ObjectOutputStream(fileOut);
			out.writeObject(mapHighscores);
			out.close();
			fileOut.close();
			Logger.write("saved highscore.dat");
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			Logger.write("error saving highscore.dat");
			return false;
		}
	}
		
}
