package gdi.ws0809.test;

import gdi1sokoban.exceptions.InternalFailureException;
import gdi1sokoban.exceptions.InvalidLevelException;
import gdi1sokoban.game.SokobanWindow;

import java.io.File;
import java.io.IOException;

/*============================================================================*/
/**
 * Provide an implementation for SokobanTest in this class. Your game must
 * compile and run without this class. This class is just meant to adapt the 
 * test cases to your implementation.
 *
 * @author Daniel
 * @author [Editied by] GDI1 group: 4 get points - Work IT out
 * @since February, 07th 2009
 *
 */
public class SokobanTestAdapter implements SokobanTest
{
	private static SokobanWindow mainWindow;
	
	public static void init()
	{
		try
		{
			// create an instance of the game-window
			mainWindow = new SokobanWindow("Sokoban", false);
			// set the game-window visible
			mainWindow.setVisible(false);
			mainWindow.setPlayAudio(false);
			// set the default position
			mainWindow.setLocation(50, 50);
			mainWindow.getFileHandler().setLevelDirectory("bin");
		}
		catch (InternalFailureException e) {
			System.out.println("InternalFailureException in SokobanTestAdapter init(): " + e.getMessage());
		}
		catch (IOException e) {
			System.out.println("FileNotFoundException in SokobanTestAdapter init(): " + e.getMessage());
		}
	}
	

	public int getWallCount()
	{
		return mainWindow.getLevelCheck().walls;
	}
	
	public int getCrateCount() 
	{
		return mainWindow.getLevelCheck().boxes;
	}

	public int getGoalCount() 
	{
		return mainWindow.getLevelCheck().targets;
	}

	public void writeHighScoreFile()
	{
		mainWindow.getFileHandler().saveHighscorelist(mainWindow.getHighScore().toString());
	}

	public void loadGame(File saveFile)
	{
		try {
			mainWindow.getSavegameHandler().loadGame(saveFile);
		}
		catch (IOException e) { }
		catch (InvalidLevelException e) { }
	}

	public void saveGame(File f)
	{
		try
		{
			mainWindow.getSavegameHandler().saveGame(f);
		}
		catch (IOException e) { }
	}

	public boolean createHighscoreEntry(String playername, int i, int j, int k) 
	{
		if(!mainWindow.getHighScore().addEntry(""+i, playername, ""+j, ""+k)) return false;
		return true;
	}

	public String getBestPlayerName()
	{
		mainWindow.getHighScore().listSort("ZUEGE");
		return mainWindow.getHighScore().getContentFor("SPIELERNAME", 0);
	}

	public int getHighscoreCount() 
	{
		return mainWindow.getHighScore().getSize();
	}

	public void clearHighscoreList() 
	{
		mainWindow.getHighScore().dropHighscoreList();
	}
	
	public void loadLevel(File lvl) throws Exception
	{
		mainWindow.getGameActions().initGame(lvl.getAbsolutePath());
	}

	public void setLevelDir(File levelDir)
	{
		mainWindow.getGameActions().changeLevelDir(levelDir.getAbsolutePath());
	}

	public void startNextLevel() throws Exception
	{
		mainWindow.getGameActions().loadNextLevel();
	}
	
	public boolean isDeadlock()
	{
		return mainWindow.getGameActions().isDeadLock();
	}	
	
	public boolean canMoveCrate(int i, int j, char c)
	{
		return mainWindow.getGameActions().canMoveToDirection(c, i, j);
	}

	public void redoLastUndoneMove() throws Exception
	{
		mainWindow.getGameActions().redoAction();
	}

	public void undoLastMove() throws Exception
	{
		mainWindow.getGameActions().undoAction();
	}
	
	public void moveWorker(char direction)
	{
		mainWindow.getGameActions().moveWorker(direction, true);
	}
	
	public String currentLevelToString()
	{
		return mainWindow.getCurrentGameAreaString(true);
	}

	public int getStepsInCurrentLevel()
	{
		return mainWindow.getUserSteps();
	}

	public void setPlayerName(String name)
	{
		mainWindow.setCurrentPlayerName(name);
		mainWindow.getPanel().refreshGameStatus();
	}

	public int getLevelHeight() 
	{
		return mainWindow.getPanel().layoutHeight;
	}

	public int getLevelWidth()
	{
		return mainWindow.getPanel().layoutWidth;
	}
	
	public void moveWorkerSequence(String moves) 
	{
		for(int i = 0; i < moves.length(); i++)
		{
			mainWindow.getGameActions().moveWorker(moves.charAt(i), true);
		}
	}

	public int getWorkerPositionX() 
	{
		return mainWindow.getGameActions().workerPosition[0];
	}

	public int getWorkerPositionY()
	{
		return mainWindow.getGameActions().workerPosition[1];
	}

	public boolean isCrateAt(int i, int j) 
	{
		if(
			mainWindow.getGameActions().currentGameArea[j][i]=='$' ||
			mainWindow.getGameActions().currentGameArea[j][i]=='*'
		) return true;
		else return false;
	}

	public boolean isGoalAt(int i, int j)
	{
		if(
				mainWindow.getGameActions().currentGameArea[j][i]=='.' ||
				mainWindow.getGameActions().currentGameArea[j][i]=='+' ||
				mainWindow.getGameActions().currentGameArea[j][i]=='*'
			) return true;
			else return false;
	}

	public boolean isWallAt(int i, int j) 
	{
		if(mainWindow.getGameActions().currentGameArea[j][i]=='#') return true;
		else return false;
	}	
	
	public boolean isSolved()
	{
		return mainWindow.getGameActions().isLevelSolved();
	}

	public boolean checkWallBounding()
	{
		return mainWindow.getLevelCheck().checkWallBounding(mainWindow.getGameActions().currentGameArea);
	}

	public String solveLevel(int maxTime)
	{
		return null;
	}
}