/*
 * 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 "PlayerController.h"
#include "../assetmgr/AssetManager.h"
#include "ColliderComponent.h"
// #include "../game/Vector2D.h"

class KeyboardController : public Component
{
public:

    TransformComponent *transform;
	SpriteComponent *sprite;
    PlayerController *playerCtrl;
    ColliderComponent *collider;
    
	Game *game;

	void init() override
	{
		transform = &entity->getComponent<TransformComponent>();
		sprite = &entity->getComponent<SpriteComponent>();
//         collider = &entity->getComponent<ColliderComponent>();
//         playerCtrl = &entity->getComponent<PlayerController>();
	}

	void update() override
	{
		if (Game::event.type == SDL_KEYDOWN)
		{
			switch (Game::event.key.keysym.sym)
			{
            case SDLK_UP:
                if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
                    if(transform->position.y>0){
                    transform->velocity.y = -1;
//                     if(Game::playerIsGrounded){
                        sprite->Play("Walk");
//                     }
                    sprite->spriteFlip = SDL_FLIP_NONE;
                    }
                }
				break;
            case SDLK_DOWN:
                if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
                    if(transform->position.y<Game::levelMap.h){
                    transform->velocity.y = 1;
//                     if(Game::playerIsGrounded){
                        sprite->Play("Walk");
//                     }
                    sprite->spriteFlip = SDL_FLIP_NONE;
                    }
                }
				break;
            case SDLK_LEFT:
                if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
//                     if (playerCtrl == NULL){
//                         printf("No player controller found\n");
//                     } else {
//                     printf("playerctrl.lastsafepos .x: %g .y: %g \n",playerCtrl->lastSafePosition.x,playerCtrl->lastSafePosition.y);    
//                     playerCtrl->lastSafePosition.x = transform->position.x;
//                     }
//                     transform->lastSafePos = Vector2D(transform->position.x,transform->position.y);
//                     printf("lastSafePos .x: %g .y: %g \n",transform->lastSafePos.x,transform->lastSafePos.y);
                    if(transform->position.x>0){
                    transform->velocity.x = -1;
//                     if(Game::playerIsGrounded){
                        sprite->Play("Walk");
//                     }
                    sprite->spriteFlip = SDL_FLIP_NONE;
                    }
                }
				break;
            case SDLK_RIGHT:
                if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
//                     &entity->getComponent<PlayerController>().setLastSafePos(Vector2D(transform->position.x,transform->position.y));
//                     playerCtrl->lastSafePosition.x = transform->position.x;
//                     transform->lastSafePos = Vector2D(transform->position.x,transform->position.y);
//                     =========
//                     Do a pre-check for tile-based smooth algorithm
//                     =========
//                     int intersectionTileX = collider->center.x;
                    if(transform->position.x<Game::levelMap.w){
                    transform->velocity.x = 1;
//                     if(Game::playerIsGrounded){
                        sprite->Play("Walk");
//                     }
                    sprite->spriteFlip = SDL_FLIP_HORIZONTAL;
                    }
                }
				break;
//             case SDLK_a:
//                 if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
//                     transform->velocity.x = -1;
//                     if(Game::playerIsGrounded){
//                         sprite->Play("Walk");
//                     }
//                     sprite->spriteFlip = SDL_FLIP_NONE;
//                 }
//                 break;
//             case SDLK_d:
//                 if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
//                     transform->velocity.x = 1;
//                     if(Game::playerIsGrounded){
//                         sprite->Play("Walk");
//                     }
//                     sprite->spriteFlip = SDL_FLIP_HORIZONTAL;
//                 }
//                 break;
			case SDLK_k:
				if (!Game::debugCollisionBoxes)
                    { Game::debugCollisionBoxes = true; }
				else
                    {Game::debugCollisionBoxes = false; }
                if (!Game::debugMenu)
                    { Game::debugMenu = true; }
                else
                    { Game::debugMenu = false; }
				break;
			case SDLK_j:
                if(Game::playerIsGrounded){
                    if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
                        if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0) == 0)
                        {
                            Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0);
                        }
                        transform->lastSafePos = Vector2D(transform->position.x,transform->position.y);
                        Game::gravityOnPlayer = true;
                        sprite->Play("Jump");
                        transform->velocity.y = -2;
                    }
                }
				break;
            case SDLK_u:
                Game::gsm->AdvanceState();
                break;
            case SDLK_i:
                Game::gsm->GameOver();
                break;
			default:
				break;
			}
		}
// 		===============================================
// 		           ON KEY UP
// 		===============================================
		else if (Game::event.type == SDL_KEYUP)
		{
			switch (Game::event.key.keysym.sym)
			{
                case SDLK_LEFT:
                if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
                    transform->velocity.x = 0;
                    sprite->Play("Idle");
//                     if (!Game::gravityOnPlayer){
//                         Game::gravityOnPlayer = true;
//     //                     sprite->Play("Fall");
//                     }
                }
				break;
                case SDLK_RIGHT:
                if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
                    transform->velocity.x = 0;
                    sprite->Play("Idle");
//                     if (!Game::gravityOnPlayer){
//                         Game::gravityOnPlayer = true;
//                     }
                }
				break;
                case SDLK_UP:
                if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
                    transform->velocity.y = 0;
                    sprite->Play("Idle");
//                     if (!Game::gravityOnPlayer){
//                         Game::gravityOnPlayer = true;
//     //                     sprite->Play("Fall");
//                     }
                }
				break;
                case SDLK_DOWN:
                if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
                    transform->velocity.y = 0;
                    sprite->Play("Idle");
//                     if (!Game::gravityOnPlayer){
//                         Game::gravityOnPlayer = true;
//                     }
                }
				break;
//             case SDLK_a:
//                 if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
//                     transform->velocity.x = 0;
//                     sprite->Play("Idle");
//                     if (!Game::gravityOnPlayer){
//                         Game::gravityOnPlayer = true;
//     //                     sprite->Play("Fall");
//                     }
//                 }
// 				break;
//             case SDLK_d:
//                 if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
//                     transform->velocity.x = 0;
//                     sprite->Play("Idle");
//                     if (!Game::gravityOnPlayer){
//                         Game::gravityOnPlayer = true;
//                     }
//                 }
// 				break; 
			case SDLK_k:
				break;
            case SDLK_j:
                if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
                    transform->velocity.y = 0;
                    sprite->Play("Fall");
                    Game::gravityOnPlayer = true;
                }
                break;
            case SDLK_u:
                break;
            case SDLK_i:
                break;
//             case SDLK_d:
//                 if (Game::debugMenu == false){
//                 Game::debugMenu = true;}
//                 else {
//                 Game::debugMenu = false;    
//                 }
//                 break;
			case SDLK_ESCAPE: // exit the game when Escape pressed
				Game::isRunning = false;
				break;
			default:
				break;
			}
		}
	}
};


#endif /* SRC_ECS_KEYBOARDCONTROLLER_H_ */
