#pragma once
#include "olcUTIL_Animate2D.h"
#include "Animation.h"
#include "Monster.h"
#include "State.h"
#include "Ability.h"
#include "Class.h"
#include "Buff.h"
#include "Pathfinding.h"


struct CastInfo{
	std::string name;
	float castTimer;
	float castTotalTime;
};

struct Player{
	friend class Crawler;
	friend class sig::Animation;
	private:
	int hp=100,maxhp=hp;
	int mana=100,maxmana=mana;
	int atk=10;
	vf2d pos;
	float friction=400;
	float z=0;
	float moveSpd=1.0f;
	float size=1.0f;
	float spin_attack_timer=0;
	float spin_spd=0;
	float spin_angle=0;
	float lastAnimationFlip=0;
	float manaTickTimer=0;
	std::pair<std::string,float> notEnoughManaDisplay={"",0};
	float teleportAttemptWaitTime=0; //If a teleport fails, we wait awhile before trying again, it's expensive.
	State state=State::NORMAL;
	Animate2D::Animation<AnimationState>animation;
	Animate2D::AnimationState internal_animState;
	Key lastReleasedMovementKey;
	void Update(float fElapsedTime);
	void AddAnimation(AnimationState state);
	std::vector<Buff>buffList;
	CastInfo castInfo={"",0};
protected:
	const float ATTACK_COOLDOWN=0.35f;
	const float MAGIC_ATTACK_COOLDOWN=0.85f;
	void SetSwordSwingTimer(float val);
	void SetState(State newState);
	void SetFacingDirection(Key direction);
	void SetLastReleasedMovementKey(Key k);
	void Spin(float duration,float spinSpd);
	//Returns true if the move was valid and successful.
	bool SetX(float x);
	//Returns true if the move was valid and successful.
	bool SetY(float y);
	void SetZ(float z);
	//Returns true if the move was valid and successful.
	bool SetPos(vf2d pos);
	float attack_cooldown_timer=0;
	float iframe_time=0;
	float teleportAnimationTimer=0;
	vf2d teleportTarget={};
	vf2d teleportStartPosition={};
	std::pair<std::string,float> notificationDisplay={"",0};
	bool upperLevel=false;
	vf2d vel={0,0};
	float attack_range=1.5f;
	Key facingDirection;
	float swordSwingTimer=0;
	void CastSpell(Ability&ability);
	Ability*castPrepAbility;
	void PrepareCast(Ability&ability);
	vf2d precastLocation={};
public:
	Player();
	//So this is rather fascinating and only exists because we have the ability to change classes which means we need to initialize a class
	//using a new object type... Because of that we'll take the pointer reference to the old object and copy some of its properties to this new
	//one. It's hackish but it means we can reduce the amount of extra boilerplate when class changing...I don't know how to feel about this.
	Player(Player*player);
	const static float GROUND_SLAM_SPIN_TIME;
	vf2d&GetPos();
	float GetX();
	float GetY();
	float GetZ();
	int GetHealth();
	int GetMaxHealth();
	int GetMana();
	int GetMaxMana();
	int GetAttack();
	float GetMoveSpdMult();
	float GetSizeMult();
	float GetAttackRangeMult();
	float GetSpinAngle();
	State GetState();
	Key GetFacingDirection();
	vf2d GetVelocity();
	bool HasIframes();
	void UpdateWalkingAnimation(Key direction);
	void UpdateIdleAnimation(Key direction);
	//The range is the search range in tiles.
	bool CanPathfindTo(vf2d pos,vf2d targetPos,float range=8);
	bool CanMove();

	void AddBuff(BuffType type,float duration,float intensity);
	std::vector<Buff>GetBuffs(BuffType buff);

