local lib = starlit.mod.lib
local E = starlit.effect
local N = {}
starlit.fx.nano = N
local nanopool= {
	{
		name = 'starlit-fx-nano-spark-small.png';
		scale_tween = {0,.5, style = 'pulse', rep = 3};
	};
	{
		name = 'starlit-fx-nano-spark-small.png';
		scale_tween = {0,1, style = 'pulse', rep = 2};
	};
	{
		name = 'starlit-fx-nano-spark-big.png';
		scale_tween = {0,1, style = 'pulse'};
	};
}

function N.heal(user, targets, amt, dur)
	local amthealed = {}
	local f = E.cast {
		caster = user.entity;
		subjects = targets;
		duration = dur;
		intervals = {
			{
				after = 0;
				period = 4;
				fn = function(c)
					for i,v in pairs(c.effect.subjects) do
						local u = starlit.activeUsers[v.player:get_player_name()]
						if u then
							local heal = math.max(amt/4, 1)
							amthealed[u] = amthealed[u] or 0
							if amthealed[u] < amt then
								amthealed[u] = amthealed[u] + heal
								u:statDelta('health', heal)
							end
						end
					end
				end;
			}
		}
	}

	local casterIsTarget = false
	for _, sub in pairs(f.subjects) do
		if sub.player == user.entity then
			casterIsTarget = true
		end
		f.visual(sub, {
			amount = 50;
			time = dur;
			glow = 14;
			jitter = 0.01;
			attached = user.entity;
			vel = { min = -0.1, max = 0.1; };
			pos = {
				min = vector.new(0,0.2,0);
				max = vector.new(0,1.2,0);
			};
			radius  = { min = 0.2; max = 0.6; bias = -1; };
			exptime = {min=0.5,max=2};
			attract = {
				kind = 'line';
				strength = {min = 0.5, max = 2};
				origin = 0;
				direction = vector.new(0,1,0);
				origin_attached = sub.player;
				direction_attached = sub.player;
			};

			texpool = nanopool;
		})
	end
	if not casterIsTarget then
		-- f.visual_caster { }
	end
	f.play(0.3, {
		where = 'subjects';
		sound = 'starlit-nano-heal';
		ephemeral = true;
		spec = {gain = 0.3};
	})

	return f
end

function N.shred(user, pos, prop, time, node)
	local f = E.cast {
		caster = user.entity;
		subjects = {};
		duration = time;
	}
	local sp,sv = user:lookupSpecies()
	local eh = sv.eyeHeight or sp.eyeHeight
	f.visual_caster {
		amount = 200 * time;
		pos =  vector.new(0.12,eh - 0.1,0);
		radius = 0.2;
		time = time - (time/3);
		glow = 14;
		jitter = 0.1;
		size = {min = 0.2, max = 0.5};
		exptime = {min=0.5,max=1};
		vel_tween = {
			0;
			{ min = -0.4, max = 0.4; };
			style = 'pulse', rep = time * 2; 
		};
		attract = {
			kind = 'point';
			origin = pos;
			radius = 0.5;
			strength = {min=.3,max=2};
		};
		texpool = nanopool;
	};
	f.queue(0.05, function(s, timepast, timeleft)
		f.visual(nil, {
			amount = timeleft * 40;
			time = timeleft;
			pos = pos;
			size_tween = {
				0, {min = 0.5, max = 2};
			};
			vel = {
				min = vector.new(-1.2,0.5,-1.2);
				max = vector.new(1.2,3.5,1.2);
			};
			acc = vector.new(0,-starlit.world.planet.gravity,0);
			node = node;
		})
	end);
	f.queue(0.9, function(s, timepast, timeleft)
		f.visual(nil, {
			amount = 200;
			time = timeleft;
			pos = pos;
			size = {min = 0.1, max = 0.3};
			vel = {
				min = vector.new(-2,0.5,-2);
				max = vector.new(2,4,2);
			};
			acc = vector.new(0,-starlit.world.planet.gravity,0);
			node = node;
		})
	end);
	f.queue(0.3, function(s, timepast, timeleft)
		local function v(fn)
			local def = {
				amount = timeleft * 100;
				pos = pos;
				time = timeleft;
				radius = 0.5;
				jitter = {min = 0.0, max = 0.2};
				size = {min = 0.2, max = 0.5};
				exptime = {min = 0.5, max = 1};
				attract = {
					kind = 'point';
					strength = {min=0.3, max = 1};
					origin = vector.new(0,eh-0.1,0);
					radius = 0.5;
					origin_attached = user.entity;
				};
			}
			fn(def)
			f.visual(nil, def)
		end
		v(function(t) t.texpool = nanopool t.glow = 14 end)
		v(function(t)
			t.node = node
			t.amount = timeleft * 20
			t.size = {min = 0.1, max = 0.3};
		end)
	end)
	return f

end
