require "vl_quest_npc_server"

VLF = VLF or {}

local GetPlayerGlobalModDataQ1 = VLF.GetPlayerGlobalModDataQ1
local AddVehicle = VLF.AddVehicle

--Cập nhật quest step : true, false hoặc "done" vào Global ModData Player
local function SetQuestStepStateS(player, args)
	if not player then return end
	local qStep = args.qStep
	local active = args.active
	if qStep then
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, {qStep = 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 = 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 = 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
local function CreateAllQuestStepMD(player, args)
	if not player then return end
	local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, {qStep = nil})
	local maxStep = VLF.Quest.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

local function OnClientCommandQ1(module, command, player, args)
	if module == "MainQuest1" then
		if command == "UpdateQuestStep" then
			SetQuestStepStateS(player, args)
		elseif command == "CreAllQuestStepMD" then
			CreateAllQuestStepMD(player, args)
		elseif command == "UpdatePlayerGMD" then
			if args.qStep then
				local qStep = args.qStep
				local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, {qStep = qStep})
				if args.waitHourPassed and args.waitHourPassed == true then
					playerGMD_Quest_Q1[qStep].waitHourPassed = true
					playerGMD_Quest_Q1[qStep].startHourWait = nil
				end
			end
		elseif command == "UpdateAllPlayerGMD" then	
			if args then
				local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, {qStep = nil})
                for k, v in pairs(args) do
                    playerGMD_Quest_Q1[k] = v
                end
            end
		elseif command == "AddVehicleEvents" then
			if args.qStep == "q1s009" then
				AddVehicleEventq1s009()
			end
		end
	end
end
Events.OnClientCommand.Add(OnClientCommandQ1)
