#include "City.h"
#include "Human.h"
#include "Zombie.h"
#include "Organism.h"
#include <Windows.h>
using namespace std;



City::City()
{
	//creates grid and fills with NULL
	for (int i = 0; i < GRID_HEIGHT; i++){

		for (int j = 0; j < GRID_WIDTH; j++) {

			grid[j][i] = NULL;
		}
	}
}


City::~City() {}

//Getters and Setters
Organism *City::getOrganism(int x, int y) { 
	
	return this->grid[x][y]; 

}
void City::setOrganism(Organism *organism, int x, int y) {
	
	grid[x][y] = organism; 

}



void City::move()
{
	//sets everyything to FALSE
	for (int i = 0; i < GRID_HEIGHT; i++){

		for (int j = 0; j < GRID_WIDTH; j++){

			if (grid[j][i] != NULL){

				grid[j][i]->setHasMoved(false);
				grid[j][i]->setHasSpawned(false);
			}
		}
	}


	 //This sets organisms move
	for (int i = 0; i < GRID_HEIGHT; i++){

		for (int j = 0; j < GRID_WIDTH; j++){

			if (grid[j][i] != NULL){

				if (grid[j][i]->getHasMoved() == false) {

					grid[j][i]->move();
				}
			}
		}
	}

	 //This sets organism to spawn
	for (int i = 0; i < GRID_HEIGHT; i++){

		for (int j = 0; j < GRID_WIDTH; j++){

			if (grid[j][i] != NULL){

				if (grid[j][i]->getHasSpawned() == false) {

					grid[j][i]->spawn();
				}
			}
		}
	}
}


 //gets the number of humans
int City::numberOfHumans()
{
	int humans = 0;
	for (int i = 0; i<GRID_HEIGHT; i++){

		for (int j = 0; j<GRID_WIDTH; j++){

			if (dynamic_cast<Human*>(grid[j][i]) != NULL) {

				humans++;
			}
		}
	}

	return humans;
}
//get the number of zombies
int City::numberOfZombies()
{
	int zombies = 0;
	for (int i = 0; i<GRID_HEIGHT; i++){

		for (int j = 0; j<GRID_WIDTH; j++){

			if (dynamic_cast<Zombie*>(grid[j][i]) != NULL) {
				zombies++;
			}
		}
	}
	return zombies;
}

 
ostream& operator<<(ostream &output, City &city)
{
	HANDLE  color = GetStdHandle(STD_OUTPUT_HANDLE);
	//keeps the city at the top of the console
	SetConsoleCursorPosition(color, { 0, 0 }); 

	SetConsoleTextAttribute(color, 7);
	cout << "~~~~~~~~~~~~";
	SetConsoleTextAttribute(color, 2);
	cout << "Humans";
	SetConsoleTextAttribute(color, 7);
	cout << " Vs ";
	SetConsoleTextAttribute(color, 4);
	cout << "Zombies";
	SetConsoleTextAttribute(color, 7);
	cout << "~~~~~~~~~~~~" << endl << endl;
	
	//Draw the City
	
	for (int i = 0; i<GRID_HEIGHT; i++){
		
		//output << "|";

		for (int j = 0; j < GRID_WIDTH; j++) {
			
			output << city.grid[j][i] ; /*"|";*/
			
		}
		output << endl;
	}



	return output;
}