	bool Hurt(int damage);
	//specificClass is a bitwise-combination of classes from the Class enum. It makes sure certain animations only play if you are a certain class.
	void UpdateAnimation(AnimationState animState,int specificClass=ANY);
	Animate2D::Frame GetFrame();
	Key GetLastReleasedMovementKey();
	float GetSwordSwingTimer();
	bool OnUpperLevel();
	//Triggers when the player has moved.
	void Moved();
	virtual ~Player()=default;
	virtual Class GetClass()=0;
	virtual bool AutoAttack()=0;
	virtual void OnUpdate(float fElapsedTime)=0;
	virtual void InitializeClassAbilities()=0;
	virtual std::string GetClassName()=0;
	virtual Ability&GetRightClickAbility()=0;
	virtual Ability&GetAbility1()=0;
	virtual Ability&GetAbility2()=0;
	virtual Ability&GetAbility3()=0;
	virtual Ability&GetAbility4()=0;
	virtual AnimationState&GetWalkNAnimation()=0;
	virtual AnimationState&GetWalkEAnimation()=0;
	virtual AnimationState&GetWalkSAnimation()=0;
	virtual AnimationState&GetWalkWAnimation()=0;
	virtual AnimationState&GetIdleNAnimation()=0;
	virtual AnimationState&GetIdleEAnimation()=0;
	virtual AnimationState&GetIdleSAnimation()=0;
	virtual AnimationState&GetIdleWAnimation()=0;

	CastInfo&GetCastInfo();
};

struct Warrior:Player{
	static std::string name;
	static Class cl;
	static Ability rightClickAbility,ability1,ability2,ability3,ability4;
	static AnimationState walk_n,walk_e,walk_s,walk_w,idle_n,idle_e,idle_s,idle_w;
	Warrior();
	Warrior(Player*player);
	Class GetClass()override;
	bool AutoAttack()override;
	void OnUpdate(float fElapsedTime)override;
	void InitializeClassAbilities()override;
	std::string GetClassName()override;
	Ability&GetRightClickAbility()override;
	Ability&GetAbility1()override;
	Ability&GetAbility2()override;
	Ability&GetAbility3()override;
	Ability&GetAbility4()override;
	AnimationState&GetWalkNAnimation()override;
	AnimationState&GetWalkEAnimation()override;
	AnimationState&GetWalkSAnimation()override;
	AnimationState&GetWalkWAnimation()override;
	AnimationState&GetIdleNAnimation()override;
	AnimationState&GetIdleEAnimation()override;
	AnimationState&GetIdleSAnimation()override;
	AnimationState&GetIdleWAnimation()override;
};

struct Thief:Player{
	static std::string name;
	static Class cl;
	static Ability rightClickAbility,ability1,ability2,ability3,ability4;
	static AnimationState walk_n,walk_e,walk_s,walk_w,idle_n,idle_e,idle_s,idle_w;
	Thief();
	Thief(Player*player);
	Class GetClass()override;
	bool AutoAttack()override;
	void OnUpdate(float fElapsedTime)override;
	void InitializeClassAbilities()override;
	std::string GetClassName()override;
	Ability&GetRightClickAbility()override;
	Ability&GetAbility1()override;
	Ability&GetAbility2()override;
	Ability&GetAbility3()override;
	Ability&GetAbility4()override;
	AnimationState&GetWalkNAnimation()override;
	AnimationState&GetWalkEAnimation()override;
	AnimationState&GetWalkSAnimation()override;
	AnimationState&GetWalkWAnimation()override;
	AnimationState&GetIdleNAnimation()override;
	AnimationState&GetIdleEAnimation()override;
	AnimationState&GetIdleSAnimation()override;
	AnimationState&GetIdleWAnimation()override;
};

