#include "Player/PlayerSkills.h"
#include "Player/PlayerTactics.h"
#include "Common/Main.h"
#include "Common/Tasks.h"
#include "Common/Support/Debug.h"
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include <limits.h>

#include "DataTypes.h"


bool PlayerTactics::ShallPredict()
{
	float distance = (world->ball->pos - world->me->pos).GetLength();
	if((world->ball->GetAge() == 0) && (distance <= 15.0f) && (distance >= 5.0f))
	{
		TXTN("SHALLPREDICT!!!");
		return true;
	}

	return false;
}


void PlayerSkills::ExecuteAction(AS* action)
{
	TXTN("Vykonanie akcie typu %d, cislo akcie %d", action->type, action->action);

	if(action->type == PASS)
		PassBallToPlayer((ASPass*)action);
	else if(action->type == DRIBBLET)
		ExecuteDribble((ASDribble*)action);
	else if(action->type == INTERCEPT)
		MoveToInterceptPoint((ASReceive*)action);
}


AS* PlayerTactics::PredictActionOfFriend()
{
	if(!ShallPredict())	return NULL;

	int player = FriendPlayerWithBall();
	if(player == -1) return NULL;

	int uniform = world->friends[player].GetUniform();
	TXTN("Loptu ma pod kontrolou nas hrac %d", uniform);

	if(uniform == 1) return NULL;

	//ASPass* passaction = TryPass(player);
	//ASDribble* dribbleaction = TryDribble(player);

	//if(passaction->payoff == -1 && dribbleaction->payoff == -1) return NULL;
	//if(passaction->payoff >= dribbleaction->payoff)
	//	return passaction;
	//else
	//	return dribbleaction;

	if(ShallPass(player))
	{
		TXTN("HRAC BY MAL PRIHRAT.");
	}
	else
	{
		
	}

	return NULL;
}



AS* PlayerTactics::MyAction()
{
	float ballDistance = (world->ball->pos - world->me->pos).GetLength();
	float karea = (world->params->server->ball_size + world->me->type->player_size + world->me->type->kickable_margin);
	bool maybekickable = (ballDistance < karea) && (ballDistance > 0.8f * karea);
	bool kickable = ballDistance <= (0.8f * karea);

	if(maybekickable && (abs((world->ball->pos - world->me->pos).GetAngle().GetDeg() - world->me->GetAbsDir().GetDeg()) < 20.f))
	{
		maybekickable = false;
		kickable = true;
	}

	if(maybekickable && (GetNearestEnemyDistance(world->me->pos) < 3.0f))
	{
		maybekickable = false;
		kickable = true;
	}

	TXTN("MYACTION ballDistance : %f, kickable : %d, maybekickable %d, ignoreNotKickable %d", ballDistance, kickable, maybekickable, ignoreNotKickable);

	if(kickable)
	{
		ASPass* passaction = TryPass(-1);
		ASDribble* dribbleaction = TryDribble(-1);

		if(passaction->payoff == -1 && dribbleaction->payoff == -1) 
		{
			TXTN("%i: MyAction : No action!!!", world->state->GetSimTime());

			delete passaction;
			delete dribbleaction;
			return NULL;
		}

		if(passaction->payoff >= dribbleaction->payoff)
		{
			if(passaction->action != DONOTHING)
			{
				delete dribbleaction;
				return passaction; 
			}
			else 
			{
				delete dribbleaction;
				delete passaction;
				return NULL;
			}
		}
		else if(dribbleaction->action != DONOTHING) 
		{
			delete passaction;
			return dribbleaction; 
		}
		else 
		{
			delete dribbleaction;
			delete passaction;
			return NULL;
		}
	}
	else if(maybekickable)
	{
		if(DontRunAfterBall == 0) return HoldBall(); else return NULL;	
	}	
	else if(!ignoreNotKickable)
	{
		if(ballDistance > 30.0f) return NULL;
		
		AS* ra = TryInterceptBall(NULL);
		if(ra->type != INTERCEPT) return ra;

		
		ASReceive* receiveaction = (ASReceive*)ra;

		if(receiveaction->shallrun)
		{
			return receiveaction;
		}

		AS* faction = PredictActionOfFriend(); 
	
		if(faction != NULL)
		{
			
		}
		
		return NULL;
	}

	return NULL;
}




