﻿using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour {
	const int MaxX = 80;
	const int MaxY = 80;
	public GameObject[] Rooms;
	public int depth = 1;
	private bool[,] not_available = new bool[MaxX, MaxY];

/*	public class Room{
		public RoomType type;
		public int depth;
		public int centerx, centery, xsize, ysize;

		public Room(int d, RoomType t){
			depth = d;
			type = t;
			setRoomSize();
			do{
				centerx = Random.Range(0, MaxX-xsize); // bottom left corner of room
				centery = Random.Range(0, MaxY-ysize);
			} while(!RoomOk(centerx, centery, xsize, ysize));
		}
		public Room(int d, RoomType t, int x, int y){
			depth = d;
			type = t;
			centerx = x;
			centery = y;
			setRoomSize();
		}
		// Creates a room
		public bool createRoom() {
			buildRoom(centerx, centery, xsize, ysize); // blocks off room in 2d array
			instantiater.Instantiate(this); // creates room object

			// create depth monsters in room

			return true;
		}
		private void buildRoom(int centerx, int centery, int xsize, int ysize){
			for(int i=0; i<xsize; i++){
				for(int j=0; j<ysize; j++){
					squares[centerx+i,centery+j] = true;
				}
			}
		}
		private void setRoomSize(){
			xsize = Random.Range(2,7);
			ysize = Random.Range(2,7);
		}
	};
	*/

	// Use this for initialization
	void Start () {
		GenerateLevel();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	// Todo other params for level
	void GenerateLevel(){
		// Generate n rooms
		int num_rooms = Random.Range(7,30);

		// Place Rooms so not on top
		for(int num_placed = 0; num_placed < num_rooms; num_placed++){
			CreateRoom(Room.RoomType.DUNGEON);
		}
	}

	private void CreateRoom(Room.RoomType type, int x, int y){
	}

	private void CreateRoom(Room.RoomType type){
		// Try to create a room
		int xsize = Random.Range(4,10);
		int ysize = Random.Range(4,10);
		int centery, centerx = -1;

		// Get a Room that fits
		do{
			centerx = Random.Range(0, MaxX-xsize); // bottom left corner of room
			centery = Random.Range(0, MaxY-ysize);
		} while(!RoomOk(centerx, centery, xsize, ysize));

		BuildRoom(centerx, centery, xsize, ysize);
		InstantiateRoom(Rooms[0], centerx, centery, xsize, ysize); // Todo other room types
		PlaceStairs();
	}

	private bool RoomOk(int centerx, int centery, int xsize, int ysize){		
		for(int i=0; i<xsize; i++){
			for(int j=0; j<ysize; j++){
				if(not_available[centerx+i,centery+j]){
					return false;
				}
			}
		}
		return true;
	}

	// Marks squares as not available
	private void BuildRoom(int centerx, int centery, int xsize, int ysize){
		for(int i=0; i<xsize; i++){
			for(int j=0; j<ysize; j++){
				not_available[centerx+i,centery+j] = true;
			}
		}
	}

	private void InstantiateRoom(GameObject room, int centerx, int centery, int xsize, int ysize){
		Quaternion rotation = new Quaternion ();
		GameObject s = (GameObject)Instantiate( room, new Vector3(centerx+xsize/2, centery+ysize/2, 0), rotation);
		s.transform.localScale = new Vector3(s.transform.localScale.x * xsize, s.transform.localScale.y * ysize, s.transform.localScale.z);
	}

	private void PlaceStairs(){
			
	}

	// Todo, maybe make this based on movement type, but for now simple.
	public bool SpaceMoveable(int x, int y){
		return not_available[x,y];
	}

	public bool SpaceMoveable(Vector3 pos){
		return SpaceMoveable((int)pos.x, (int)pos.y);
	}
}



