local blockManager = require("blockManager")

local onlinePlay = require("scripts/onlinePlay")
local onlinePlayPlayers = require("scripts/onlinePlay_players")
local onlinePlayNPC = require("scripts/onlinePlay_npc")

local phantoCollection = require("maskCollection/scripts/phantoCollection")
local battlePhanto = require("scripts/battlePhanto")

local easing = require("ext/easing")


local phantoCollectionArea = {}
local blockID = BLOCK_ID

local phantoCollectionAreaSettings = {
	id = blockID,

	sizable = false,
	passthrough = true,
}

blockManager.setBlockSettings(phantoCollectionAreaSettings)


function phantoCollectionArea.onInitAPI()
    blockManager.registerEvent(blockID, phantoCollectionArea, "onTickBlock")
end


--local startAttractCommand = onlinePlay.createCommand("phantoCollectionArea_startAttract",onlinePlay.IMPORTANCE_MAJOR)


--[[local function canAttractNPC(npc)
	if not onlinePlayNPC.ownsNPC(npc) then
		return false
	end

	if not NPC.HITTABLE_MAP[npc.id] then
		return false
	end
	
	return true
end

local function startAttractingNPC(v,npc)
	local data = v.data

	if npc.heldIndex > 0 then
		Player(npc.heldIndex):mem(0x154,FIELD_WORD,0)
	end

	npc.heldIndex = 0

	table.insert(data.attractingNPCs,{
		npc = npc,
		timer = 0,

		startX = npc.x + npc.width*0.5,
		startY = npc.y + npc.height*0.5,
	})
end]]


function phantoCollectionArea.onTickBlock(v)
	if v.isHidden then
		return
	end

	local data = v.data

	data.attractingNPCs = data.attractingNPCs or {}

	for _,p in ipairs(Player.getIntersecting(v.x,v.y,v.x + v.width,v.y + v.height)) do
		if onlinePlayPlayers.ownsPlayer(p) and battlePhanto.isWearingMask(p) then
			phantoCollection.collectFromPlayer(p,vector(v.x + v.width*0.5,v.y - 16),vector(0,-1))
		--[[elseif p.holdingNPC ~= nil and canAttractNPC(p.holdingNPC) then
			startAttractingNPC(v,p.holdingNPC)

			if onlinePlay.currentMode ~= onlinePlay.MODE_OFFLINE and onlinePlayNPC.getUIDFromNPC(p.holdingNPC) ~= nil then
				startAttractCommand:send(0, v.idx,onlinePlayNPC.getUIDFromNPC(p.holdingNPC))
			end]]
		end
	end

	-- Attract NPCs
	--[[local i = 1

	while (data.attractingNPCs[i] ~= nil) do
		local npcData = data.attractingNPCs[i]
		local npc = npcData.npc

		if npc.isValid then
			npcData.timer = math.min(1,npcData.timer + 1/64)

			local riseTime = easing.inExpo(npcData.timer,0,1,1)
			local alignTime = easing.inOutQuad(math.min(1,npcData.timer*8),0,1,1)

			npc.speedX = math.lerp(npcData.startX,v.x + v.width*0.5,alignTime) - (npc.x + npc.width*0.5)
			npc.speedY = math.lerp(npcData.startY,v.y,riseTime) - (npc.y + npc.height*0.5)

			npc.friendly = true
			npc.isProjectile = true
			npc.noblockcollision = true

			if npcData.timer >= 1 and onlinePlayNPC.ownsNPC(npc) then
				Effect.spawn(147,npc.x + npc.width*0.5 - 16,npc.y + npc.height*0.5 - 16)
				SFX.play(55)

				onlinePlayNPC.forceKillNPC(npc,HARM_TYPE_VANISH)
				table.remove(data.attractingNPCs,i)
			else
				i = i + 1
			end
		else
			table.remove(data.attractingNPCs,i)
		end
	end]]
end


--[[function startAttractCommand.onReceive(sourcePlayerIdx, blockIdx,attractedNPCUID)
	local v = Block(blockIdx)
	local attractedNPC = onlinePlayNPC.getNPCFromUID(attractedNPCUID)

	if attractedNPC == nil then
		return
	end

	startAttractingNPC(v,attractedNPC)
end]]


return phantoCollectionArea