using UnityEngine;
using System.Collections;
using System.IO;

public class MapMaker : MonoBehaviour {

	public string 				pathFile = "world.map";
	public int					originX = 0;
	public int					originY = 0;
	public int					limitDimX = 35;
	public int					limitDimY = 21;
	public GameObject	prefabWall;
	public GameObject	prefabFloor;
	public GameObject	prefabIntersection;
	public GameObject	prefabPassageZiggurat;
	public GameObject	prefabCentreZiggurat;
	public GameObject	prefabUnvisibleWallZiggurat;
	public GameObject	prefabUnvisibleWallSpawn;
	public GameObject	prefabUnvisibleFloorSpawn;
	public GameObject	prefabSpawnMonster;
	public GameObject	referee;
	
	private int 					dimX = 0;
	private int 					dimY = 0;
	private string 			stringMap;

	void Start(){
		ReadFile();
		GenerateMap();
		referee.GetComponent<PowerUpSpawner>().Init();
	}
	
	void ReadFile(){
		System.IO.StreamReader myFile = new System.IO.StreamReader(pathFile);
		stringMap = myFile.ReadToEnd();
		myFile.Close();
	}
	
	void GenerateMap(){
		GameObject newObject;
		Vector3 newPos = new Vector3();
		foreach (char caract in stringMap){
			newPos.x = originX + dimX;
			newPos.y = originY + dimY;
			switch (caract){
				case 'w' :
					newObject = Instantiate(prefabWall, newPos, Quaternion.identity) as GameObject;
					newObject.transform.name = "Wall " + dimX.ToString() + " " + dimY.ToString();
					break;
				case 'f' :
					newObject = Instantiate(prefabFloor, newPos, Quaternion.identity) as GameObject;
					newObject.transform.name = "Floor " + dimX.ToString() + " " + dimY.ToString();
					break;
				case 'd' :
					newObject = Instantiate(prefabIntersection, newPos, Quaternion.identity) as GameObject;
					newObject.transform.name = "Intersection " + dimX.ToString() + " " + dimY.ToString();
					break;
				case 'e' :
					newObject = Instantiate(prefabPassageZiggurat, newPos, Quaternion.identity) as GameObject;
					newObject.transform.name = "PassageZiggurat " + dimX.ToString() + " " + dimY.ToString();
					break;
				case 'r' :
					newObject = Instantiate(prefabCentreZiggurat, newPos, Quaternion.identity) as GameObject;
					newObject.transform.name = "CentreZiggurat " + dimX.ToString() + " " + dimY.ToString();
					break;
				case 'z' :
					newObject = Instantiate(prefabUnvisibleWallZiggurat, newPos, Quaternion.identity) as GameObject;
					newObject.transform.name = "UnvisibleWallZiggurat " + dimX.ToString() + " " + dimY.ToString();
					break;
				case 'o' :
					newObject = Instantiate(prefabUnvisibleWallSpawn, newPos, Quaternion.identity) as GameObject;
					newObject.transform.name = "UnvisibleWallSpawn " + dimX.ToString() + " " + dimY.ToString();
					break;
				case 'p' :
					newObject = Instantiate(prefabUnvisibleFloorSpawn, newPos, Quaternion.identity) as GameObject;
					newObject.transform.name = "UnvisibleFloorSpawn " + dimX.ToString() + " " + dimY.ToString();
					break;
				case 'm' :
					newObject = Instantiate(prefabSpawnMonster, newPos, Quaternion.identity) as GameObject;
					newObject.transform.name = "SpawnMonster " + dimX.ToString() + " " + dimY.ToString();
					break;
			}
			++dimX;
			if (caract == '\n'){
				dimX = 0;
				++dimY;
			}
		}
	}
}
