--------------------------------------------------
-- 周末福利
-- 周六、周日、周一开启 每天可领礼包

-- db.weekendFuli.ts 		开始时间戳
-- db.weekendFuli.list 		领取标记 [id]=是否领取
--------------------------------------------------

local WeekendFuliExcel = require("excel.present").weekendFuli
local Util = require("common.Util")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local PanelDefine = require("broadcast.PanelDefine")
local YunYingLogic = require("yunying.YunYingLogic")

-- 状态
local STATE_NONE = 0 		-- 不可领
local STATE_GET = 1 		-- 可领
local STATE_HAD = 2 		-- 已领
local STATE_OVER = 3 		-- 过期

-- 获取本期活动时间 开始时间 结束时间
local TEMP_TIME = nil
function getActTime()
	local nowTime = os.time()
	if not TEMP_TIME or nowTime >= TEMP_TIME.endTime then
		local time = Util.getWeekStartTime(nowTime)
		local sconf = WeekendFuliExcel[1]
		local tconf = WeekendFuliExcel[#WeekendFuliExcel]
		local startTime = time + (sconf.week - 1) * 86400
		local endTime = time + tconf.week * 86400
		if startTime > endTime then
			if nowTime >= endTime then
				endTime = endTime + 7 * 86400
			else
				startTime = startTime - 7 * 86400 
			end
		end
		TEMP_TIME = TEMP_TIME or {}
		TEMP_TIME.startTime = startTime 
		TEMP_TIME.endTime = endTime
	end
	return TEMP_TIME.startTime, TEMP_TIME.endTime
end

-- 是否开启
function isOpen(human)
	local startTime, endTime = getActTime()
	local nowTime = os.time()
	if startTime <= nowTime and nowTime < endTime then
		return true
	end
end

-- 是否有红点
function isRed(human)
	checkDirty(human)

	for id in ipairs(WeekendFuliExcel) do
		if getState(human, id) == STATE_GET then
			return true
		end
	end
end

-- 礼包状态
function getState(human, id)
	local config = WeekendFuliExcel[id]
	if not config then 
		return STATE_NONE
	end
	if isGet(human, id) then
		return STATE_HAD
	end
	local nowWeek = Util.getWeekDay() - 1
	if nowWeek == 0 then
		nowWeek = 7
	end
	if nowWeek == config.week then
		return STATE_GET
	end
	local startTime = getActTime()
	local nowTime = os.time()
	local nowID = math.ceil(math.max(nowTime - startTime, 1) / 86400)
	if nowID > id then
		return STATE_OVER
	end
	return STATE_NONE
end

-- 检查状态
function checkDirty(human)
	if not human.db.weekendFuli then
		return 
	end
	local data = human.db.weekendFuli
	local startTime = getActTime()
	if data.ts ~= startTime then
		human.db.weekendFuli = nil
	end
end

-- 设置已领取
function setGet(human, id)	
	if not human.db.weekendFuli then
		local startTime = getActTime()
		human.db.weekendFuli = {}
		human.db.weekendFuli.ts = startTime
	end
	local data = human.db.weekendFuli
	data.list = data.list or {}
	data.list[id] = true
end

-- 是否已领取
function isGet(human, id)
	if not human.db.weekendFuli then
		return 
	end
	if not human.db.weekendFuli.list then
		return
	end
	return human.db.weekendFuli.list[id]
end

-- 封装礼包结构体
function fontNet(net, id, config, human)
	net.id = id 
	net.name = config.name 
	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.state = getState(human, id)
end

-- 界面查询
function sendQuery(human)
	checkDirty(human)
	local startTime, endTime = getActTime()	
	local msgRet = Msg.gc.GC_WEEKEND_FULI_QUERY
	msgRet.startTime = startTime
	msgRet.endTime = endTime
	msgRet.list[0] = #WeekendFuliExcel
	for id, config in ipairs(WeekendFuliExcel) do
		fontNet(msgRet.list[id], id, config, human)
	end
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

-- 领取奖励
function getItems(human, id)
	local state = getState(human, id)
	if state == STATE_NONE or state == STATE_OVER then
		return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_CONDITION)
	end
	if state == STATE_HAD then
		return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_HADGET)
	end

	local config = WeekendFuliExcel[id]
	setGet(human, id)
	BagLogic.addItemList(human, config.items, "weekend_fuli")
	sendQuery(human)

    -- 没有此活动 暂时注释
	--YunYingLogic.updateIcon(YYInfo, human)
	--YunYingLogic.sendGroupUpdate(YYInfo, human, PanelDefine.PANEL_ID_3306)
end