local RoleSystemExcel = require("excel.roleSystem")
local Msg = require("core.Msg")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local CommonDefine = require("common.CommonDefine")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")

local function RoleSystem_InitPrizeDB(human)
	human.db.roleSystemPrize = {}
	for id, conf in pairs(RoleSystemExcel.roleSystem) do
		if conf.isdisplay == 1 then
			human.db.roleSystemPrize[id] = CommonDefine.COMMON_PRIZE_STATE_NOGET
		end
	end
end

function RoleSystem_CheckPrize(human)
	if not human.db.roleSystemPrize then
		RoleSystem_InitPrizeDB(human)
	end

    local bSendDot = false
	for id, conf in pairs(RoleSystemExcel.roleSystem) do
		if conf.isdisplay == 1 then
			if not human.db.roleSystemPrize[id] then
				human.db.roleSystemPrize[id] = CommonDefine.COMMON_PRIZE_STATE_NOGET
			end

			if true == RoleSystemLogic.isOpen(human,conf.id) then
				local nState = human.db.roleSystemPrize[id]
				if CommonDefine.COMMON_PRIZE_STATE_NOGET == nState then
					human.db.roleSystemPrize[id] = CommonDefine.COMMON_PRIZE_STATE_CANGET
                    bSendDot = true
				end
			end
		end
	end

    if true == bSendDot then
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_110)
    end
end

function RoleSystem_QueryPrize(human)
	if not human.db.roleSystemPrize then
		RoleSystem_InitPrizeDB(human)
	end

	local tMsgData = Msg.gc.GC_ROLESYSTEM_PRIZE_QUERY
	tMsgData.tList[0] = 0
	local nLen = 0
	for id, conf in pairs(RoleSystemExcel.roleSystem) do 
		if conf.isdisplay == 1 then
			nLen = nLen + 1
			local tData = tMsgData.tList[nLen]
			tData.nID = id
			if not human.db.roleSystemPrize[id] then
				human.db.roleSystemPrize[id] = CommonDefine.COMMON_PRIZE_STATE_NOGET
			end
			tData.nStatus = human.db.roleSystemPrize[id]

			tData.tItemList[0] = #conf.reward
			for i, value in ipairs(conf.reward) do
				local tItemData = tData.tItemList[i]
				Grid.makeItem(tItemData, value[1], value[2])
			end
		end
	end
	tMsgData.tList[0] = nLen

	Msg.send(tMsgData, human.fd)
	print("[RoleSystem_QueryPrize] 下发活动预告奖励结束")
end

function RoleSystem_GetPrize(human)
	local tItem = {}
	for id, conf in pairs(RoleSystemExcel.roleSystem) do 
		if conf.isdisplay == 1 then
			local nStatus = human.db.roleSystemPrize[id] or CommonDefine.COMMON_PRIZE_STATE_NOGET
			if CommonDefine.COMMON_PRIZE_STATE_CANGET == nStatus then
				for _, v in ipairs(conf.reward) do
					if not tItem[v[1]] then
						tItem[v[1]] = 0
					end

					tItem[v[1]] = tItem[v[1]] + v[2]
				end

				human.db.roleSystemPrize[id] = CommonDefine.COMMON_PRIZE_STATE_GET
			end
		end
	end

	if nil ~= _G.next(tItem) then
		local tGoods = {}
		for nID, nNum in pairs(tItem) do
			table.insert(tGoods, {nID, nNum})
		end

		BagLogic.addItemList(human, tItem, "rolesystemprize")
		RoleSystem_QueryPrize(human)
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_110)
	end
end

function isDot(human)
    if not human.db.roleSystemPrize then
        return false
    end

    for id, conf in pairs(RoleSystemExcel.roleSystem) do
        if conf.isdisplay == 1 then
            if human.db.roleSystemPrize[id] and CommonDefine.COMMON_PRIZE_STATE_CANGET == human.db.roleSystemPrize[id] then
                return true
            end
        end
    end

    return false
end