struct Ranger:Player{
	static std::string name;
	static Class cl;
	static Ability rightClickAbility,ability1,ability2,ability3,ability4;
	static AnimationState walk_n,walk_e,walk_s,walk_w,idle_n,idle_e,idle_s,idle_w;
	Ranger();
	Ranger(Player*player);
	Class GetClass()override;
	bool AutoAttack()override;
	void OnUpdate(float fElapsedTime)override;
	void InitializeClassAbilities()override;
	std::string GetClassName()override;
	Ability&GetRightClickAbility()override;
	Ability&GetAbility1()override;
	Ability&GetAbility2()override;
	Ability&GetAbility3()override;
	Ability&GetAbility4()override;
	AnimationState&GetWalkNAnimation()override;
	AnimationState&GetWalkEAnimation()override;
	AnimationState&GetWalkSAnimation()override;
	AnimationState&GetWalkWAnimation()override;
	AnimationState&GetIdleNAnimation()override;
	AnimationState&GetIdleEAnimation()override;
	AnimationState&GetIdleSAnimation()override;
	AnimationState&GetIdleWAnimation()override;
};

struct Trapper:Player{
	static std::string name;
	static Class cl;
	static Ability rightClickAbility,ability1,ability2,ability3,ability4;
	static AnimationState walk_n,walk_e,walk_s,walk_w,idle_n,idle_e,idle_s,idle_w;
	Trapper();
	Trapper(Player*player);
	Class GetClass()override;
	bool AutoAttack()override;
	void OnUpdate(float fElapsedTime)override;
	void InitializeClassAbilities()override;
	std::string GetClassName()override;
	Ability&GetRightClickAbility()override;
	Ability&GetAbility1()override;
	Ability&GetAbility2()override;
	Ability&GetAbility3()override;
	Ability&GetAbility4()override;
	AnimationState&GetWalkNAnimation()override;
	AnimationState&GetWalkEAnimation()override;
	AnimationState&GetWalkSAnimation()override;
	AnimationState&GetWalkWAnimation()override;
	AnimationState&GetIdleNAnimation()override;
	AnimationState&GetIdleEAnimation()override;
	AnimationState&GetIdleSAnimation()override;
	AnimationState&GetIdleWAnimation()override;
};

struct Wizard:Player{
	static std::string name;
	static Class cl;
	static Ability rightClickAbility,ability1,ability2,ability3,ability4;
	static AnimationState walk_n,walk_e,walk_s,walk_w,idle_n,idle_e,idle_s,idle_w;
	Wizard();
	Wizard(Player*player);
	Class GetClass()override;
	bool AutoAttack()override;
	void OnUpdate(float fElapsedTime)override;
	void InitializeClassAbilities()override;
	std::string GetClassName()override;
	Ability&GetRightClickAbility()override;
	Ability&GetAbility1()override;
	Ability&GetAbility2()override;
	Ability&GetAbility3()override;
	Ability&GetAbility4()override;
	AnimationState&GetWalkNAnimation()override;
	AnimationState&GetWalkEAnimation()override;
	AnimationState&GetWalkSAnimation()override;
	AnimationState&GetWalkWAnimation()override;
	AnimationState&GetIdleNAnimation()override;
	AnimationState&GetIdleEAnimation()override;
	AnimationState&GetIdleSAnimation()override;
	AnimationState&GetIdleWAnimation()override;
};

struct Witch:Player{
	static std::string name;
	static Class cl;
	static Ability rightClickAbility,ability1,ability2,ability3,ability4;
	static AnimationState walk_n,walk_e,walk_s,walk_w,idle_n,idle_e,idle_s,idle_w;
	Witch();
	Witch(Player*player);
	Class GetClass()override;
	bool AutoAttack()override;
	void OnUpdate(float fElapsedTime)override;
	void InitializeClassAbilities()override;
	std::string GetClassName()override;
	Ability&GetRightClickAbility()override;
	Ability&GetAbility1()override;
	Ability&GetAbility2()override;
	Ability&GetAbility3()override;
	Ability&GetAbility4()override;
	AnimationState&GetWalkNAnimation()override;
	AnimationState&GetWalkEAnimation()override;
	AnimationState&GetWalkSAnimation()override;
	AnimationState&GetWalkWAnimation()override;
	AnimationState&GetIdleNAnimation()override;
	AnimationState&GetIdleEAnimation()override;
	AnimationState&GetIdleSAnimation()override;
	AnimationState&GetIdleWAnimation()override;
};