-- 成神之路-冲级奖励
local Util = require("common.Util")
local CommonDB = require("common.CommonDB")
local OpenActExcel = require("excel.openAct")
local Msg = require("core.Msg")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local OpenAct = require("present.OpenAct")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")
local GuideLogic = require("guide.GuideLogic")

local MSGMAXLEN = 20 -- GC_PRESENT_CHONGJI_QUERY 协议 一次下发数据长度

function getLeftTime()
    return 0
end

function isOpen()
	return true 
end

function query(human)
	if isOpen(human) ~= true then
		return
	end
	-- local chongJi = human.db.chongJi or {}
	-- local msgRet = Msg.gc.GC_PRESENT_CHONGJI_QUERY
	-- msgRet.lv = human.db.lv	
	-- for i = 1, #OpenActExcel.chongJi do
	-- 	local config = OpenActExcel.chongJi[i]
    --     msgRet.list[i].id = i
    --     msgRet.list[i].maxLv = config.lv
    --     msgRet.list[i].desc = Util.format(config.desc, config.lv)
	-- 	msgRet.list[i].state = 0
	-- 	if chongJi[i] then msgRet.list[i].state = 1 end
    --     for j = 1, #config.reward do
    --         local itemID = config.reward[j][1]
    --         local itemCnt = config.reward[j][2]
    --         Grid.makeItem(msgRet.list[i].item[j], itemID, itemCnt)
    --     end
    --     msgRet.list[i].item[0] = #config.reward
    -- end
    -- msgRet.list[0] = #OpenActExcel.chongJi	
	-- Msg.send(msgRet, human.fd)


	local allLen = #OpenActExcel.chongJi
	local allTimes = math.ceil(allLen / MSGMAXLEN)

	local realIndex = 1
	local chongJiCfg = OpenActExcel.chongJi
	local chongJi = human.db.chongJi or {}

	for k=1, allTimes do
		local msgRet = Msg.gc.GC_PRESENT_CHONGJI_QUERY
		local nowLen = allLen > MSGMAXLEN and MSGMAXLEN or allLen

		msgRet.list[0] = nowLen
		msgRet.lv = human.db.lv
		msgRet.isEnd = k < allTimes and 0 or 1

		for i = 1, nowLen do
			local config = chongJiCfg[realIndex]
			msgRet.list[i].id = realIndex
			msgRet.list[i].maxLv = config.lv
			msgRet.list[i].desc = Util.format(config.desc, config.lv)
			msgRet.list[i].state = 0

			if chongJi[realIndex] then
				msgRet.list[i].state = 1
			end
			for j = 1, #config.reward do
				local itemID = config.reward[j][1]
				local itemCnt = config.reward[j][2]
				Grid.makeItem(msgRet.list[i].item[j], itemID, itemCnt)
			end
			msgRet.list[i].item[0] = #config.reward
			realIndex = realIndex + 1
		end
		Msg.send(msgRet,human.fd)
		allLen = allLen - MSGMAXLEN
	end

end

function getChongJiReward(human, id)
	if isOpen(human) ~= true then
		return
	end

	local config = OpenActExcel.chongJi[id]
	if not config then return end
	if human.db.lv < config.lv then return end

	local chongJi = human.db.chongJi or {}
	if chongJi[id] then 
		return
	end

	-- 改db
	human.db.chongJi = human.db.chongJi or {}
	human.db.chongJi[id] = 1

	BagLogic.addItemList(human, config.reward, "chongJi")

	query(human)
    for k, v in pairs(funcID) do
        YunYingLogic.updateIcon(YYInfo[k], human)
        YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3505)
        break
    end
    GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_LEVEL_GIFT)
end

function isRed(human)
	local lv = human.db.lv
	local chongJi = human.db.chongJi or {}
	for i, config in ipairs(OpenActExcel.chongJi) do
		if lv >= config.lv and not chongJi[i] then
			return true
		end
	end
end

function onLevelUp(human)
    for k, v in pairs(funcID) do
		YunYingLogic.updateIcon(YYInfo[k], human)
        YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3505)
        break
    end
end