using UnityEngine;
using System.Collections;

public class DrawMap : MonoBehaviour {
    public GameObject Tile;
    public int width;
    public int height;
    public float z;
    float spacingX;
    float spacingY;
    float TileWidth;
    float TileHeight;
    public GameObject HorizontalWall;
    public GameObject HorizontalWallDoor;
    public GameObject VerticalWallRight;
    public GameObject VerticalWallLeft;
    public int hallwayWidth;
    public int hallwayHeight;

    SpriteRenderer TileSprite;
	// Use this for initialization
	void Start () {
        TileSprite = Tile.GetComponent<SpriteRenderer>();
        TileWidth = TileSprite.bounds.size.x;
        TileHeight = TileSprite.bounds.size.y;
        spacingX = TileHeight/2;
        spacingY = TileWidth/2;
        Generate();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    /*
     * Generates a height·width grid of Tile, right next to each other, at z depth.
     */
    public void Generate()
    {
        GenerateRoom();
        GenerateHallway();
        
    }

    public void GenerateRoom()
    {
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                //TileSprite.color = new Vector4(Random.value, Random.value, Random.value, 1);
                if (i == width - 1)
                {

                    if (j == 0)
                    {
                        GameObject wall2 = (GameObject)Instantiate(VerticalWallLeft, new Vector3(j * TileHeight + spacingX, i * TileWidth + spacingY, z), Quaternion.Euler(new Vector3(0, 0, 0)));
                        wall2.transform.SetParent(this.transform);
                    }
                    else if (j == height - 1)
                    {
                        GameObject wall2 = (GameObject)Instantiate(VerticalWallRight, new Vector3(j * TileHeight + spacingX, i * TileWidth + spacingY, z), Quaternion.Euler(new Vector3(0, 0, 0)));
                        wall2.transform.SetParent(this.transform);
                    }


                    GameObject wall = (GameObject)Instantiate(HorizontalWall, new Vector3(j * TileHeight + spacingX, i * TileWidth + spacingY, z), Quaternion.Euler(new Vector3(0, 0, 0)));
                    wall.transform.SetParent(this.transform);


                }
                else if (i == 0)
                {
                    if ((j >= (height / 2) - hallwayWidth / 2) && (j <= (height / 2) + hallwayWidth / 2 - 1))
                    {
                        GameObject wallDoor = (GameObject)Instantiate(HorizontalWallDoor, new Vector3(j * TileHeight + spacingX, i * TileWidth + spacingY, z), Quaternion.Euler(new Vector3(0, 0, 0)));
                        wallDoor.transform.SetParent(this.transform);
                    }
                    else
                    {
                        GameObject wall = (GameObject)Instantiate(HorizontalWall, new Vector3(j * TileHeight + spacingX, i * TileWidth + spacingY, z), Quaternion.Euler(new Vector3(0, 0, 0)));
                        wall.transform.SetParent(this.transform);
                    }
                }
                else if (j == 0)
                {
                    GameObject wall = (GameObject)Instantiate(VerticalWallLeft, new Vector3(j * TileHeight + spacingX, i * TileWidth + spacingY, z), Quaternion.Euler(new Vector3(0, 0, 0)));
                    wall.transform.SetParent(this.transform);
                }
                else if (j == height - 1)
                {
                    GameObject wall = (GameObject)Instantiate(VerticalWallRight, new Vector3(j * TileHeight + spacingX, i * TileWidth + spacingY, z), Quaternion.Euler(new Vector3(0, 0, 0)));
                    wall.transform.SetParent(this.transform);
                }


                GameObject tile = (GameObject)Instantiate(Tile, new Vector3(j * TileHeight + spacingX, i * TileWidth + spacingY, z), Quaternion.Euler(new Vector3(0, 0, 0)));
                tile.transform.SetParent(this.transform);

            }
        }
    }
    public void GenerateHallway()
    {
        for (int j = (height / 2) - hallwayWidth / 2; j < (height / 2) + hallwayWidth / 2; j++)
        {
            for (int i = 0; i < hallwayHeight; i++)
            {
                if (j == (height / 2) - hallwayWidth / 2)
                {
                    GameObject wall = (GameObject)Instantiate(VerticalWallLeft, new Vector3(j * TileHeight + spacingX, -(i * TileWidth + spacingY) + TileWidth, z - 1), Quaternion.Euler(new Vector3(0, 0, 0)));
                    wall.transform.SetParent(this.transform);
                }
                else if (j == (height / 2) + hallwayWidth / 2 - 1)
                {
                    GameObject wall = (GameObject)Instantiate(VerticalWallRight, new Vector3(j * TileHeight + spacingX, -(i * TileWidth + spacingY) + TileWidth, z - 1), Quaternion.Euler(new Vector3(0, 0, 0)));
                    wall.transform.SetParent(this.transform);
                }
                GameObject tile = (GameObject)Instantiate(Tile, new Vector3(j * TileHeight + spacingX, -(i * TileWidth + spacingY), z), Quaternion.Euler(new Vector3(0, 0, 0)));
                tile.transform.SetParent(this.transform);
            }
        }
    }
}       
