#include "Player.hpp"
#include "Echo.hpp"
#include "Shared.hpp"

Texture Corrupted::_tex;

Corrupted::Corrupted(Shared* context)
{
	_context = context;
	setOrigin(16.f, 25.f);
	if(_tex.getSize().x == 0)
		_tex.loadFromFile("data/corrupted.png");
	setTexture(_tex);
	_direction = rand() % 2;
	out = 24.f;
	in = 0.f;
	height = 0.f;
	dead = false;
	lifespan = rand() % 10000 + 5000;
	setTextureRect(IntRect(0, 0,
					_tex.getSize().x / 166, _tex.getSize().y));
	speed = 72.f;
	hp = 1;
	ready = false;
}

Corrupted::~Corrupted()
{

}

void Corrupted::loop(float delta)
{
	xd += delta;
	if(xd >= 0.5f) {
		xd = 0.f;
		if(!(rand() % 4))
			_direction = !_direction;
	}

	for(unsigned i = 0; i < _context->gz.size(); ++i) {
		if(getPosition().x >= _context->gz[i].left && getPosition().x <= _context->gz[i].left + _context->gz[i].width &&
		   getPosition().y >= _context->gz[i].top && getPosition().y <= _context->gz[i].top + _context->gz[i].height) {
			   --hp;
			   if(hp <= 0)
			   		dead = true;
			   _context->gz[i].width = 0;
			   _context->_s += 100;
			   return;
		}
		if(getPosition().x - 8.f * getScale().x >= _context->gz[i].left && getPosition().x - 8.f * getScale().x <= _context->gz[i].left + _context->gz[i].width &&
		   getPosition().y >= _context->gz[i].top && getPosition().y <= _context->gz[i].top + _context->gz[i].height) {
			   --hp;
			   if(hp <= 0)
			   		dead = true;
			   _context->gz[i].width = 0;
			   _context->_s += 100;
			   return;
		}
	}



	if(height == 0.f) {
		height = getPosition().y - out;
	}
	if(out != 0.f) {
		float tmp = delta * -24.f;
		out += tmp;
		if(out < 0.f) {
			out = 0.f;
			setPosition(getPosition().x, height);
			ready = true;
		}
		else
			move(0.f, tmp);
	}
	else if(in != 0.f) {
		float tmp = delta * 24.f;
		in -= tmp;
		if(in < 0.f) {
			in = 0.f;
			dead = true;
		}
		else
			move(0.f, tmp);
	}
	else {
		if(_anclock.getElapsedTime().asMilliseconds() > lifespan) {
			in = 24.f;
		}
		bool k = false;
		for(unsigned i = 0; i < _context->cp.size(); ++i) {
			if((((getPosition().x >= _context->cp[i].left + 4.f && getPosition().x <= _context->cp[i].left - 4.f + _context->cp[i].width) ||
			   (getPosition().x - 8.f >= _context->cp[i].left && getPosition().x - 8.f <= _context->cp[i].left + _context->cp[i].width)) &&
			   getPosition().y >= _context->cp[i].top && getPosition().y <= _context->cp[i].top + _context->cp[i].height)) {
				   k = true;
			}
		}
		if(!k) {
			_direction = !_direction;
		}

		if(_direction) {
			move(delta * speed, 0.f);
			setScale(1.f, 1.f);
			for(unsigned i = 0; i < _context->cb.size(); ++i) {
				if(getPosition().x + 12.f > _context->cb[i].left && getPosition().x + 12.f < _context->cb[i].left + _context->cb[i].width &&
				   getPosition().y - 8.f > _context->cb[i].top && getPosition().y - 8.f < _context->cb[i].top + _context->cb[i].height) {
					_direction = !_direction;
					break;
				}
			}
		}
		else {
			move(delta * -speed, 0.f);
			setScale(-1.f, 1.f);
			for(unsigned i = 0; i < _context->cb.size(); ++i) {
				if(getPosition().x - 12.f > _context->cb[i].left && getPosition().x - 12.f < _context->cb[i].left + _context->cb[i].width &&
				   getPosition().y - 8.f > _context->cb[i].top && getPosition().y - 8.f < _context->cb[i].top + _context->cb[i].height) {
					_direction = !_direction;
					break;
				}
			}
		}

		unsigned elapsed = _anclock.getElapsedTime().asMilliseconds() % 500;
		unsigned frame = (elapsed * 166) / 500;
		setTextureRect(IntRect(_tex.getSize().x * frame / 166, 0,
						_tex.getSize().x / 166, _tex.getSize().y));
	}
}
