require "vl_quest_npc_server"

VLOCC["MainQuest1"] = VLOCC["MainQuest1"] or {}

assert(VLF,  "Stack trace: Missing dependency: VLF - vl_quest1_onserver.lua")

local GetPlayerGlobalModDataQ1 = VLGMD.GetPlayerGlobalModDataQ1
local AddVehicle = VLF.AddVehicle
local GetPlayerGlobalModData = VLGMD.GetPlayerGlobalModData
local SetPlayerGMD_Data = VLGMD.SetPlayerGMD_Data
local SetPlayerGMD_Event = VLGMD.SetPlayerGMD_Event
local SetPlayerGMD_Q1 = VLGMD.SetPlayerGMD_Q1
local ClearTablePlayerGMD = VLGMD.ClearTablePlayerGMD
local SetPlayerGMD_Quest = VLGMD.SetPlayerGMD_Quest

--Cập nhật quest step : true, false hoặc "done" vào Global ModData Player
local function SetQuestStepStateS(player, args)
	if not player or not args then return end
	local qStep = args.qStep
	local active = args.active
	if qStep then
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		playerGMD_Quest_Q1[qStep].active = args.active
		--print("Server-----------SetQuestStepStateS 111= ", qStep, playerGMD_Quest_Q1[qStep].active)
		--không cần sendServerCommand vì client đã cập nhật trước rồi mới gửi tới server
	end
	if args.qStep_isAddedCorpses then
		local qStep = args.qStep_isAddedCorpses
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Server-----------SetQuestStepStateS_isAddedCorpses= ", args.qStep_isAddedCorpses, args.isAddedCorpses)
		playerGMD_Quest_Q1[qStep].isAddedCorpses = args.isAddedCorpses
	end
	if args.qStep_isNPC then
		local qStep = args.qStep_isNPC
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Server-----------SetQuestStepStateS_isNPC= ", args.qStep_isNPC, args.active_isNPC)
		playerGMD_Quest_Q1[qStep].isNPC = args.active_isNPC
	end
end

--Spawn Vehicle Event q1s009
local function AddVehicleEventq1s009()
	--print("Server-----------AddVehicleEventq1s009") 
	AddVehicle({vehicleName = "Base.StepVan_LouisvilleSWAT", x = 13520, y = 4857, z = 0, angleX = 0, angleY = 180, angleZ = 0, damage = 270, crashFont = true, crashBack = true})
	AddVehicle({vehicleName = "Base.CarLightsMuldraughPolice", x = 13520, y = 4854, z = 0, angleX = 0, angleY = 90, angleZ = 270, damage = 100, crashFont = true, crashBack = true})
	AddVehicle({vehicleName = "Base.CarLightsLouisvilleCounty", x = 13516, y = 4860, z = 0, angleX = 0, angleY = 200, angleZ = 0, damage = 200, crashFont = true, crashBack = false})					
	AddVehicle({vehicleName = "Base.CarNormalBurnt", x = 13520, y = 4867, z = 0, angleX = 0, angleY = 163, angleZ = 0})					
	AddVehicle({vehicleName = "Base.CarLightsLouisvilleCounty", x = 13523, y = 4862, z = 0, angleX = 0, angleY = 193, angleZ = 0, damage = 200, crashFont = true, crashBack = true})					
	AddVehicle({vehicleName = "Base.CarLightsLouisvilleCounty", x = 13523, y = 4866, z = 0, angleX = 0, angleY = 167, angleZ = 0, damage = 188, crashFont = true, crashBack = false})
end
	
--Khởi tạo tất cả GMD qStep player
local function CreateAllQuestStepMD(player, args)
	if not player then return end
	local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, nil)
	if playerGMD_Quest_Q1.q1s000 and playerGMD_Quest_Q1.q1s000.CreQStepMD == true then return end --Chỉ tạo 1 lần, tránh spam
	local maxStep = VLF.var.Q1MaxStep
	local step = "q1s"
	for i = 0, maxStep do
		local qStep = ""
		if i < 10 then qStep = step.."00"..tostring(i)
		elseif i >= 10 and i <= 99 then qStep = step.."0"..tostring(i)
		else qStep = step..tostring(i) end

		if not playerGMD_Quest_Q1[qStep] then playerGMD_Quest_Q1[qStep] = {} end
	end
	playerGMD_Quest_Q1.q1s000.CreQStepMD = true
end


--================= ONCLIENTCOMMAND =================--
VLOCC["MainQuest1"]["UpdateQuestStep"] = function(player, args)
	SetQuestStepStateS(player, args)
end

VLOCC["MainQuest1"]["CreAllQuestStepMD"] = function(player, args)
	CreateAllQuestStepMD(player, args)
end

--Cập nhật Global ModData của player. Chỉ nhận để cập nhật trên Server, KHÔNG transmit cho các client khác
VLOCC["MainQuest1"]["UpdatePlayerGMD"] = function(player, args)
	if args and args.qStep then
		player = player or getPlayer()

		if key == "waitHourPass" then		--{qStep = qStep, key = "waitHourPass", waitHourPassed = true, startHourWait = nil}
			local qStep, key = args.qStep, args.key
			local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
			local waitHourPassed, startHourWait = args.waitHourPassed, args.startHourWait
			
			if waitHourPassed then
				playerGMD_Quest_Q1[qStep].waitHourPassed = waitHourPassed
			end
			if startHourWait then
				playerGMD_Quest_Q1[qStep].startHourWait = startHourWait
			else
				playerGMD_Quest_Q1[qStep].startHourWait = nil
			end
			  
		elseif key == "Data" then		--{qStep = qStep, key = "Data", modDataKey = "event_q1s035", val = true}
			SetPlayerGMD_Data(player, args.modDataKey, args.val)
			 
		elseif key == "Event" then		--{key = "Event", qStep = qStep, modDataKey = modDataKey, val = val}
			SetPlayerGMD_Event(player, args.qStep, args.modDataKey, args.val)
		
		elseif key == "Quest" then		--{key = "Quest", questKey = questKey, qStep = qStep, modDataKey = modDataKey, val = val}
			SetPlayerGMD_Quest(player, args.questKey, args.qStep, args.modDataKey, args.val)
		 
		else			--{qStep = qStep, modDataKey = "activeQStepEvent", val = true}
			SetPlayerGMD_Q1(player, args.qStep, args.modDataKey, args.val)
		end
	end
end

--Clear 1 table trong Global ModData của player. Chỉ nhận để cập nhật trên Server, KHÔNG transmit cho các client khác
VLOCC["MainQuest1"]["ClearTableInPlayerGMD"] = function(player, args)
	ClearTablePlayerGMD(player, args.isInner, args.outerTabKey, args.clearTabKey)
end

VLOCC["MainQuest1"]["CollectQuestItemPGMD"] = function(player, args)	--{qStep = qStep, dataName = dataName, collected = nil, request = nil}
	if args then
		local qStep, dataName, collected, request = args.qStep, args.dataName, args.collected, args.request
		if qStep and dataName then
			local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
			local collectedKey = "collected" .. dataName
			local requestKey   = "request" .. dataName
			if collected then
				playerGMD_Quest_Q1[qStep][collectedKey] = collected
			else
				playerGMD_Quest_Q1[qStep][collectedKey] = nil
			end
			if collected then
				playerGMD_Quest_Q1[qStep][requestKey] = request
			else
				playerGMD_Quest_Q1[qStep][requestKey] = nil
			end
		end
	end
end

VLOCC["MainQuest1"]["AddVehicleEvents"] = function(player, args)	
	if args.qStep == "q1s009" then
		AddVehicleEventq1s009()
	end
end
