-- 推送礼包
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local Broadcast = require("broadcast.Broadcast")
local PresentExcel = require("excel.present")
local Util = require("common.Util")
local BuyLogic = require("topup.BuyLogic")
local YunYingLogic = require("yunying.YunYingLogic")

-- db.tuiSongLiBao.libao[id] = { 
-- 	activeTime 激活时间
--	isBuy 是否购买
--	activeCnt 触发次数
-- }
-- db.tuiSongLiBao.task[taskId] = {
-- 	finCnt 完成次数
-- }

TUISONGLIBAOTASK_LV = 1 --玩家到达x级别
TUISONGLIBAOTASK_STARS_HERO = 2 --获得x星武将
TUISONGLIBAOTASK_ZHAOJIANG = 3 --召将达到x次
TUISONGLIBAOTASK_ZHENGZHAN = 4 --通关关卡（x）
TUISONGLIBAOTASK_TOWER = 5 --通关通天塔（x）层
TUISONGLIBAOTASK_ZHUANPAN = 6 --许愿（x）次

local function getLeftTimeByID(human,id)
	local tuiSongLiBaoDB = human.db.tuiSongLiBao
	if not tuiSongLiBaoDB or 
		not tuiSongLiBaoDB.libao or 
		not tuiSongLiBaoDB.libao[id] then return 0 end

	local config = PresentExcel.tuiSongLiBao[id]
	local activeTime = tuiSongLiBaoDB.libao[id].activeTime
	local leftTime = (activeTime + config.canBuyTime) - os.time()

	if leftTime < 0 then
		leftTime = 0
	end

	return leftTime
end

local function getMinLeftTime(human)
	local tuiSongLiBaoDB = human.db.tuiSongLiBao
	if not tuiSongLiBaoDB or 
		not tuiSongLiBaoDB.libao then return end

	local min = nil
	for id,data in pairs(tuiSongLiBaoDB.libao) do
		if not data.isBuy then
			local leftTime = getLeftTimeByID(human,id)
			if leftTime > 0 then
				if not min then
					min = leftTime
				end

				if min > leftTime then
					min = leftTime
				end
			end
		end
	end

	return min
end

local function isBuy(human,id)
	local tuiSongLiBaoDB = human.db.tuiSongLiBao
	if not tuiSongLiBaoDB or 
		not tuiSongLiBaoDB.libao or 
		not tuiSongLiBaoDB.libao[id] then return end

	return tuiSongLiBaoDB.libao[id].isBuy
end

local function makeTuiSongLiBaoNet(human,id,net)
	net.leftTime = getLeftTimeByID(human,id)
	
	local config = PresentExcel.tuiSongLiBao[id]
	net.desc = config.desc
	net.reward[0] = #config.reward
	for i=1,#config.reward do
		local itemID = config.reward[i][1]
		local itemCnt = config.reward[i][2]
		Grid.makeItem(net.reward[i], itemID, itemCnt)
	end
	net.bg = config.bg
	net.fanli = config.fanli
	net.isBuy = isBuy(human,id) and 1 or 0
	net.id = id
	net.giftVipExp = config.vipExp
	net.type = config.task[1]
	BuyLogic.fontBuyItem(human,net.buyItem, config.buyID)
end

function CG_PRESENT_TUISONGLIBAO_QUERY(human)
	ObjHuman.updateDaily(human)

	local tuiSongLiBaoDB = human.db.tuiSongLiBao
	if not tuiSongLiBaoDB or 
		not tuiSongLiBaoDB.libao then return end

	local minLeftTime = getMinLeftTime(human)
	if not minLeftTime then return end

	local msgRet = Msg.gc.GC_PRESENT_TUISONGLIBAO_QUERY
	msgRet.libao[0] = 0
	for id in pairs(tuiSongLiBaoDB.libao) do
		if msgRet.libao[0] < #msgRet.libao then
			local leftTime = getLeftTimeByID(human,id)
			if leftTime > 0 then
				msgRet.libao[0] = msgRet.libao[0] + 1
				makeTuiSongLiBaoNet(human,id,msgRet.libao[msgRet.libao[0]])
			end
		end
	end
	--Msg.trace(msgRet)
	Msg.send(msgRet,human.fd)
end

function tuiSongLiBaoBuy(human,id)
	ObjHuman.updateDaily(human)

	local config = PresentExcel.tuiSongLiBao[id]
	if not config then return end

	local tuiSongLiBaoDB = human.db.tuiSongLiBao
	if not tuiSongLiBaoDB 
		or not tuiSongLiBaoDB.libao 
		or not tuiSongLiBaoDB.libao[id] then return end

	local leftTime = getLeftTimeByID(human,id)
	if leftTime <= 0 then
	 	return Broadcast.sendErr(human,Lang.PRESENT_TUISONGLIBAO_NOTBUY)
	end

	if isBuy(human,id) then return end

	human.db.tuiSongLiBao = human.db.tuiSongLiBao or {}
	human.db.tuiSongLiBao.libao = human.db.tuiSongLiBao.libao or {}
	human.db.tuiSongLiBao.libao[id] = human.db.tuiSongLiBao.libao[id] or {}
	human.db.tuiSongLiBao.libao[id].isBuy = (human.db.tuiSongLiBao.libao[id].isBuy or 0) + 1

	BagLogic.addItemList(human, config.reward, "tuisong_liBao")
	Broadcast.sendErr(human, Lang.ITEM_BUY_SUCCESS)

	local msgRet = Msg.gc.GC_PRESENT_TUISONGLIBAO_BUY
	msgRet.id = id
	Msg.send(msgRet,human.fd)
