#include "util.h"

namespace util{
	float random(float range){
		return float(rand())/RAND_MAX*range;
	}
	void ApplyMatrixEffect(PixelGameEngine*pge,Renderable&r,Renderable&originalImg,std::unique_ptr<Renderable>&matrixImg){
		pge->SetDrawTarget(r.Sprite());
		pge->Clear(BLANK);
		for(int y=0;y<r.Sprite()->height;y++){
			for(int x=0;x<r.Sprite()->width;x++){
				Pixel col=originalImg.Sprite()->GetPixel(x,y);
				if(col==WHITE){
					pge->Draw({x,y},matrixImg->Sprite()->GetPixel(x,y));
				} else {
					pge->Draw({x,y},originalImg.Sprite()->GetPixel(x,y));
				}
			}
		}
		pge->SetDrawTarget(nullptr);
		r.Decal()->Update();
	};
	bool CanAfford(Resources&resources,std::vector<Memory>&unitCosts){
		for(Memory&mem:unitCosts){
			switch(mem.type){
			case HEALTH:{
				if(resources.health<mem.size)return false;
			}break;
			case ATKSPD:{
				if(resources.atkSpd<mem.size)return false;
			}break;
			case MOVESPD:{
				if(resources.moveSpd<mem.size)return false;
			}break;
			case RANGE:{
				if(resources.range<mem.size)return false;
			}break;
			case PROCEDURE:{
				if(resources.procedure<mem.size)return false;
			}break;
			}
		}
		return true;
	}

	int GetHealthCost(std::vector<Memory>&unitCosts){
		for(Memory&mem:unitCosts){
			if(mem.type==HEALTH)return mem.size;
		}
		return -1;
	};
	int GetAtkSpdCost(std::vector<Memory>&unitCosts){
		for(Memory&mem:unitCosts){
			if(mem.type==ATKSPD)return mem.size;
		}
		return -1;
	};
	int GetMoveSpdCost(std::vector<Memory>&unitCosts){
		for(Memory&mem:unitCosts){
			if(mem.type==MOVESPD)return mem.size;
		}
		return -1;
	};
	int GetRangeCost(std::vector<Memory>&unitCosts){
		for(Memory&mem:unitCosts){
			if(mem.type==RANGE)return mem.size;
		}
		return -1;
	};
	int GetProcedureCost(std::vector<Memory>&unitCosts){
		for(Memory&mem:unitCosts){
			if(mem.type==PROCEDURE)return mem.size;
		}
		return -1;
	};
}