-- <action actionid="4627" event="script" value="bosses/scarlet_boss.lua"/> adicionar no actions.xml
local posInicio = {x = 33385, y = 32638, z = 6} --pos inicio da sala
local posFim = {x = 33406, y = 32660, z = 6}--pos final inicio da sala
local posExitTime = {x = 33393, y = 32671, z = 6}-- pos para onde o player vai sair
local function verificaPlayers()
	for x = posInicio.x, posFim.x do
		for y = posInicio.y, posFim.y do
   			if isPlayer(getThingFromPos({x = x, y = y, z = posInicio.z, stackpos = 253}).uid) then
				return 1
			end
		end
	end
end
local function verificaBoss()
	for x = posInicio.x, posFim.x  do
		for y = posInicio.y, posFim.y do
			if isMonster(getThingFromPos({x = x, y = y, z = posInicio.z, stackpos = 253}).uid) then
				doRemoveCreature(getThingFromPos({x = x, y = y, z = posInicio.z, stackpos = 253}).uid)
			end
		end
	end
end
local function removePlayersTime()
	for x = posInicio.x, posFim.x  do
		for y = posInicio.y, posFim.y do
			local remove, clean = true, true
			local pos = {x = x, y = y, z = posInicio.z}
			local m = getTopCreature(pos).uid
			if m ~= 0 and isPlayer(m) then
				doTeleportThing(m, posExitTime)
			end
		end
	end
	doSendMagicEffect(posExitTime, 10)
	return true
end
function onUse(cid, item, frompos, item2, topos)
	-- Lever Boss
	if item.aid == 4627 then -- action id da alavanca
		local playerChecked = false
		local teleportToRoom = false
		local stoExhaust = 424148 -- staorage de segurança para exhaust
		local stoExhaustPlayer = getPlayerStorageValue(cid, stoExhaust)
		if stoExhaustPlayer > os.time() then
			doPlayerSendCancel(cid, "You have to wait " .. stoExhaustPlayer - os.time() .. " seconds to try again.")
			return true
		end
		if verificaPlayers() then
			doPlayerSendCancel(cid, "There are other players inside the room at the moment.")
			doSendMagicEffect(topos,2)
			setPlayerStorageValue(cid, stoExhaust, os.time()+5)
			return true
		end
		local config = {
			[1] = {pos = {x = 33395, y = 32661, z = 6}}, --posições dos pisos
			[2] = {pos = {x = 33394, y = 32662, z = 6}},
			[3] = {pos = {x = 33395, y = 32662, z = 6}},
			[4] = {pos = {x = 33396, y = 32662, z = 6}},
			[5] = {pos = {x = 33395, y = 32663, z = 6}},
		}
		local storageTime = 424149 -- storage de tempo do boss
		
		for i, v in pairs(config) do
			if isPlayer(getTopCreature(config[i].pos).uid) then
				player = getTopCreature(config[i].pos).uid
				if getPlayerStorageValue(player, storageTime) > os.time() then
					time = (getPlayerStorageValue(player, storageTime)-os.time())/60
					str = "minutes"
					if time > 60 then
						time = time/60
						str = "hours"
					end
					if player == cid then name = "You" else name = getCreatureName(player) end
					doPlayerSendCancel(cid, name .. " need to wait more " .. math.floor(time+0.5) .. " " .. str .. " to do again.")
					doSendMagicEffect(topos,2)
					return true
				end
				if player == cid then
					playerChecked = true
				end
			end
		end
		if playerChecked then
			for i, v in pairs(config) do
				if isPlayer(getTopCreature(config[i].pos).uid) then
					player = getTopCreature(config[i].pos).uid
					doSendMagicEffect(getCreaturePosition(player), 2)
					doTeleportThing(player, {x = 33396, y = 32656, z = 6}) -- posiçao que player vai cair
					setPlayerStorageValue(player, storageTime, os.time()+72000) -- tempo de 20 horas
					teleportToRoom = true
				end
			end
			if teleportToRoom then
				stopEvent(ResetArea)
				verificaBoss()
				doSendMagicEffect({x = 33396, y = 32642, z = 6}, 10) -- efeito que vai aparece no monstro quando ele nasce, mesma posiçao onde nasce o boss
				doCreateMonster("Scarlett Etzel", {x = 33396, y = 32642, z = 6}) -- onde o boss nasce e nome do boss
				ResetArea = addEvent(function()
					removePlayersTime()
				end, 10 * 60 * 1000) -- tempo dentro da sala para ser kikado tem tera 10 minutos para matar o boss ou sera teleportado de volta
				return false
			end
		else
			doPlayerSendCancel(cid, "Wouldn't you like to go too?")
			doSendMagicEffect(topos,2)
		end
		return true
	end
end