--======== Xử lý tất cả sendServerCommand từ Clients ========--
--KHÔNG hoạt động trong Singleplayer
--===========================================================--

--require "vl_quest_utils"
--require "vl_quest_zombie_npc_utils"
--require "vl_quest_player_utils"

assert(VLPL, "Stack trace: Missing dependency: VLPL - vl_onservercommand.lua")
assert(VLZB, "Stack trace: Missing dependency: VLZB - vl_onservercommand.lua")
--assert(VLIT, "Stack trace: Missing dependency: VLIT - vl_onservercommand.lua")
--assert(VLF,  "Stack trace: Missing dependency: VLF - vl_onservercommand.lua")

local SetZombieDefault = VLZB.SetZombieDefault
local GetPlayerGlobalModData = VLGMD.GetPlayerGlobalModData
local GetQuestGlobalModData = VLGMD.GetQuestGlobalModData
local GetZombieByOnlineIDOrUID = VLZB.GetZombieByOnlineIDOrUID
local GetZombieModDataQuest = VLZB.GetZombieModDataQuest
local NpcDoRifleShot = VLZB.NpcDoRifleShot
local IsCharExistAndAlive = VLZB.IsCharExistAndAlive
local DeactiveNPCSpawnedData = VLZB.DeactiveNPCSpawnedData
local ActiveNPCSpawnedData = VLZB.ActiveNPCSpawnedData


VLOSC["NPCQuestCM_C"] = VLOSC["NPCQuestCM_C"] or {}
VLOSC["NPCFirearmCM_C"] = VLOSC["NPCFirearmCM_C"] or {}

--================= ONSERVERCOMMAND =================--
VLOSC["NPCQuestCM_C"]["GetOrCreatePlayerGMD_C"] = function(args)	--args = playerGMD = GetPlayerGlobalModData(player)
	--cập nhật moddata global player từ server
	local player = getPlayer()
	if not player then return end
	local playerGMD = GetPlayerGlobalModData(player)
	for k, v in pairs(args) do
		playerGMD[k] = v
	end
end

VLOSC["NPCQuestCM_C"]["UpdateQuestModData_C"] = function(args)   --{qStep = qStep, modDataKey = modDataKey, val = val}
	local qStep = args.qStep
	local questMD = GetQuestGlobalModData(qStep)
	local modDataKey = args.modDataKey
	local val = args.val
	if modDataKey == "turnToZombie" then
		questMD.NPC[qStep][modDataKey] = val
	elseif modDataKey == "AddedObj1" then
		if not questMD.Object[qStep] then questMD.Object[qStep] = {} end
		questMD.Object[qStep][modDataKey] = val
	end
end

VLOSC["NPCQuestCM_C"]["SetDefaultZombie_C"] = function(args)   --{zID = zID}
	local zombie = GetZombieByOnlineIDOrUID(args.zID)
    if zombie then SetZombieDefault(zombie) end
end

VLOSC["NPCQuestCM_C"]["ActiveNPCInQueueSpawnCM_C"] = function(args)	  --{qStep = qStep, active = active}
	local qStep = args.qStep
	local questMD = GetQuestGlobalModData(qStep)
	questMD.NPCsQueueToSpawn[qStep] = args.active
end

VLOSC["NPCQuestCM_C"]["ActiveNPCSpawnedData_C"] = function(args)	--{x = x, y = y, z = z, qStep = qStep, zID = zID, zUID = zUID}
	ActiveNPCSpawnedData(args.x, args.y, args.z, args.qStep, args.zID, args.zUID)
end
  
VLOSC["NPCQuestCM_C"]["DeactiveNPCSpawnedData_C"] = function(args)	--{qStep = qStep, zID = zID}
	DeactiveNPCSpawnedData(args.qStep, args.zID)
end

VLOSC["NPCQuestCM_C"]["UpdateNPCCM_C"] = function(args)		--{qStep_Next = qStep_Next, zID = zID}
	local qStep_Next, zID = args.qStep_Next, args.zID
	local questMD = GetQuestGlobalModData(qStep_Next)
	questMD.NPC[qStep_Next]["spawned"] = true
	--questMD.NPC[qStep_Next]["uid"] = zombie:getUID() --dùng cho server kiểm tra zombie nếu không tồn tại thì xóa spawned, chỉ dùng UID vì tính duy nhất
	questMD.NPC[qStep_Next]["zID"] = zID
	questMD.NPCqStep[zID] = qStep_Next
end


--================= ONSERVERCOMMAND =================--
VLOSC["NPCFirearmCM_C"]["NPCFocusOnTarget_C"] = function(args)		--{npcID = npcID, targetID = targetID}
	local zNPC = GetZombieByOnlineIDOrUID(args.npcID)
	if not zNPC then return end
	local target = GetZombieByOnlineIDOrUID(args.targetID)
	if target then
		zNPC:faceThisObject(target)
	end            
	
	--NPC không tìm mục tiêu khác
	local zData = GetZombieModDataQuest(zNPC)
	zData.requestedNPCShootZ = true
end

VLOSC["NPCFirearmCM_C"]["RequestNPCShootZombie_C"] = function(args)	--{damage = damage, npcID = npcID, targetID = targetID, wpnFullType = wpnFullType, bumpType = bumpType, delay = delay, isHit = isHit}
	local zNPC = GetZombieByOnlineIDOrUID(args.npcID)
	local target = GetZombieByOnlineIDOrUID(args.targetID)
	
	--if zNPC and target then
	if IsCharExistAndAlive(zNPC) and IsCharExistAndAlive(target) then
		if zNPC:getActionStateName() == "idle" then
			zNPC:faceThisObject(target)
			local bumpType, bumpTypeCase = args.bumpType, args.bumpTypeCase
			local delay = args.delay
			--FIXME: Mỗi Client sẽ gây damage 1 lần? x client = x lần damage?
			NpcDoRifleShot(zNPC, target, args.wpnFullType, bumpType, bumpTypeCase, delay, args.damage, args.isHit)
		else
			local zData = GetZombieModDataQuest(zNPC)
			zData.requestedNPCShootZ = nil  --reset data trên client để NPC tìm mục tiêu mới
		end
	end
end

VLOSC["NPCFirearmCM_C"]["ResetFlags_C"] = function(args)	--{npcID = npcID, zDataKey = "requestedNPCShootZ"}
	local zNPC = GetZombieByOnlineIDOrUID(args.npcID)
	if zNPC then
		local zData = GetZombieModDataQuest(zNPC)
		zData[args.zDataKey] = nil
	end
end

local ServerCommand = VLOSC

--OnServerCommand KHÔNG CHẠY TRONG SINGLE PLAYER
local function OnServerCommand(module, command, args)
    local _module = ServerCommand[module]
    if not _module then return end

    local fn = _module[command]
    if fn then
        fn(args)
    end
end
Events.OnServerCommand.Remove(OnServerCommand)
Events.OnServerCommand.Add(OnServerCommand)














