#include "maps.h"

Maps::Maps(SDL_Renderer* render){
    //Set renderer
    renderer = render;

    //set the window's (renderer) dimensions
    SDL_GetRendererOutputSize(renderer,&screenWidth,&screenHeight);

    //Set unit size
	sz = screenWidth/16;

	//Set keyboard input
	currentKeyStates = SDL_GetKeyboardState(NULL);
};

int Maps::map1(){
    //Set the map dimensions
    int mapWidth = 36*sz;
    int mapHeight = screenHeight;

    //Initialize the player, set its position, pass the map's size and set the renderer
	Player player(40,8*sz,mapHeight,renderer);

	//Set the camera and pass the map and screen's dimensions
	Camera camera(mapWidth,mapHeight,screenWidth,screenHeight);

	//Initialize the block class and set the player and the renderer
	Block ground(renderer,&player);

	//Set the quantity and size of the blocks
	int blockRect[5][4];
    setRectSize(blockRect[0], 8*sz, 7*sz, 2*sz,2*sz);
    setRectSize(blockRect[1], 4*sz, 9*sz,   sz,2*sz);
    setRectSize(blockRect[2], 6*sz, 9*sz,   sz,2*sz);
    setRectSize(blockRect[3], 0   ,11*sz,16*sz,  sz);
    setRectSize(blockRect[4],20*sz,11*sz,16*sz,  sz);

    //Initialize the powerup, set his position, pass the renderer and the player
    Powerup powerup(13*sz,8*sz,renderer,&player);

	bool quit = false;
	SDL_Event e;

	//Game loop
	while(quit == false){
		while(SDL_PollEvent(&e)!=0){
			if(e.type == SDL_QUIT){
				quit = true;
			}
		}
		if(currentKeyStates[SDL_SCANCODE_ESCAPE]) return 0;

		//Clear the render and set the background color
		SDL_SetRenderDrawColor(renderer,0,0,100,0xFF);
		SDL_RenderClear(renderer);

		//Update the camera position
		camera.update(player.getRectangle().x,player.getRectangle().y);

        //Print player
		if(player.print(camera.getPosX()) == 1) map1();

		//Print the blocks the corresponding dimensions and positions and check collisions
        for(int i = 0; i < 5; i++) ground.printAndCheck(blockRect[i],camera.getPosX());

        //Print the poweup and check collisions
		powerup.printAndCheck(camera.getPosX());

		//Render
		SDL_RenderPresent(renderer);
	}
    return -1;
};

int Maps::map2(){
    int mapWidth = screenWidth;
    int mapHeight = screenHeight;

	Player player(40,8*sz,mapHeight,renderer);
	Camera camera(mapWidth,mapHeight,screenWidth,screenHeight);
	Block ground(renderer,&player);
    Powerup powerup(13*sz,8*sz,renderer,&player);

	int blockRect[4];
    setRectSize(blockRect, 0   ,11*sz,16*sz,  sz);

	bool quit = false;
	SDL_Event e;

	while(quit == false){
		while(SDL_PollEvent(&e)!=0){
			if(e.type == SDL_QUIT){
				quit = true;
			}
		}
		if(currentKeyStates[SDL_SCANCODE_ESCAPE]) return 0;

		SDL_SetRenderDrawColor(renderer,0,0,100,0xFF);
		SDL_RenderClear(renderer);

		camera.update(player.getRectangle().x,player.getRectangle().y);

		if(player.print(camera.getPosX()) == 1) map2();
        ground.printAndCheck(blockRect,camera.getPosX());
		powerup.printAndCheck(camera.getPosX());

		SDL_RenderPresent(renderer);
	}
    return -1;
};

void Maps::setRectSize(int rect[],int x, int y, int w, int h){
    rect[0] = x;
    rect[1] = y;
    rect[2] = w;
    rect[3] = h;
}
