/*
 * KeyboardController.h
 *
 *  Created on: Mar 1, 2020
 *      Author: ayoungblood
 */

#ifndef SRC_ECS_KEYBOARDCONTROLLER_H_
#define SRC_ECS_KEYBOARDCONTROLLER_H_

#include "../game/Game.hpp"
#include  "ECS.h"
#include "Components.h"
#include "../assetmgr/AssetManager.h"

class KeyboardController : public Component
{
public:
	TransformComponent *transform;
	SpriteComponent *sprite;

	Game *game;

	void init() override
	{
		transform = &entity->getComponent<TransformComponent>();
		sprite = &entity->getComponent<SpriteComponent>();
	}

	void update() override
	{
		if (Game::event.type == SDL_KEYDOWN)
		{
			switch (Game::event.key.keysym.sym)
			{
			case SDLK_UP:
				break;
			case SDLK_DOWN:
				break;
			case SDLK_LEFT:
				transform->velocity.x = -1;
                if(Game::playerIsGrounded){
				sprite->Play("Walk");
                }
                sprite->spriteFlip = SDL_FLIP_NONE;
				break;
			case SDLK_RIGHT:
				transform->velocity.x = 1;
                if(Game::playerIsGrounded){
				sprite->Play("Walk");
                }
				sprite->spriteFlip = SDL_FLIP_HORIZONTAL;
				break;
			case SDLK_k:
//                 game->printDebug("");
				break;
			case SDLK_j:
				if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0) == 0)
				{
					Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0);
				}
				Game::gravityOnPlayer = true;
                sprite->Play("Jump");
                transform->velocity.y = -2;
				break;
			default:
				break;
			}
		}

		if (Game::event.type == SDL_KEYUP)
		{
			switch (Game::event.key.keysym.sym)
			{
			case SDLK_UP:
				transform->velocity.y = 0;
// 				sprite->Play("idle");
// 				sprite->spriteFlip = SDL_FLIP_NONE;
				break;
			case SDLK_DOWN:
				transform->velocity.y = 0;
// 				sprite->Play("idle");
// 				sprite->spriteFlip = SDL_FLIP_NONE;
				break;
			case SDLK_LEFT:
				transform->velocity.x = 0;
				sprite->Play("Idle");
                if (!Game::gravityOnPlayer){
                    Game::gravityOnPlayer = true;
                    sprite->Play("Fall");
                }
				break;
			case SDLK_RIGHT:
				transform->velocity.x = 0;
				sprite->Play("Idle");
                if (!Game::gravityOnPlayer){
                    Game::gravityOnPlayer = true;
                }
				break;
			case SDLK_k:
				if (Game::debugCollisionBoxes)
				{ Game::debugCollisionBoxes = false; }
				else
				{Game::debugCollisionBoxes = true; }
				break;
            case SDLK_j:
                transform->velocity.y = 0;
                sprite->Play("Fall");
                Game::gravityOnPlayer = true;
                break;
			case SDLK_ESCAPE: // exit the game when Escape pressed
				Game::isRunning = false;
				break;
			default:
				break;
			}
		}
	}

};


#endif /* SRC_ECS_KEYBOARDCONTROLLER_H_ */
