local onlinePlayNPC = require("scripts/onlinePlay_npc")

local tilemap = require("maskCollection/scripts/tilemap")

local npcManager = require("npcManager")
local npcutils = require("npcs/npcutils")

local lankyFoot = {}
local npcID = NPC_ID

local lankyFootSettings = {
	id = npcID,

	gfxwidth = 128,
	gfxheight = 32,

	width = 128,
	height = 32,

	gfxoffsetx = 0,
	gfxoffsety = 0,

	frames = 1,
	framestyle = 0,
	framespeed = 8,

	luahandlesspeed = false,
	nowaterphysics = false,
	cliffturn = false,
	staticdirection = false,

	npcblock = true,
	npcblocktop = false,
	playerblock = true,
	playerblocktop = true,

	nohurt = true,
	nogravity = true,
	noblockcollision = true,
	notcointransformable = false,
	nofireball = true,
	noiceball = true,
	noyoshi = true,

	jumphurt = false,
	spinjumpsafe = false,
	harmlessgrab = false,
	harmlessthrown = false,
	nowalldeath = false,

	linkshieldable = false,
	noshieldfireeffect = false,

	grabside = false,
	grabtop = false,

	terminalvelocity = -1,

	foreground = true,


	crushSpeed = 3,
}

npcManager.setNpcSettings(lankyFootSettings)
npcManager.registerHarmTypes(npcID,
	{
		--HARM_TYPE_JUMP,
		--HARM_TYPE_FROMBELOW,
		--HARM_TYPE_NPC,
		--HARM_TYPE_PROJECTILE_USED,
		--HARM_TYPE_LAVA,
		--HARM_TYPE_HELD,
		--HARM_TYPE_TAIL,
		--HARM_TYPE_SPINJUMP,
		--HARM_TYPE_OFFSCREEN,
		--HARM_TYPE_SWORD
	}, 
	{
		--[HARM_TYPE_JUMP]=10,
		--[HARM_TYPE_FROMBELOW]=10,
		--[HARM_TYPE_NPC]=10,
		--[HARM_TYPE_PROJECTILE_USED]=10,
		--[HARM_TYPE_LAVA]={id=13, xoffset=0.5, xoffsetBack = 0, yoffset=1, yoffsetBack = 1.5},
		--[HARM_TYPE_HELD]=10,
		--[HARM_TYPE_TAIL]=10,
		--[HARM_TYPE_SPINJUMP]=10,
		--[HARM_TYPE_OFFSCREEN]=10,
		--[HARM_TYPE_SWORD]=10,
	}
)


onlinePlayNPC.onlineHandlingConfig[npcID] = {
	synchronisationType = onlinePlayNPC.SYNCHRONISATION.NO_UPDATES,

	shouldStealFunc = function(v)
		return false
	end,
}


function lankyFoot.onInitAPI()
	npcManager.registerEvent(npcID, lankyFoot, "onTickNPC")
end


function lankyFoot.onTickNPC(v)
	if Defines.levelFreeze then return end

	local data = v.data

	if v.despawnTimer <= 0 or v.friendly then
		return
	end

	local config = NPC.config[v.id]

	-- Player interaction
	for _,p in ipairs(Player.getIntersecting(v.x,v.y + v.height,v.x + v.width,v.y + v.height + 16)) do
		local speedY = p.speedY

		if p:mem(0x48,FIELD_WORD) > 0 then
			local slopeBlock = Block(p:mem(0x48,FIELD_WORD))
			local slopeConfig = Block.config[slopeBlock.id]

			speedY = p.speedX*slopeBlock.height/slopeBlock.width*slopeConfig.floorslope
		end

		-- Slightly better collision on the bottom
		if (p.y + speedY - 0.1) <= (v.y + v.height + math.min(0,v.speedY)) and (p.y - math.min(0,speedY) + 4) >= (v.y + v.height) then
			p.y = v.y + v.height
			p.speedY = v.speedY + 0.1
			p:mem(0x11C,FIELD_WORD,0)

			local collider = Colliders.getHitbox(p)

			collider.x = collider.x + p.speedX
			collider.height = collider.height + 1

			local block = tilemap.getTouchingSolidBlock(collider)

			if block ~= nil then
				local blockConfig = Block.config[block.id]

				if blockConfig.floorslope < 0 then
					p.x = block.x + block.width*(1 - math.clamp((p.y + p.height - block.y)/block.height)) - p.width - 1
					p.speedX = 0
				elseif blockConfig.floorslope > 0 then
					p.x = block.x + block.width*math.clamp((p.y + p.height - block.y)/block.height) + 1
					p.speedX = 0
				end
			end
		end

		-- Crushing
		if v.speedY >= config.crushSpeed and (p.y - speedY) >= (v.y + v.height - v.speedY) and p.y <= (v.y + v.height) then
			local crushCollider = Colliders.Box(p.x,p.y + p.height,p.width,1)
			
			if tilemap.getTouchingSolidBlock(crushCollider) ~= nil then
				p:harm()
			end
		end
	end

	-- Kill NPCs
	if v.speedY >= config.crushSpeed then
		for _,npc in NPC.iterateIntersecting(v.x + 4,v.y + v.height,v.x + v.width - 4,v.y + v.height + 4) do
			if NPC.HITTABLE_MAP[npc.id] and not NPC.config[npc.id].noblockcollision and not npc.noblockcollision and npc.heldIndex == 0 then
				npc:kill(HARM_TYPE_NPC)
			end
		end
	end
end


return lankyFoot