#pragma once
#include "cocos2d.h"
#include "PRect.h"
#include "Item.h"

class UnitTemplate
{
public:
	unsigned _id;
	int _speed;
	int _score;
	int _health;
	int _critHealth;
	int _behavior;
	cocos2d::Vec2 _size;
	std::vector<int> _attacks;

	UnitTemplate();
	UnitTemplate(unsigned id,
		int speed,
		int score,
		int health,
		int critHealth,
		int behavior,
		cocos2d::Vec2 size,
		std::vector<int>* attacks);
	~UnitTemplate();
};

#define BOT_TEAM 1
#define PLAYER1_TEAM 2
#define PLAYER2_TEAM 3

class Unit
{
protected:
public:
	bool _inAction;
	bool _facingRight;
	bool _isMoving;
	
	bool _dead;
	UnitTemplate* _template;
	
	cocos2d::Sprite* _sprite;
	cocos2d::Sprite* _stunSprite;
	cocos2d::SpriteBatchNode* _batch; 
	cocos2d::SpriteBatchNode* _stunBatch;
	
	unsigned _team;
	int _health;
	int _specialCount;
	int _specialCountMax;
	float _slideCalldown;
	std::vector<float> _calldowns;

	cocos2d::Vec2 _pushDirecton;
	cocos2d::Vec2 _moveDirection;
	PRect _rect;

	ARect _attackedRect;
	unsigned _attackIndex;

	Unit(void);
	Unit(UnitTemplate* u, cocos2d::Vec2 position, unsigned team, cocos2d::Node* parent);
	~Unit(void);

	virtual void ActionStopped();
	virtual void Attack(unsigned type);

	cocos2d::Vec2 countDelta(cocos2d::Vec2 direction);
	void Move();
	void stopMoving();
	bool checkDirectionConflict(std::vector<PRect*>* rects, cocos2d::Vec2 direction, float speed);
	virtual void UpdateMove(std::vector<PRect*>* rects, bool direction = false);
	void addDirection(cocos2d::Vec2 direction, std::vector<PRect*>* rects);
	void UpdateRect();
	void UpdateDirection();
	void UpdateZOrder(unsigned z);
	virtual void UpdateCalldown(float dt);
	void Attacked(ARect* attackedRect);
	void Die();
	bool isDead();
	void StunFinished();
	unsigned GetTeam();
	void StartMoveAnim();
	void CheckItemPickUp(Item* item);
	virtual void UseItem(Item* item);

	void CheckHealth();
	ARect GetAttackRect();

	PRect* getRect();
	cocos2d::SpriteBatchNode* GetBatch();

};

bool compareUnitByZOrder(Unit* u1,Unit* u2);
