using UnityEngine;
using System.Collections;

//<summary>
//Game object, that creates maze and instantiates it in scene
//</summary>
public class MazeSpawner : MonoBehaviour {
    public static MazeSpawner instance;
	public bool FullRandom = false;
	public int RandomSeed = 12345;
    public GameObject Floor = null;
    public GameObject Roof = null;
    public GameObject Wall = null;
	public GameObject Pillar = null;
    [HideInInspector]
	public int Rows = 10;
    [HideInInspector]
	public int Columns = 10;
    [HideInInspector]
	public readonly float CellSize = 4;
	public bool AddGaps = true;
    public GameObject sword;
    public GameObject shield;
    public GameObject coin;

	private BasicMazeGenerator mMazeGenerator = null;

    public MazeCell GetMazeCell(int row, int column)
    {
        if (mMazeGenerator != null)
            return mMazeGenerator.GetMazeCell(row, column);
        return null;
    }

	void Awake () {
        instance = this;
		if (!FullRandom) {
			Random.InitState(RandomSeed);
		}

        Columns = PlayerPrefs.GetInt("MazeWidth", 10);
        Rows = PlayerPrefs.GetInt("MazeLength", 10);

		mMazeGenerator = new RecursiveTreeMazeGenerator (Rows, Columns);

		mMazeGenerator.GenerateMaze ();
		for (int row = 0; row < Rows; row++) {
			for(int column = 0; column < Columns; column++){
				float x = column*(CellSize+(AddGaps? 0.2f:0));
				float z = row*(CellSize+(AddGaps? 0.2f:0));
				MazeCell cell = GetMazeCell(row,column);
				GameObject tmp;
                tmp = Instantiate(Floor, new Vector3(x, 0, z), Quaternion.Euler(0, 0, 0)) as GameObject;
                tmp.transform.parent = transform;
                if (Roof != null)
                {
                    tmp = Instantiate(Roof, new Vector3(x, 4, z), Quaternion.Euler(0, 0, 0)) as GameObject;
                    tmp.transform.parent = transform;
                }
                if (cell.WallRight){
					tmp = Instantiate(Wall,new Vector3(x+CellSize/2,0,z)/*+Wall.transform.position*/,Quaternion.Euler(0,90,0)) as GameObject;// right
					tmp.transform.parent = transform;
				}
                else
                    cell.neighbours.Add(mMazeGenerator.GetMazeCell(row, column+1));

                if (cell.WallFront){
					tmp = Instantiate(Wall,new Vector3(x,0,z+CellSize/2)/*+Wall.transform.position*/,Quaternion.Euler(0,0,0)) as GameObject;// front
					tmp.transform.parent = transform;
				}
                else
                    cell.neighbours.Add(mMazeGenerator.GetMazeCell(row+1, column));

                if (cell.WallLeft){
					tmp = Instantiate(Wall,new Vector3(x-CellSize/2,0,z)/*+Wall.transform.position*/,Quaternion.Euler(0,270,0)) as GameObject;// left
					tmp.transform.parent = transform;
				}
                else
                    cell.neighbours.Add(mMazeGenerator.GetMazeCell(row, column-1));

                if (cell.WallBack){
					tmp = Instantiate(Wall,new Vector3(x,0,z-CellSize/2)/*+Wall.transform.position*/,Quaternion.Euler(0,180,0)) as GameObject;// back
					tmp.transform.parent = transform;
				}
                else
                    cell.neighbours.Add(mMazeGenerator.GetMazeCell(row-1, column));

                if (cell.loot != Loot.Type.None){
                    switch (cell.loot)
                    {
                        case Loot.Type.Sword:
                            tmp = Instantiate(sword, new Vector3(x, 1, z), Quaternion.Euler(0, 0, 0)) as GameObject;
                            break;
                        case Loot.Type.Shield:
                            tmp = Instantiate(shield, new Vector3(x, 1, z), Quaternion.Euler(0, 0, 0)) as GameObject;
                            break;
                        default:
                            tmp = Instantiate(coin, new Vector3(x, 1, z), Quaternion.Euler(0, 0, 0)) as GameObject;
                            break;
                    }
                    tmp.transform.parent = transform;
				}
			}
		}
		if(Pillar != null){
			for (int row = 0; row < Rows+1; row++) {
				for (int column = 0; column < Columns+1; column++) {
					float x = column*(CellSize+(AddGaps ? 0.2f:0));
					float z = row*(CellSize+(AddGaps ? 0.2f:0));
					GameObject tmp = Instantiate(Pillar,new Vector3(x-CellSize/2,0,z-CellSize/2),Quaternion.identity) as GameObject;
					tmp.transform.parent = transform;
				}
			}
		}
	}
}
