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

local battleGeneral = require("scripts/battleGeneral")
local battlePlayer = require("scripts/battlePlayer")
local battleHUD = require("scripts/battleHUD")
local battleBombs = require("scripts/battleBombs")

local onlinePlayPlayers = require("scripts/onlinePlay_players")


local bombBag = {}
local npcID = NPC_ID

local bombBagSettings = {
	id = npcID,

	gfxwidth = 32,
	gfxheight = 44,

	width = 32,
	height = 32,

	gfxoffsetx = 0,
	gfxoffsety = 2,

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

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

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

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

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

	isinteractable = true,

	isstationary = true,


	baseRespawnTime = 64*15,
	maxPlayersRespawnTimeMultiplier = 0.2,

	playerRespawnPreventionDistance = 32,
}

npcManager.setNpcSettings(bombBagSettings)
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,
	}
)


battleBombs.bagNPCID = npcID


function bombBag.onInitAPI()
	npcManager.registerEvent(npcID, bombBag, "onTickNPC")

	registerEvent(bombBag, "onPostNPCCollect")
end


local function getRespawnTime()
	local config = NPC.config[npcID]

	local playerCount = battlePlayer.getActivePlayerCount()

	if playerCount > 2 then
		return config.baseRespawnTime*math.lerp(1,config.maxPlayersRespawnTimeMultiplier, math.invlerp(2,battleGeneral.maxOnlinePlayers,playerCount))
	else
		return config.baseRespawnTime
	end
end

local function playerIsNearby(v)
	local config = NPC.config[npcID]

	local margin = config.playerRespawnPreventionDistance

	for _,p in ipairs(Player.getIntersecting(v.x - margin,v.y - margin,v.x + v.width + margin,v.y + v.height + margin)) do
		if battlePlayer.getPlayerIsActive(p) then
			return true
		end
	end

	return false
end


function bombBag.onTickNPC(v)
	local data = v.data

	if v.despawnTimer <= 0 then
		if v.spawnId == 0 or v.isHidden then
			return
		end

		if v:mem(0x06,FIELD_WORD) > 0 then
			if not data.setRespawnTime then
				v:mem(0x06,FIELD_WORD,getRespawnTime())
				data.setRespawnTime = true
			end
		else
			if (not v:mem(0x126,FIELD_BOOL) or not v:mem(0x128,FIELD_BOOL)) and not playerIsNearby(v) then
				Effect.spawn(10,v.x + v.width*0.5 - 16,v.y + v.height*0.5 - 16)

				v.despawnTimer = Defines.npc_despawntimer
				v:mem(0x124,FIELD_BOOL,true)
				
				v:mem(0x126,FIELD_BOOL,true)
				v:mem(0x128,FIELD_BOOL,true)
			end

			data.setRespawnTime = false
		end

		return
	end

	data.setRespawnTime = false
end


function bombBag.onPostNPCCollect(npc,p)
	if npc.id ~= npcID then
		return
	end

	local playerData = battlePlayer.getPlayerData(p)

	local newBombCount = math.min(battleBombs.maxBombs,playerData.bombs + battleBombs.bagBombCount)

	if newBombCount > playerData.bombs then
		battlePlayer.addPointEffect(p,newBombCount - playerData.bombs,battleHUD.bombsImage)
		playerData.bombs = newBombCount

		battleBombs.displayTip(p)
	end

	if onlinePlayPlayers.canMakeSound(p) then
		SFX.play(12)
	end
end


return bombBag