using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//<summary>
//Basic class for maze generation logic
//</summary>
public abstract class BasicMazeGenerator {
	public int RowCount{ get{ return mMazeRows; } }
	public int ColumnCount { get { return mMazeColumns; } }

	private int mMazeRows;
	private int mMazeColumns;
	private MazeCell[,] mMaze;

    protected List<MazeCell> possibleLootCells = new List<MazeCell>(); 

	public BasicMazeGenerator(int rows, int columns){
		mMazeRows = Mathf.Abs(rows);
		mMazeColumns = Mathf.Abs(columns);
		if (mMazeRows == 0) {
			mMazeRows = 1;
		}
		if (mMazeColumns == 0) {
			mMazeColumns = 1;
		}
		mMaze = new MazeCell[rows,columns];
		for (int row = 0; row < rows; row++) {
			for(int column = 0; column < columns; column++){
				mMaze[row,column] = new MazeCell(column, row);
			}
		}
	}

	public abstract void GenerateMaze();

    public void AssignObjectPositions()
    {
        if (possibleLootCells.Count == 0) return;
        List<int> exc = new List<int> { -1, -1, -1, -1 };
        
        for (int i = 0; i < 4; i++)
        {
            int index = -1;
            while (exc.Contains(index))
                index = Random.Range(0, possibleLootCells.Count - 1);
            exc[i] = index;
        }
        var scan = Random.Range(0, possibleLootCells.Count - 1);

        possibleLootCells[exc[0]].loot = Loot.Type.Sword;
        possibleLootCells[exc[1]].loot = Loot.Type.Sword;
        possibleLootCells[exc[2]].loot = Loot.Type.Shield;
        possibleLootCells[exc[3]].loot = Loot.Type.Shield;

        for (int i = 0; i < possibleLootCells.Count; i++)
        {
            if (possibleLootCells[i].loot == Loot.Type.None)
                possibleLootCells[i].loot = Loot.Type.Coin;
        }
        
    }

	public MazeCell GetMazeCell(int row, int column){
		if (row >= 0 && column >= 0 && row < mMazeRows && column < mMazeColumns) {
			return mMaze[row,column];
		}else{
			Debug.Log(row+" "+column);
			throw new System.ArgumentOutOfRangeException();
		}
	}

	protected void SetMazeCell(int row, int column, MazeCell cell){
		if (row >= 0 && column >= 0 && row < mMazeRows && column < mMazeColumns) {
			mMaze[row,column] = cell;
		}else{
			throw new System.ArgumentOutOfRangeException();
		}
	}
}