end

local function getLiBaoData(human,id)
	local tuiSongLiBaoDB = human.db.tuiSongLiBao
	if not tuiSongLiBaoDB or not tuiSongLiBaoDB.libao then return end

	return tuiSongLiBaoDB.libao[id]
end

local function getLiBaoActiveCnt(human,id)
	local data = getLiBaoData(human,id)
	return data and data.activeCnt or 0
end

local function checkLiBaoCanActive(human,id)
	local libaoActiveCnt = getLiBaoActiveCnt(human,id)
	local config = PresentExcel.tuiSongLiBao
	if config[id].dayActiveCnt > libaoActiveCnt then
		return true
	end
end

local function libaoMake(human,id)
	human.db.tuiSongLiBao = human.db.tuiSongLiBao or {}
	human.db.tuiSongLiBao.libao = human.db.tuiSongLiBao.libao or {}
	human.db.tuiSongLiBao.libao[id] = human.db.tuiSongLiBao.libao[id] or {}
	human.db.tuiSongLiBao.libao[id].activeTime = os.time()
	human.db.tuiSongLiBao.libao[id].activeCnt = (human.db.tuiSongLiBao.libao[id].activeCnt or 0) + 1
	human.db.tuiSongLiBao.libao[id].isBuy = nil
end

local function checkTaskSucess(human, config, arg, arg2)
	local taskType = config.task[1]
	local cntNeed = config.task[2]
	if taskType == TUISONGLIBAOTASK_STARS_HERO then
		-- 星级
		if arg == cntNeed then 
			return true
		end
	elseif taskType == TUISONGLIBAOTASK_LV then
		-- 等级
		local cntNew = arg
		local cntOld = arg2
		if cntNew >= cntNeed and cntOld < cntNeed then
			return true
		end
	elseif taskType == TUISONGLIBAOTASK_ZHAOJIANG then
		-- 招将次数
		local cntNew = human.db.tuiSongLiBao.task[taskType] or 0
		local cntOld = cntOld - arg
		if cntNew >= cntNeed and cntOld < cntNeed then
			return true
		end
	elseif taskType == TUISONGLIBAOTASK_ZHENGZHAN then
		-- 征战
		local cntNew = arg
		local cntOld = arg2
		if cntNew >= cntNeed and cntOld < cntNeed then
			return true
		end
	elseif taskType == TUISONGLIBAOTASK_TOWER then
		-- 爬塔
		local cntNew = arg
		local cntOld = arg2
		if cntNew >= cntNeed and cntOld < cntNeed then
			return true
		end
	elseif taskType == TUISONGLIBAOTASK_ZHUANPAN then
		-- 转盘次数
		local cntNew = human.db.tuiSongLiBao.task[taskType] or 0
		local cntOld = cntOld - arg
		if cntNew >= cntNeed and cntOld < cntNeed then
			return true
		end
	else
		assert(nil)
	end
end

function tuiSongLiBaoOnTask(human,taskType,arg, arg2)
	do return end
	if taskType == nil then 
		assert(nil)
	end

	ObjHuman.updateDaily(human)

	local taskTypeConfig = PresentExcel.tuiSongLiBaoTask[taskType]
	if taskTypeConfig.needSaveDB == 1 then
		human.db.tuiSongLiBao = human.db.tuiSongLiBao or {}
		human.db.tuiSongLiBao.task = human.db.tuiSongLiBao.task or {}
		human.db.tuiSongLiBao.task[taskType] = (human.db.tuiSongLiBao.task[taskType] or 0) + arg
	end

	local isActive = nil
	for id, config in pairs(PresentExcel.tuiSongLiBao) do
		if config.task[1] == taskType then
			if checkTaskSucess(human, config, arg, arg2) then
				local canActive = checkLiBaoCanActive(human,id)
				local leftTime = getLeftTimeByID(human,id)
				if canActive and leftTime <= 0 then
					libaoMake(human,id)
					isActive = true

					local msgRet = Msg.gc.GC_PRESENT_TUISONGLIBAO_QUERY
					msgRet.libao[0] = 1
					makeTuiSongLiBaoNet(human,id,msgRet.libao[1])
					--Msg.trace(msgRet)
					Msg.send(msgRet,human.fd)
				end
			end
		
		end
	end

	if isActive then
		for k, v in pairs(funcID) do
            YunYingLogic.updateIcon(YYInfo[k], human,true)
            break
        end
	end
end

function updateDaily(human)
	local tuiSongLiBaoDB = human.db.tuiSongLiBao
	if not tuiSongLiBaoDB then return end
	
	human.db.tuiSongLiBao.task = nil
	
	if not tuiSongLiBaoDB.libao then return end

	for id,data in pairs(tuiSongLiBaoDB.libao) do
		local leftTime = getLeftTimeByID(human,id)
		if leftTime > 0 then
			human.db.tuiSongLiBao.libao[id].activeCnt = nil
		else
			human.db.tuiSongLiBao.libao[id] = nil
		end
	end
end

function isOpen(human)
	local min = getMinLeftTime(human)
	if min and min > 0 then return true end
end

-- function isRed(human)
-- 	local min = getMinLeftTime(human)
-- 	if min and min > 0 then return true end
-- end

function getLeftTime(human, YYInfo)
	if not YYInfo then return 0 end

	local min = getMinLeftTime(human)
	return min or 0
end