---------------------------------------------------
-- 充值-每周/每月礼包
-- 开服起每x天为1期

-- db.richangLibao[libaoType] = libaoData
-- libaoData.ts 				购买时间戳	
-- libaoData.list				购买记录 [id] = cnt
---------------------------------------------------

local PresentExcel = require("excel.present")
local Util = require("common.Util")
local Lang = require("common.Lang")
local CommonDB = require("common.CommonDB")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local PanelDefine = require("broadcast.PanelDefine")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local BuyLogic = require("topup.BuyLogic")
local YunYingLogic = require("yunying.YunYingLogic")

LIBAOTYPE_WEEK = 1 			-- 每周礼包
LIBAOTYPE_MONTH = 2 		-- 每月礼包

FREE_BUY = 1 				-- 免费领取
RMB_BUY = 2 				-- rmb购买

-- 根据类型获取配置
function getConfigs(libaoType)
	if libaoType == LIBAOTYPE_WEEK then
		return PresentExcel.weekLibao
	elseif libaoType == LIBAOTYPE_MONTH then
		return PresentExcel.monthLibao
	end
end

-- 获取刷新周期 x天
function getRefreshDay(libaoType)
	if libaoType == LIBAOTYPE_WEEK then
		return 7
	elseif libaoType == LIBAOTYPE_MONTH then
		return 30
	end
end

-- 
function getLogType(libaoType)
	if libaoType == LIBAOTYPE_WEEK then
		return "week_libao"
	elseif libaoType == LIBAOTYPE_MONTH then
		return "month_libao"
	end
end

-- 获取本期礼包开始和结束时间
function getLibaoTime(libaoType)
	local openDay = CommonDB.getServerOpenDay()
	if not openDay then return end -- 没有设置开服时间 不开活动哦
	if openDay < 1 then return end -- 开服时间不对 也不开

	local refreshDay = getRefreshDay(libaoType)
	if not refreshDay then return end

	local day = openDay % refreshDay -- 本期礼包 第x天
	if day == 0 then
		day = refreshDay
	end
	local dayStartTime = Util.getDayStartTime(os.time())
	local startTime = dayStartTime - (day - 1) * 86400
	local endTime = startTime + refreshDay * 86400
	return startTime, endTime
end

-- 检查是否需要刷新
function checkDirty(human)
	if not human.db.richangLibao then
		return 
	end

	for libaoType, libaoData in pairs(human.db.richangLibao) do
		local startTime, endTime = getLibaoTime(libaoType)
		if (startTime and libaoData.ts < startTime) or 
			(endTime and libaoData.ts > endTime) then			
			human.db.richangLibao[libaoType] = nil
		end
	end
end

-- 获取已购买次数
function getBuyCnt(human, libaoType, id)
	if not human.db.richangLibao then
		return 0
	end
	local libaoData = human.db.richangLibao[libaoType]
	if not libaoData then
		return 0
	end
	return libaoData.list[id] or 0
end

-- 增加已购买次数
function addBuyCnt(human, libaoType, id)
	if not human.db.richangLibao then
		human.db.richangLibao = {}
	end
	local libaoData = human.db.richangLibao[libaoType]
	if not libaoData then
		libaoData = {}
		libaoData.ts = os.time()
		libaoData.list = {}
		human.db.richangLibao[libaoType] = libaoData
	end
	libaoData.list[id] = (libaoData.list[id] or 0) + 1
end

-- 封装礼包结构体
function fontLibaoNet(net, id, config, libaoType, human)
	net.id = id 
	net.items[0] = #config.items
	for i = 1, net.items[0] do
		local itemID = config.items[i][1]
		local itemCnt = config.items[i][2]
		Grid.makeItem(net.items[i], itemID, itemCnt)
	end
	net.name = config.name 
	net.buyItem[0] = 0
	if config.buyID > 0 then
		net.buyItem[0] = 1
		BuyLogic.fontBuyItem(human, net.buyItem[1], config.buyID)
	end
	net.cnt = getBuyCnt(human, libaoType, id)
	net.maxCnt = config.cnt
end

-- 界面查询
function sendQuery(human, libaoType)
	local startTime, endTime = getLibaoTime(libaoType)
	if not startTime then -- 没设置开服时间不开
		return Broadcast.sendErr(human, Lang.JJC_NOT_SET_OPEN_TIME)
	end 
	local configs = getConfigs(libaoType)
	if not configs then return end

	checkDirty(human)	
	local msgRet = Msg.gc.GC_RICHANG_LIBAO_QUERY
	msgRet.libaoType = libaoType
	msgRet.leftTime = endTime - os.time()
	msgRet.list[0] = #configs
	for i = 1, msgRet.list[0] do
		local config = configs[i]
		fontLibaoNet(msgRet.list[i], i, config, libaoType, human)
	end
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

-- 购买礼包
function buyLibao(human, libaoType, id, buyType)
	local startTime, endTime = getLibaoTime(libaoType)
	if not startTime then -- 没设置开服时间不开
		return Broadcast.sendErr(human, Lang.JJC_NOT_SET_OPEN_TIME)
	end 
	local configs = getConfigs(libaoType)
	local config = configs and configs[id]
	if not config then return end

	if buyType == FREE_BUY and config.buyID > 0 then
		return -- rmb礼包不能免费领取
	end

	checkDirty(human)
	if getBuyCnt(human, libaoType, id) >= config.cnt then
		return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_CNT)
	end

	local logType = getLogType(libaoType)
	addBuyCnt(human, libaoType, id)
	BagLogic.addItemList(human, config.items, logType)
	sendQuery(human, libaoType)
	
	if libaoType == LIBAOTYPE_WEEK then
	    for k, v in pairs(funcID) do
            YunYingLogic.updateIcon(YYInfo[k], human)
	        YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3104)
            break
        end
	else
	    for k, v in pairs(funcID) do
            YunYingLogic.updateIcon(YYInfo[k], human)
	        YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3207)
            break
        end
	end
end

-- 红点
function isRed(human, YYInfo, funcConfig)
	local libaoType = nil
	if funcConfig.panelID == PanelDefine.PANEL_ID_3104 then
		libaoType = LIBAOTYPE_WEEK 
	elseif funcConfig.panelID == PanelDefine.PANEL_ID_3207 then 
		libaoType = LIBAOTYPE_MONTH
	end 
	if not libaoType then return end
	local configs = getConfigs(libaoType)
	if not configs then return end

	for id, config in pairs(configs) do
		if config.buyID < 1 and 
			getBuyCnt(human, libaoType, id) < config.cnt then
			return true
		end
	end
end

-- 是否开启
function isOpen(human, YYInfo, funcConfig)
	local libaoType = nil
	if funcConfig.panelID == PanelDefine.PANEL_ID_3104 then
		libaoType = LIBAOTYPE_WEEK 
	elseif funcConfig.panelID == PanelDefine.PANEL_ID_3207 then 
		libaoType = LIBAOTYPE_MONTH
	end 
	if not libaoType or not getLibaoTime(libaoType) then
		return
	end
    local startTime, endTime = getLibaoTime(libaoType)
	return true, endTime,startTime
end



