package adm;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

import javax.imageio.ImageIO;

public class ADM {
	
	private static ADM uniqueInstance;
	
	private BufferedImage[][] _map_assets;
	
	private BufferedImage[][] _player_assets;
	private BufferedImage[][] _monster_assets;
	private BufferedImage[][] _projector_assets;
	private BufferedImage[][] _item_assets;
	
	private ADM() {
		_map_assets = readFile("./resource/Assets/Map/");
		/*
		_player_assets = readFile("./resource/Assets/Player/");
		_monster_assets = readFile("./resource/Assets/Monster/");
		_projector_assets = readFile("./resource/Assets/Projector/");
		_item_assets = readFile("./resource/Assets/Projector/");
		*/
	}
	
	public static synchronized ADM getInstance() {
		if ( uniqueInstance == null ) {
			uniqueInstance = new ADM();
		}
		return uniqueInstance;
	}
	
	private BufferedImage[][] readFile(String path) {
		assert path != null : "Invalid Path.";
		
		try {
			FileReader fr;
			fr = new FileReader(path+"Format.txt");
			BufferedReader br=new BufferedReader(fr);
			
			String Input;
			StringTokenizer st;
			Input = br.readLine();
			st = new StringTokenizer(Input);
			
			assert st.countTokens() == 1 : "Wrong Format.";
			Input = st.nextToken();
			
			assert Input.matches("\\d+") : "Wrong Format.";
			int size = Integer.parseInt(Input);
			
			BufferedImage[][] assets = new BufferedImage[size][];
			
			for (int i=0;i<size;i++) {
				assets[i] = readSpriteSheet(path, br);
			}
			
			return assets;
		} catch (FileNotFoundException e) {
			assert false : "No Such File.";
		} catch (IOException e) {
			assert false : "Wrong Format.";
		}
		return null;
	}
	
	private BufferedImage[] readSpriteSheet(String path, BufferedReader br) {
		try {
			String Input;
			StringTokenizer st;
			
			Input = br.readLine();
			BufferedImage Image = ImageIO.read(new File(path+Input));

			Input = br.readLine();
			st = new StringTokenizer(Input);
			assert st.countTokens() == 2 : "Wrong Format.";
			
			int width, height;
			Input = st.nextToken();
			assert Input.matches("\\d+") : "Wrong Format.";
			width = Integer.parseInt(Input);
			
			Input = st.nextToken();
			assert Input.matches("\\d+") : "Wrong Format.";
			height = Integer.parseInt(Input);
			
			return sliceSpriteSheet(Image, width, height);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			assert false : "Wrong Format.";
		}
		return null;
	}
	
	private BufferedImage[] sliceSpriteSheet(BufferedImage Image, int width, int height) {
		int hei = Image.getHeight() / height, wid = Image.getWidth() / width;
		int size = hei * wid;
		BufferedImage[] assets = new BufferedImage[size];
		System.out.println(size);
		for (int i=0;i<hei;i++) {
			for (int j=0;j<wid;j++) {
				assets[i*wid+j] = Image.getSubimage(j*width, i*height, width, height);
			}
		}
		return assets;
	}
	
	public BufferedImage getMapAsset(int index) {
		assert _map_assets[0] != null && index >= 0 && index < _map_assets[0].length : "Invalid Index.";
		return _map_assets[0][index];
	}
	
	public int getMapWidth() {
		assert _map_assets[0][0] != null : "Map Assets is Null";
		return _map_assets[0][0].getWidth();
	}
	
	public int getMapHeight() {
		assert _map_assets[0][0] != null : "Map Assets is Null";
		return _map_assets[0][0].getHeight();
	}
	
	public BufferedImage getPlayerAsset(int index, int i) {
		assert index >= 0 && index < _player_assets.length : "Invalid Index.";
		assert i >= 0 && i < _player_assets[i].length : "Invalid Index.";
		return _player_assets[index][i];
	}
	
	public BufferedImage getMonsterAsset(int index, int i) {
		assert index >= 0 && index < _monster_assets.length : "Invalid Index.";
		assert i >= 0 && i < _monster_assets[i].length : "Invalid Index.";
		return _monster_assets[index][i];
	}
	
	public BufferedImage getProjectorAsset(int index, int i) {
		assert index >= 0 && index < _projector_assets.length : "Invalid Index.";
		assert i >= 0 && i < _projector_assets[i].length : "Invalid Index.";
		return _projector_assets[index][i];
	}
	
	public BufferedImage getItemAsset(int index, int i) {
		assert index >= 0 && index < _item_assets.length : "Invalid Index.";
		assert i >= 0 && i < _item_assets[i].length : "Invalid Index.";
		return _item_assets[index][i];
	}
}

@@ -4252,39 +4252,32 @@
 mAsset(int index
-, int i
 ) %7B%0A%09%09assert ind
@@ -4277,80 +4277,40 @@
 ert 
-index %3E= 0 && index %3C _item_assets.length : %22Invalid Index.%22;%0A%09%09assert i
+_item_assets%5B0%5D != null && index
  %3E= 
@@ -4311,24 +4311,28 @@
 ex %3E= 0 && i
+ndex
  %3C _item_ass
@@ -4327,33 +4327,33 @@
  %3C _item_assets%5B
-i
+0
 %5D.length : %22Inva
@@ -4383,28 +4383,28 @@
 item_assets%5B
+0%5D%5B
 index%5D
-%5Bi%5D
 ;%0A%09%7D%0A%7D%0A

Modify ADM's getItemAsset( )