package sdm;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

import adm.ADM;

public class SDM {
	
	private int _width, _height;
	private Block _map[][];
	
	private static SDM uniqueInstance;
	
	private SDM() {
		
	}
	
	public static synchronized SDM getInstance() {
		if ( uniqueInstance == null ) {
			uniqueInstance = new SDM();
		}
		return uniqueInstance;
	}
	
	public void readMap(String path) {
		assert path != null : "Path is null";
		
		try {
			FileReader fr;
			fr = new FileReader(path);
			BufferedReader br=new BufferedReader(fr);
			
			analyisFile(br);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.err.println("File is not exist.");
		}
	}
	
	public void analyisFile(BufferedReader br) {
		
		String Input;
		StringTokenizer st;
		
		try {
			Input = br.readLine();
			assert Input != null : "Wrong Format of File.";
			st = new StringTokenizer(Input);
			
			Input = st.nextToken();
			assert isLegal(Input) : "Wrong Format of File.";
			_width = Integer.parseInt(Input);
			
			Input = st.nextToken();
			assert isLegal(Input) : "Wrong Format of File.";
			_height = Integer.parseInt(Input);
			
			assert _width > 0 && _height > 0 : "Wrong Map Size.";
			_map = new Block[_height][_width];
			
			for (int y=0;y<_height;y++){
				Input = br.readLine();
				st = new StringTokenizer(Input);
				assert Input != null : "Wrong Format of File.";
				assert st.countTokens() == ( 2 * _width ) : "Wrong Format of File.";
				
				for (int x=0;x<_width;x++) {
					int value, bool;
					Input = st.nextToken();
					assert isLegal(Input) : "Wrong Format of File.";
					value = Integer.parseInt(Input);
					
					Input = st.nextToken();
					assert isLegal(Input) : "Wrong Format of File.";
					bool = Integer.parseInt(Input);
					assert bool == 1 || bool == 0 : "Wrong Format of File.";
					
					_map[y][x] = new Block((bool == 1 ? true : false), value);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private boolean isLegal(String str) {
		if ( str == null || str.length() == 0 ) return false;
		for( int i=0;i<str.length();i++) {
			if ( ! Character.isDigit(str.charAt(i)) ) {
				return false;
			}
		}
		return true;
	}
	
	public boolean isWalkable(float x, float y) {
		int X = (int) Math.floor(x), Y = (int) Math.floor(y);
		X /= ADM.getInstance().getMapWidth();
		Y /= ADM.getInstance().getMapHeight();
		return ( X >= 0 && X < _width && Y >= 0 && Y < _height ) ? _map[Y][X].isWalkable() : false;
	}
	
	public boolean isLegal(int x, int y) {
		return ( x >= 0 && x < _width && y >= 0 && y < _height );
	}
	
	public int getAssetIndex(int x, int y) {
		assert isLegal(x, y) : "Invalid Index.";
		return _map[y][x].getAssetIndex();
	}
	
	public int getWidth() {
		return _width;
	}
	
	public int getHeight() {
		return _height;
	}
	
	public void printMap() {
		for (int i=0;i<_height;i++) {
			for (int j=0;j<_width;j++) {
				System.out.print(_map[i][j].getAssetIndex() + " ");
			}
			System.out.println();
		}
	}
}

@@ -803,29 +803,30 @@
 %0A%09%09%7D%0A%09%7D%0A%09%0A%09p
-ublic
+rivate
  void analyi
@@ -2391,87 +2391,27 @@
 ble(
-float x, float y) %7B%0A%09%09int X = (int) Math.floor(x), Y = (int) Math.floor(y);%0A%09%09X
+int x, int y) %7B%0A%09%09x
  /= 
@@ -2445,17 +2445,17 @@
 th();%0A%09%09
-Y
+y
  /= ADM.
@@ -2495,17 +2495,17 @@
 eturn ( 
-X
+x
  %3E= 0 &&
@@ -2505,17 +2505,17 @@
 %3E= 0 && 
-X
+x
  %3C _widt
@@ -2519,17 +2519,17 @@
 idth && 
-Y
+y
  %3E= 0 &&
@@ -2529,17 +2529,17 @@
 %3E= 0 && 
-Y
+y
  %3C _heig
@@ -2554,12 +2554,12 @@
 map%5B
-Y%5D%5BX
+y%5D%5Bx
 %5D.is

Fix Function in SDM