require "vl_quest_utils"
-- require "vl_quest_globalmoddata"

VLGMD = VLGMD or {}
VLF.var.Q1MaxStep = 60

local GetPlayerID

--Sử dụng OnGameBoot để trỏ tới function file khác. THÌ KHÔNG CẦN REQUIRE file
local function OnGameBoot()
    GetPlayerID = VLPL.GetPlayerID
end
Events.OnGameBoot.Add(OnGameBoot)

local function OnInitGlobalModData(newGame)
	local questMD = ModData.getOrCreate("VL_Quest")
	if not questMD.NPC then questMD.NPC = {} end    --reset khi xong quest
    if not questMD.NPCsQueueToSpawn then questMD.NPCsQueueToSpawn = {} end  --reset khi xong quest
    if not questMD.NPCqStep then questMD.NPCqStep = {} end  --reset khi xong quest
    if not questMD.Object then questMD.Object = {} end  --KHÔNG reset khi xong quest
    if not questMD.Events then questMD.Events = {} end
	if not questMD.QData then questMD.QData = {} end
	
    if isClient() then
        ModData.request("VL_Quest")
    end
end
Events.OnInitGlobalModData.Add(OnInitGlobalModData)

--Lấy Global ModData VL_Quest
function VLGMD.GetQuestGlobalModData(qStep) --local questMD = VLGMD.GetQuestGlobalModData(nil)
    local questMD = ModData.getOrCreate("VL_Quest")
    
    --local qStep = args.qStep
    if qStep and questMD.NPC then
        if not questMD.NPC[qStep] then questMD.NPC[qStep] = {} end
    end
    return questMD
end

--Lấy Global ModData Player 	--playerGMD
function VLGMD.GetPlayerGlobalModData(player) --local playerGMD = VLGMD.GetPlayerGlobalModData(player)
	if not player then return nil end
	
	local pID = GetPlayerID(player)
	local playerGMD = ModData.getOrCreate("VL_Player_"..tostring(pID))
	
	if not playerGMD.Quest then playerGMD.Quest = {} end
	if not playerGMD.Data then playerGMD.Data = {} end
	if not playerGMD.Event then playerGMD.Event = {} end
	
	if not playerGMD.Data["ReputationPoint"] then playerGMD.Data["ReputationPoint"] = 0 end
	
	return playerGMD
end
local GetPlayerGlobalModData = VLGMD.GetPlayerGlobalModData

--YÊU CẦU GLOBAL MODDATA PLAYER TỪ SERVER KHI ĐĂNG NHẬP VÀO GAME --nếu gọi ở lúc khởi tạo ModData thì lúc đó chưa có player
local function CreateGlobalModDataPlayer(playerNum, player)
    local playerGMD = GetPlayerGlobalModData(player)
    sendClientCommand(player, 'NPCQuestCM', 'GetOrCreatePlayerGMD', {pID = nil})
end
if not isServer() then
	Events.OnCreatePlayer.Remove(CreateGlobalModDataPlayer)
	Events.OnCreatePlayer.Add(CreateGlobalModDataPlayer)
end

--Lấy Global ModData Player Quest Q1	-- playerGMD.Quest.Q1
function VLGMD.GetPlayerGlobalModDataQ1(player, qStep) --local playerGMD_Quest_Q1 = VLGMD.GetPlayerGlobalModDataQ1(player, qStep)
	if not player then return nil end
	
	local playerGMD = GetPlayerGlobalModData(player)
	if not playerGMD.Quest.Q1 then playerGMD.Quest.Q1 = {} end
	 
	if qStep then
		if not playerGMD.Quest.Q1[qStep] then playerGMD.Quest.Q1[qStep] = {} end
	end
	
	return playerGMD.Quest.Q1
end
local GetPlayerGlobalModDataQ1 = VLGMD.GetPlayerGlobalModDataQ1

--Lấy Global ModData Player Event-- playerGMD.Quest.Q1
function VLGMD.GetPlayerGlobalModDataEvent(player, qStep) --local playerGMD_Event = VLGMD.GetPlayerGlobalModDataEvent(player, qStep)
	if not player then return nil end
	
	local playerGMD = GetPlayerGlobalModData(player)
	if not playerGMD.Event then playerGMD.Event = {} end
	
	if qStep then
		if not playerGMD.Event[qStep] then playerGMD.Event[qStep] = {} end
	end
	
	return playerGMD.Event
end
local GetPlayerGlobalModDataEvent = VLGMD.GetPlayerGlobalModDataEvent

