require "vl_quest_utils"
require "vl_quest_zombie_npc_utils"
require "vl_quest_globalmoddata"

if isServer() then return end

local GetPlayerGlobalModDataQ3 = VLGMD.GetPlayerGlobalModDataQ3
local SetPlayerGMD_Q3 = VLGMD.SetPlayerGMD_Q3
local GetNPCsByVarAndQStep = VLZB.GetNPCsByVarAndQStep
local GetZombieOnlineIDorUID = VLZB.GetZombieOnlineIDorUID
local KillNPCQuest = VLZB.KillNPCQuest
local SetNPCStateInQueueSpawnList = VLZB.SetNPCStateInQueueSpawnList
local DeactiveNPCSpawnedData = VLZB.DeactiveNPCSpawnedData
local GetQuestGlobalModData = VLGMD.GetQuestGlobalModData

--reset data khi Q3 kết thúc
function VLF.ResetData_EndQ3(player)
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, nil)
	for qStep, data in pairs(playerGMD_Quest_Q3) do
		if playerGMD_Quest_Q3[qStep] and playerGMD_Quest_Q3[qStep]["active"] == true then
			if qStep ~= "q3s100" and qStep ~= "q3s101" and qStep ~= "q3s102" and qStep ~= "q3s103" and qStep ~= "q3s104" and qStep ~= "q3s105" and qStep ~= "q3s106" then
				SetPlayerGMD_Q3(player, qStep, "active", "done")
			end
		end
	end
	
	--xóa queue tất cả NPC của Q3
	local questMD = GetQuestGlobalModData(nil)
	for _qStep, _ in pairs(questMD.NPCsQueueToSpawn) do
		if string.find(_qStep, "q3s") then
			SetNPCStateInQueueSpawnList({qStep = _qStep, active = nil})
		end
	end
end

function VLF.SetNPCDead(npcVariable, qStep, gunSound, setPosX, setPosY, setPosZ)
	local zNPCList = GetNPCsByVarAndQStep(npcVariable, qStep)
	if zNPCList and zNPCList[1] then

		local zID = GetZombieOnlineIDorUID(zNPCList[1])
		if zID then
			if setPosX then
				zNPCList[1]:setPosition(setPosX, setPosY, setPosZ)
			end
			
			if gunSound then
				zNPCList[1]:getEmitter():playSound(gunSound)
			end
			
			KillNPCQuest(qStep, zID)
			return true
		end
	end
	
	--Xóa data và queue bất kể có đang spawn hay không
	local questMD = GetQuestGlobalModData(nil)
	local zID = questMD.NPC[qStep]["zID"]
	DeactiveNPCSpawnedData(qStep, zID)
	SetNPCStateInQueueSpawnList({qStep = qStep, active = nil})
	return false
end
 
function VLF.IsQuest3Completing()
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(getPlayer(), "q3s100")
	if playerGMD_Quest_Q3["q3s100"] and playerGMD_Quest_Q3["q3s100"]["active"] ~= nil then
		return true
	end
	return false
end









