package world;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import org.newdawn.slick.geom.Rectangle;


public class SaveAndLoadMap
{
	static ArrayList <Rectangle> boxs;
	public static int map[][] = new int[20][25];
	public static ArrayList<Rectangle> getBoxs()
	{
		return boxs;
	}
	public static int[][] Load()
	{
		map = new int[20][25];
		boxs = new ArrayList<Rectangle>();
			try{
			BufferedReader buff = new BufferedReader(new FileReader("./map/map.txt"));
			 System.out.println("--- MAP ---");
				try {
					String line;
					int j = 0;
					while ((line = buff.readLine()) != null)
					{
						int i = 0;
						for(String n : line.split(" "))
						{
							map[j][i++] = Integer.parseInt(n);
						}
						System.out.println(line);
						j++;
					}
				} finally {
					buff.close();
				}
			} catch (IOException ioe) {
				System.out.println("Erreur --" + ioe.toString());
			}
		for(int i=0;i<20;i++)
			for(int j=0;j<25;j++)
				if(map[i][j] != 0 && map[i][j] != 2)
				{
					boxs.add(new Rectangle(j*32, i*32, 32,32));
				}
		return map;
	}
	static void save(int[][] map)
	{
		BufferedWriter buff;
		try {
			buff = new BufferedWriter(new FileWriter("map/map.txt"));
			buff.flush();
			for(int i=0;i<20;i++, buff.newLine())
				for(int j =0; j < 25; j++)
				{
					buff.write(String.valueOf(map[i][j]));
					buff.write(' ');
				}
			buff.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