--Set GlobalModData Quest của Player và gửi yêu cầu cập nhật tới Server nếu là Client 	--playerGMD.Quest
function VLGMD.SetPlayerGMD_Quest(player, questKey, qStep, modDataKey, val)
	if not player then return end
	
	local playerGMD = GetPlayerGlobalModData(player)
	if not playerGMD.Quest[questKey] then playerGMD.Quest[questKey] = {} end
	if not playerGMD.Quest[questKey][qStep] then playerGMD.Quest[questKey][qStep] = {} end
	
	playerGMD.Quest[questKey][qStep][modDataKey] = val
	
	if isClient() then
		sendClientCommand(player, "MainQuest1", "UpdatePlayerGMD", {key = "Quest", questKey = questKey, qStep = qStep, modDataKey = modDataKey, val = val})
	end
end
 
--Set GlobalModData Quest 1 của Player và gửi yêu cầu cập nhật tới Server nếu là Client 	--playerGMD_Quest_Q1
function VLGMD.SetPlayerGMD_Q1(player, qStep, modDataKey, val)
	if not player then return end
	
	local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
	playerGMD_Quest_Q1[qStep][modDataKey] = val
	if isClient() then
		sendClientCommand(player, "MainQuest1", "UpdatePlayerGMD", {qStep = qStep, modDataKey = modDataKey, val = val})
	end
end
 
--Set GlobalModData Data của Player và gửi yêu cầu cập nhật tới Server nếu là Client	--playerGMD.Data
function VLGMD.SetPlayerGMD_Data(player, modDataKey, val)
	if not player then return end
	
	local playerGMD = GetPlayerGlobalModData(player)
	playerGMD.Data[modDataKey] = val
	if isClient() then
		sendClientCommand(player, "MainQuest1", "UpdatePlayerGMD", {key = "Data", modDataKey = modDataKey, val = val})
	end
end
local SetPlayerGMD_Data = VLGMD.SetPlayerGMD_Data
 
--Set GlobalModData Event của Player và gửi yêu cầu cập nhật tới Server nếu là Client	--playerGMD.Event.qStep
function VLGMD.SetPlayerGMD_Event(player, qStep, modDataKey, val)
	if not player then return end
	local playerGMD_Event = GetPlayerGlobalModDataEvent(player, qStep)
	
	playerGMD_Event[qStep][modDataKey] = val
	if isClient() then
		sendClientCommand(player, "MainQuest1", "UpdatePlayerGMD", {key = "Event", qStep = qStep, modDataKey = modDataKey, val = val})
	end
end

--Clear table data trong GlobalModData của Player
function VLGMD.ClearTablePlayerGMD(player, isInner, outerTabKey, clearTabKey)	--VLGMD.ClearTablePlayerGMD(player, false, nil, clearTabKey)
	if not player then return end
	local playerGMD = GetPlayerGlobalModData(player)
	
	--Nếu table cần clear nằm bên trong 1 table khác
	if isInner and isInner == true then
		playerGMD[outerTabKey][clearTabKey] = nil
	else
		--table cần clear nằm ngoài cùng
		playerGMD[clearTabKey] = nil
	end
		
	if isClient() then
		sendClientCommand(player, "MainQuest1", "ClearTableInPlayerGMD", {isInner = isInner, outerTabKey = outerTabKey, clearTabKey = clearTabKey})
	end
end


--=============== Bob and Kate Quest ==============--
--Lấy Global ModData Player Quest Q3	-- playerGMD.Quest.Q3	--Bob And Kate Questline
function VLGMD.GetPlayerGlobalModDataQ3(player, qStep) --local playerGMD_Quest_Q3 = VLGMD.GetPlayerGlobalModDataQ3(player, qStep)
	if not player then return nil end
	
	local playerGMD = GetPlayerGlobalModData(player)
	if not playerGMD.Quest.Q3 then playerGMD.Quest.Q3 = {} end
	 
	if qStep then
		if not playerGMD.Quest.Q3[qStep] then playerGMD.Quest.Q3[qStep] = {} end
	end
	
	return playerGMD.Quest.Q3
end
local GetPlayerGlobalModDataQ3 = VLGMD.GetPlayerGlobalModDataQ3

--Set GlobalModData Quest 3 của Player và gửi yêu cầu cập nhật tới Server nếu là Client 	--playerGMD_Quest_Q3
function VLGMD.SetPlayerGMD_Q3(player, qStep, key, val)
	if not player then return end
	
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
	playerGMD_Quest_Q3[qStep][key] = val
	if isClient() then
		sendClientCommand(player, "BobAndKate", "UpdatePlayerGMDQ3", {qStep = qStep, key = key, val = val})
	end
end
 
--=============== Reputation Point ==============--
function VLGMD.GetReputationPoint(player)
	if not player then return end
	
	local playerGMD = GetPlayerGlobalModData(player)
	return playerGMD.Data["ReputationPoint"]
end

function VLGMD.AddReputationPoint(player, pointToAdd)
	if not player then return end
	
	local playerGMD = GetPlayerGlobalModData(player)
	repPoint = playerGMD.Data["ReputationPoint"] + pointToAdd
	
	SetPlayerGMD_Data(player, "ReputationPoint", repPoint)
end






