#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include "time.h"
#include<Windows.h>
#include "Ball.h"
#include "Helper.h"

void Ball::InitializeBall(double x, double y)
{
	mPosX = x;
	mPosY = y;
	mCollider.r = Ball_WIDTH / 2;
	mVelX = rand() % 8 + 1;
	mVelY = rand() % 8 + 1;
	realVelX = mVelX;
	realVelY = mVelY;
	double i = rand() % 2;
	if (i == 0)
		mVelX = -mVelX;
	i = rand() % 2;
	if (i == 0)
		mVelY = -mVelY;
	shiftColliders();
}

Ball::Ball(double x, double y)
{
	InitializeBall(x, y);
}

void Ball::handleEvent(SDL_Event& e)
{
	//If a key was pressed
	if (e.type == SDL_KEYDOWN && e.key.repeat == 0)
	{
		//Adjust the velocity
		switch (e.key.keysym.sym)
		{
		case SDLK_UP: mVelY -= Ball_VEL; break;
		case SDLK_DOWN: mVelY += Ball_VEL; break;
		case SDLK_LEFT: mVelX -= Ball_VEL; break;
		case SDLK_RIGHT: mVelX += Ball_VEL; break;
		}
	}
	//If a key was released
	else if (e.type == SDL_KEYUP && e.key.repeat == 0)
	{
		//Adjust the velocity
		switch (e.key.keysym.sym)
		{
		case SDLK_UP: mVelY += Ball_VEL; break;
		case SDLK_DOWN: mVelY -= Ball_VEL; break;
		case SDLK_LEFT: mVelX += Ball_VEL; break;
		case SDLK_RIGHT: mVelX -= Ball_VEL; break;
		}
	}
}

void Ball::move()
{
		// przesuwamy sie po x plaszczyznie
		mPosX += mVelX;
		shiftColliders();

		// sprawdzenie czy kula nie wyszła poza obszar/nie koliduje ze scianami lewo/prawo
		if ((mPosX - mCollider.r < 0) || (mPosX + mCollider.r > Helper::SCREEN_WIDTH))
		{
			mPosX -= mVelX;
			mVelX = -mVelX;
			shiftColliders();
		}
		// przesuwamy sie po y plaszczyznie
		mPosY += mVelY;
		shiftColliders();

		// sprawdzenie czy kula nie wyszła poza obszar/nie koliduje ze scianami góra/dół
		if ((mPosY - mCollider.r < 0) || (mPosY + mCollider.r > Helper::SCREEN_HEIGHT))
		{
			mPosY -= mVelY;
			mVelY = -mVelY;
			shiftColliders();
		}
}

void Ball::render()
{
	Helper::gBallTexture.render(mPosX - mCollider.r, mPosY - mCollider.r);
}

void Ball::checkCollision(Ball& otherBall)
{
	double totalRadiusSquared = mCollider.r + otherBall.mCollider.r;
	totalRadiusSquared = totalRadiusSquared * totalRadiusSquared;

	// na podstawie obliczonego promienia , sprawdzam czy kule ze soba nie koliduja
	// jak tak to zamieniam im składowe prędkości ( zakładam że wszystkie moje kule mają takie same masy)
	if (distanceSquared(mCollider.x, mCollider.y, otherBall.mCollider.x, otherBall.mCollider.y) <= (totalRadiusSquared))
	{
		double rVX = otherBall.realVelX;
		double rVY = otherBall.realVelY;
		double vX = otherBall.mVelX;
		double vY = otherBall.mVelY;
		otherBall.mVelX = mVelX;
		otherBall.mVelY = mVelY;
		otherBall.realVelX = realVelX;
		otherBall.realVelY = realVelY;
		realVelX = rVX;
		realVelY = rVY;
		mVelX = vX;
		mVelY = vY;
		otherBall.shiftColliders();
		shiftColliders();
	}

}

Circle& Ball::getCollider()
{
	return mCollider;
}

void Ball::shiftColliders()
{
	mCollider.x = mPosX;
	mCollider.y = mPosY;
}

void Ball::UpdateSpeed()
{
	realVelX -= realVelX*FR*deltaTime;
	realVelY -= realVelY*FR*deltaTime;
	if (mVelX > 0)
		mVelX = realVelX;
	else
		mVelX = -realVelX;

	if (mVelY > 0)
		mVelY = realVelY;
	else
		mVelY = -realVelY;
}

