#include "Effect.h"
#include "DEFINES.h"
#include "Crawler.h"
#include "utils.h"
#include "safemap.h"

INCLUDE_game
INCLUDE_ANIMATION_DATA
INCLUDE_MONSTER_LIST

PulsatingFire::PulsatingFire(vf2d pos, float lifetime, std::string animation, bool upperLevel, vf2d size, float fadeout, vf2d spd, Pixel col, float rotation, float rotationSpd, bool additiveBlending)
	:Effect(pos,lifetime,animation,upperLevel,size,fadeout,spd,col,rotation,rotationSpd,additiveBlending){
	for(int i=0;i<8;i++){
		pulsatingFireValues.push_back(util::random(1));
	}
}

bool PulsatingFire::Update(float fElapsedTime){
	lastParticleTimer-=fElapsedTime;
	lastDamageTimer-=fElapsedTime;
	if(lastParticleTimer<=0){
		int particleCount="Wizard.Ability 3.FireRingParticleSpawnAmtRange"_FRange;
		for(int i=0;i<particleCount;i++){
			float randomAngle="Wizard.Ability 3.FireRingParticleAngleRange"_FRange;
			float randomRange=12*size.x*(1-util::random("Wizard.Ability 3.FireRingParticleRandomVariance"_F))*(1-util::random("Wizard.Ability 3.FireRingParticleRandomVariance"_F));
			float randomColorTintG=128*(1-util::random("Wizard.Ability 3.FireRingParticleColorGVariance"_F))*(1-util::random("Wizard.Ability 3.FireRingParticleColorGVariance"_F));
			float randomColorTint="Wizard.Ability 3.FireRingParticleColorBlueRange"_FRange;
			game->AddEffect(std::make_unique<Effect>(pos+vf2d{cos(randomAngle),sin(randomAngle)}*randomRange,0,"circle.png",OnUpperLevel(),vf2d{"Wizard.Ability 3.FireRingParticleXSizeRange"_FRange,"Wizard.Ability 3.FireRingParticleYSizeRange"_FRange},"Wizard.Ability 3.FireRingParticleFadeoutTimeRange"_FRange,vf2d{"Wizard.Ability 3.FireRingParticleXSpeedRange"_FRange,"Wizard.Ability 3.FireRingParticleYSpeedRange"_FRange},Pixel{128,uint8_t(randomColorTintG),uint8_t(randomColorTint),uint8_t("Wizard.Ability 3.FireRingParticleAlphaRange"_FRange)}));
		}
		lastParticleTimer="Wizard.Ability 3.FireRingParticleFreqRange"_FRange;
	}
	if(lastDamageTimer<=0){
		lastDamageTimer="Wizard.Ability 3.FireRingDamageFreq"_F-0.01;
		game->HurtEnemies(pos,"Wizard.Ability 3.MeteorRadius"_F/100*24,game->GetPlayer()->GetAttack()*"Wizard.Ability 3.FireRingDamageMult"_F,OnUpperLevel(),0);
	}
	return Effect::Update(fElapsedTime);
}

void PulsatingFire::Draw(){
	for(int i=0;i<8;i++){
		Animate2D::FrameSequence*effectSpr=nullptr;
		switch(int(pulsatingFireValues[i]*5)){
			case 0:{
				effectSpr=&ANIMATION_DATA["fire_ring0.png"];
			}break;
			case 1:{
				effectSpr=&ANIMATION_DATA["fire_ring1.png"];
			}break;
			case 2:{
				effectSpr=&ANIMATION_DATA["fire_ring2.png"];
			}break;
			case 3:{
				effectSpr=&ANIMATION_DATA["fire_ring3.png"];
			}break;
			case 4:{
				effectSpr=&ANIMATION_DATA["fire_ring4.png"];
			}break;
			default:
				effectSpr=&ANIMATION_DATA["fire_ring0.png"];
		}
		const Renderable*img=effectSpr->GetFrame(0).GetSourceImage();
		game->view.DrawPartialDecal(pos-effectSpr->GetFrame(0).GetSourceRect().size/2*size,img->Decal(),effectSpr->GetFrame(0).GetSourceRect().pos,effectSpr->GetFrame(0).GetSourceRect().size,size,{255,uint8_t(pulsatingFireValues[i]*256),0,uint8_t((63*(sin("Wizard.Ability 3.FireRingOscillatingFrequency"_F*lifetime+PI*pulsatingFireValues[i]))+63)*(fadeout/original_fadeoutTime))});
	}
}