-- original script by CJRed#6258, modified by Kaite#1102
function onCreate()
	initLuaShader('HARDLIGHTS')
end

local tweenAmount = 0.001
local funniDis = 75
local trailEnabled = false
function onEvent(name, value1, value2)
	if name == 'Toggle Special Trail' then
		if value1 ~= nil or value1 ~= '' then tweenAmount = value1 end
		if value2 ~= nil or value2 ~= '' then funniDis = value2 end
		trailEnabled = not trailEnabled
	end
end

function getIconColor(chr) return getColorFromHex(rgbToHex(getProperty(chr .. ".healthColorArray"))) end

function rgbToHex(array) return string.format('%.2x%.2x%.2x', math.min(array[1]+50,255), math.min(array[2]+50,255), math.min(array[3]+50,255)) end

function makeSmth(ea, ae, alph)
	local thing = 0
	thing = ae
	if not string.match(getProperty(ea..'.animation.curAnim.name'), 'sing') then
		thing = -1
	end
	for i = 1, 2 do
		if trailEnabled == true then ghostTrail(ea, {getProperty(ea..'.animation.frameName'), getProperty(ea..'.offset.x'), getProperty(ea..'.offset.y'), thing, getProperty(ea..'.animation.curFrame')}, alph) end
		thing = -1
	end
end

local chars = {}

function goodNoteHit(id, direction, noteType, isSustainNote)
	chars['boyfriend'] = direction
	makeSmth('boyfriend', direction, 1)
end

function opponentNoteHit(id, direction, noteType, isSustainNote)
	if getPropertyFromGroup('notes', id, 'gfNote') then
		chars['gf'] = direction
		makeSmth('gf', direction, 1)
	else
		chars['dad'] = direction
		makeSmth('dad', direction, 1)
	end
end

local ghostNum = 0
function ghostTrail(char, noteData, alph)
	-- debugPrint(char)
	local ghost = char..'Ghost'..ghostNum
	local group = char

	makeAnimatedLuaSprite(ghost, getProperty(char..'.imageFile'), getProperty(char..'.x'), getProperty(char..'.y'))
	addAnimationByPrefix(ghost, 'idle', noteData[1], 0, false)
	setProperty(ghost..'.antialiasing', getProperty(char..'.antialiasing'))
	setProperty(ghost..'.offset.x', noteData[2])
	setProperty(ghost..'.offset.y', noteData[3])
	setProperty(ghost..'.scale.x', getProperty(char..'.scale.x'))
	setProperty(ghost..'.scale.y', getProperty(char..'.scale.y'))
	setProperty(ghost..'.flipX', getProperty(char..'.flipX'))
	setProperty(ghost..'.flipY', getProperty(char..'.flipY'))
	setProperty(ghost..'.visible', getProperty(char..'.visible'))
	setProperty(ghost..'.color', getIconColor(char))
	setProperty(ghost..'.alpha', 0.75 * getProperty(char..'.alpha'))
	setSpriteShader(ghost, 'HARDLIGHT')
	setBlendMode(ghost, 'multiply')
	addLuaSprite(ghost)
	playAnim(ghost, 'idle', true, false, noteData[5])
	setObjectOrder(ghost, getObjectOrder(group..'Group') - 0.1)
	cancelTween(ghost)
	doTweenAlpha(ghost, ghost, 0, crochet*tweenAmount, 'linear')

	if noteData[4] == 0 then
		doTweenX(ghostNum..'', ghost, getProperty(char..'.x') - funniDis, crochet*tweenAmount, 'sineout')
	elseif noteData[4] == 1 then
		doTweenY(ghostNum..'', ghost, getProperty(char..'.y') + funniDis, crochet*tweenAmount, 'sineout')
	elseif noteData[4] == 2 then
		doTweenY(ghostNum..'', ghost, getProperty(char..'.y') - funniDis, crochet*tweenAmount, 'sineout')
	elseif noteData[4] == 3 then
		doTweenX(ghostNum..'', ghost, getProperty(char..'.x') + funniDis, crochet*tweenAmount, 'sineout')
	end

	ghostNum = ghostNum + 1
end

function onTweenCompleted(tag) if string.match(tag, 'Ghost') then removeLuaSprite(tag, true) end end