using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEngine.SceneManagement;

public class DoorTests{
	
	[OneTimeSetUp]
    public void OneTimeSetup()
    {
        SceneManager.LoadScene("Scenes/Demo/Demo_Start", LoadSceneMode.Single); // Selects scene for test
    }
	
    [UnityTest]
    public IEnumerator LeftDoorIsWorking(){
		GameObject MapGen = GameObject.FindWithTag("Map");
		GameObject leftDoor = GameObject.FindWithTag("LeftDoor");
		MapGeneration Script = MapGen.GetComponent();
		int rows = Script.grid.GetLength(0); // Get the number of rows
		int cols = Script.grid.GetLength(1); // Get the number of columns
		cell Location;
		Location.x = 0;
		Location.y = 0;
		Location.gridEdge = 0;
		for(int i = 0; i < rows; i++){
			for(int j = 0; j < cols; j++){
				if(Script.grid[i, j].roomName == SceneManager.GetActiveScene().name){
					Location = Script.grid[i, j];
				}
			}
		}
		if(Location.gridEdge != 1 && Location.gridEdge != 5 && Location.gridEdge != 8){
			if(Script.grid[Location.y, Location.x - 1].generated == 1){	
				Assert.IsTrue(leftDoor.activeSelf);
			}else{
				Assert.IsNull(leftDoor);
			}
		}else{
			Assert.IsNull(leftDoor);
		}
		return null;
    }
	
	[UnityTest]
    public IEnumerator TopDoorIsWorking(){
		GameObject MapGen = GameObject.FindWithTag("Map");
		GameObject topDoor = GameObject.FindWithTag("TopDoor");
		MapGeneration Script = MapGen.GetComponent();
		int rows = Script.grid.GetLength(0); // Get the number of rows
		int cols = Script.grid.GetLength(1); // Get the number of columns
		cell Location;
		Location.x = 0;
		Location.y = 0;
		Location.gridEdge = 0;
		for(int i = 0; i < rows; i++){
			for(int j = 0; j < cols; j++){
				if(Script.grid[i, j].roomName == SceneManager.GetActiveScene().name){
					Location = Script.grid[i, j];
				}
			}
		}
		if(Location.gridEdge != 2 && Location.gridEdge != 5 && Location.gridEdge != 6){
			if(Script.grid[Location.y - 1, Location.x].generated == 1){	
				Assert.IsTrue(topDoor.activeSelf);
			}else{
				Assert.IsNull(topDoor);
			}
		}else{
			Assert.IsNull(topDoor);
		}
		return null;
    }
	
	[UnityTest]
    public IEnumerator RightDoorIsWorking(){
		GameObject MapGen = GameObject.FindWithTag("Map");
		GameObject rightDoor = GameObject.FindWithTag("RightDoor");
		MapGeneration Script = MapGen.GetComponent();
		int rows = Script.grid.GetLength(0); // Get the number of rows
		int cols = Script.grid.GetLength(1); // Get the number of columns
		cell Location;
		Location.x = 0;
		Location.y = 0;
		Location.gridEdge = 0;
		for(int i = 0; i < rows; i++){
			for(int j = 0; j < cols; j++){
				if(Script.grid[i, j].roomName == SceneManager.GetActiveScene().name){
					Location = Script.grid[i, j];
				}
			}
		}
		if(Location.gridEdge != 3 && Location.gridEdge != 6 && Location.gridEdge != 7){
			if(Script.grid[Location.y, Location.x + 1].generated == 1){	
				Assert.IsTrue(rightDoor.activeSelf);
			}else{
				Assert.IsNull(rightDoor);
			}
		}else{
			Assert.IsNull(rightDoor);
		}
		return null;
    }
	
	[UnityTest]
    public IEnumerator BottomDoorIsWorking(){
		GameObject MapGen = GameObject.FindWithTag("Map");
		GameObject bottomDoor = GameObject.FindWithTag("BottomDoor");
		MapGeneration Script = MapGen.GetComponent();
		int rows = Script.grid.GetLength(0); // Get the number of rows
		int cols = Script.grid.GetLength(1); // Get the number of columns
		cell Location;
		Location.x = 0;
		Location.y = 0;
		Location.gridEdge = 0;
		for(int i = 0; i < rows; i++){
			for(int j = 0; j < cols; j++){
				if(Script.grid[i, j].roomName == SceneManager.GetActiveScene().name){
					Location = Script.grid[i, j];
				}
			}
		}
		if(Location.gridEdge != 4 && Location.gridEdge != 7 && Location.gridEdge != 8){
			if(Script.grid[Location.y + 1, Location.x].generated == 1){	
				Assert.IsTrue(bottomDoor.activeSelf);
			}else{
				Assert.IsNull(bottomDoor);
			}
		}else{
			Assert.IsNull(bottomDoor);
		}
		return null;
    }
	
	[UnityTest]
	public IEnumerator BottomDoorFoundGrombo(){
		GameObject bottomDoor = GameObject.FindWithTag("BottomDoor");
		if(GameObject.FindWithTag("BottomDoor")){
			DoorScriptB Script = bottomDoor.GetComponent();
			Assert.IsTrue(Script.Grombo);
		}else{
			Assert.IsFalse(GameObject.FindWithTag("BottomDoor"));
		}
		return null;
	}
	
	[UnityTest]
	public IEnumerator LeftDoorFoundGrombo(){
		GameObject leftDoor = GameObject.FindWithTag("LeftDoor");
		if(GameObject.FindWithTag("LeftDoor")){
			DoorScriptL Script = leftDoor.GetComponent();
			Assert.IsTrue(Script.Grombo);
		}else{
			Assert.IsFalse(GameObject.FindWithTag("LeftDoor"));
		}
		return null;
	}
	
	[UnityTest]
	public IEnumerator RightDoorFoundGrombo(){
		GameObject rightDoor = GameObject.FindWithTag("RightDoor");
		if(GameObject.FindWithTag("RightDoor")){
			DoorScriptR Script = rightDoor.GetComponent();
			Assert.IsTrue(Script.Grombo);
		}else{
			Assert.IsFalse(GameObject.FindWithTag("RightDoor"));
		}
		return null;
	}
	
	[UnityTest]
	public IEnumerator TopDoorFoundGrombo(){
		GameObject topDoor = GameObject.FindWithTag("TopDoor");
		if(GameObject.FindWithTag("TopDoor")){
			DoorScriptT Script = topDoor.GetComponent();
			Assert.IsTrue(Script.Grombo);
		}else{
			Assert.IsFalse(GameObject.FindWithTag("TopDoor"));
		}
		return null;
	}
}