#ifndef Player_hpp
#define Player_hpp

enum class states { idle, run };

class Player : public GameObject {
public:
	sf::Texture* idleTextures[16];	// idle for top, right, bottom, left
	sf::Texture* runTextures[16];	// run for top, right, bottom, left
	sf::Sprite* bodySprite;
	sf::CircleShape* collider;
	int direction;
	int step;
	float stepSize = 4.0f;
	states state;

	Player() : GameObject(0,0,32) {
		direction = 2;
		step = 0;
		state = states::idle;

		// idle textures
		for (int i = 0; i < 16; i++)
			idleTextures[i] = new sf::Texture();

		idleTextures[0]->loadFromFile("hero/idleTop0.png");
		idleTextures[1]->loadFromFile("hero/idleTop1.png");
		idleTextures[2]->loadFromFile("hero/idleTop2.png");
		idleTextures[3]->loadFromFile("hero/idleTop3.png");

		idleTextures[4]->loadFromFile("hero/idleRight0.png");
		idleTextures[5]->loadFromFile("hero/idleRight1.png");
		idleTextures[6]->loadFromFile("hero/idleRight2.png");
		idleTextures[7]->loadFromFile("hero/idleRight3.png");

		idleTextures[8]->loadFromFile("hero/idleBottom0.png");
		idleTextures[9]->loadFromFile("hero/idleBottom1.png");
		idleTextures[10]->loadFromFile("hero/idleBottom2.png");
		idleTextures[11]->loadFromFile("hero/idleBottom3.png");

		idleTextures[12]->loadFromFile("hero/idleLeft0.png");
		idleTextures[13]->loadFromFile("hero/idleLeft1.png");
		idleTextures[14]->loadFromFile("hero/idleLeft2.png");
		idleTextures[15]->loadFromFile("hero/idleLeft3.png");

		// run textures
		for(int i=0;i<16;i++)
			runTextures[i] = new sf::Texture();

		runTextures[0]->loadFromFile("hero/runTop0.png");
		runTextures[1]->loadFromFile("hero/runTop1.png");
		runTextures[2]->loadFromFile("hero/runTop2.png");
		runTextures[3]->loadFromFile("hero/runTop3.png");

		runTextures[4]->loadFromFile("hero/runRight0.png");
		runTextures[5]->loadFromFile("hero/runRight1.png");
		runTextures[6]->loadFromFile("hero/runRight2.png");
		runTextures[7]->loadFromFile("hero/runRight3.png");

		runTextures[8]->loadFromFile("hero/runBottom0.png");
		runTextures[9]->loadFromFile("hero/runBottom1.png");
		runTextures[10]->loadFromFile("hero/runBottom2.png");
		runTextures[11]->loadFromFile("hero/runBottom3.png");

		runTextures[12]->loadFromFile("hero/runLeft0.png");
		runTextures[13]->loadFromFile("hero/runLeft1.png");
		runTextures[14]->loadFromFile("hero/runLeft2.png");
		runTextures[15]->loadFromFile("hero/runLeft3.png");
	
		bodySprite = new sf::Sprite();
		bodySprite->setOrigin(32, 58);

		collider = new sf::CircleShape(16);
		collider->setFillColor(sf::Color(128, 64, 64, 128));
		collider->setOrigin(16, 16);
		collider->setScale(1, 0.5f);
	}

	~Player() { }
	
	

	void move(int direction ) { 
		state = states::run;

		if (direction != this->direction) {
			this->direction = direction;
			step = 0;
		}
		else
		{	// directions are same
			if (direction == 0) y -= 4.0f;
			if (direction == 1) x += 4.0f;
			if (direction == 2) y += 4.0f;
			if (direction == 3) x -= 4.0f;

			
		}

		
	}

	void setPosition(float x, float y) {
		this->x = x;
		this->y = y;
	}

	void update() {

		step += 1;
		if (step > 3)
			step = 0;

		if (state == states::run) {
			
			state = states::idle;
			bodySprite->setTexture(*runTextures[direction * 4 + step]);
		}
		else if(state == states::idle) {
			
			bodySprite->setTexture(*idleTextures[direction * 4 + step]);
		}

		bodySprite->setPosition(x,y);
		collider->setPosition(x, y);
		
	}

	void render(sf::RenderWindow* window) {
		
		window->draw(*bodySprite);
		window->draw(*collider);
	}
};

#endif