---------------------------------------------------
-- 充值-每日礼包
-- 每日0点刷新购买次数

-- db.dailyLibao[id] = cnt 

-- 新修改: 不同渠道的礼包配置不同
---------------------------------------------------

local DailyLibaoExcel = require("excel.present").dailyLibao
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
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 VipLogic = require("vip.VipLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local CommonDefine = require("common.CommonDefine")
local CommonDB = require("common.CommonDB")

local FREE_LIBAO_ID = 0 		-- 免费
local QUICK_LIBAO_ID = 1  		-- 一键购买

local QUICK_STATUS_CANT = 0 	-- 不能购买
local QUICK_STATUS_CAN = 1 		-- 可购买

local DAILY_GITFT_ACTID = 312	-- 日礼包活动Id

local COND_OPEN_SRV_DAYS = 2	--  需要开服2天后才展示


-- 通过渠道标识获取礼包配置ID
local function getCfgIdxByChannel(human)
	local quick_gift_id = 1
	local gift_start_id, gift_end_id = 2, 4
	--if human.phpChanelID == CommonDefine.CHANNEL_TAG_MUZI or human.phpChanelID == CommonDefine.CHANNEL_TAG_SANLI_QQ then
		quick_gift_id = 11
		gift_start_id = 12
		gift_end_id = 14
	--end
	return quick_gift_id, gift_start_id, gift_end_id
end


-- 已购买数量
function getCnt(human, id)
	if not human.db.dailyLibao then
		return 0
	end
	return human.db.dailyLibao[id] or 0
end

function addCnt(human, id)
	if not human.db.dailyLibao then
		human.db.dailyLibao = {}
	end
	human.db.dailyLibao[id] = getCnt(human, id) + 1 
end

-- 剩余次数
function getLeftCnt(human, id)
	local config = DailyLibaoExcel[id]
	if not config then return 0 end

	return math.max(config.cnt - getCnt(human, id), 0)
end

-- 封装礼包结构体
function fontLibaoNet(net, id, config, human)
	net.id = id 
	net.items[0] = #config.items
	net.doubleFlags[0] = net.items[0]
	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)
		net.doubleFlags[i] = config.items[i][3] or 0  -- 是否显示双倍标签
	end
	net.cnt = getCnt(human, id)
	net.maxCnt = config.cnt
	net.vipLv = 0
	BuyLogic.fontBuyItem(human, net.buyItem, config.buyID)
	net.name = config.name
end

-- 可否一键购买 
local function canQuickBuy(human)
	-- for id, config in ipairs(DailyLibaoExcel) do
	-- 	if id ~= QUICK_LIBAO_ID then
	-- 		-- 剩余购买次数
	-- 		if getCnt(human, id) >= config.cnt then
	-- 			return 
	-- 		end
	-- 	end
	-- end


	local _, gift_start_id, gift_end_id = getCfgIdxByChannel(human)

	for i=gift_start_id, gift_end_id do
		local config = DailyLibaoExcel[i]
		-- 剩余购买次数
		if getCnt(human, i) >= config.cnt then
			return false
		end
	end

	return true
end

-- 获取原价
local function getQuickOldPrice(human)
	local sum = 0
	-- for id, config in ipairs(DailyLibaoExcel) do
	-- 	if id ~= QUICK_LIBAO_ID then
	-- 		sum = sum + BuyLogic.getRealPrice(human, config.buyID)
	-- 	end
	-- end

	local _, gift_start_id, gift_end_id = getCfgIdxByChannel(human)

	for i=gift_start_id, gift_end_id do
		local config = DailyLibaoExcel[i]
		sum = sum + BuyLogic.getRealPrice(human, config.buyID)
	end

	return sum
end

-- 封装一键购买信息
function fontQuickNet(net, human, quick_gift_id)
	local config = DailyLibaoExcel[quick_gift_id]
	net.status = canQuickBuy(human) and QUICK_STATUS_CAN or QUICK_STATUS_CANT
	net.oldPrice = getQuickOldPrice(human)
	net.vipLv = 0
	BuyLogic.fontBuyItem(human, net.buyItem, config.buyID)
end

-- 面板查询
function sendQuery(human)
	-- ObjHuman.updateDaily(human)
	-- local msgRet = Msg.gc.GC_PRE_DAILY_LIBAO_QUERY
	-- msgRet.list[0] = 0
	-- for id, config in ipairs(DailyLibaoExcel) do
	-- 	if id ~= QUICK_LIBAO_ID then
	-- 		msgRet.list[0] = msgRet.list[0] + 1
	-- 		fontLibaoNet(msgRet.list[msgRet.list[0]], id, config, human)
	-- 	end
	-- end
	-- fontQuickNet(msgRet.quickData, human)
	-- msgRet.freeRed = (getLeftCnt(human, FREE_LIBAO_ID) > 0) and 1 or 0
	-- --Msg.trace(msgRet)
	-- Msg.send(msgRet, human.fd)

	local quick_gift_id, gift_start_id, gift_end_id = getCfgIdxByChannel(human)
	local msgRet = Msg.gc.GC_PRE_DAILY_LIBAO_QUERY
	msgRet.list[0] = 0

	local len = 0

	for i=gift_start_id, gift_end_id do
		len = len + 1
		msgRet.list[0] = len
		local config = DailyLibaoExcel[i]
		fontLibaoNet(msgRet.list[len], i, config, human)
	end

	fontQuickNet(msgRet.quickData, human, quick_gift_id)
	msgRet.freeRed = (getLeftCnt(human, FREE_LIBAO_ID) > 0) and 1 or 0

	Msg.send(msgRet, human.fd)
end

-- 给予东西
local function giveLibaoItems(human, id)
	local config = DailyLibaoExcel[id]
	if not config then return end
	if getLeftCnt(human, id) < 1 then
		return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_CNT)
	end
	
	addCnt(human, id)
	return config.items
end

-- 领取免费礼包
function getPreFreeLibao(human)
	ObjHuman.updateDaily(human)
	local items = giveLibaoItems(human, FREE_LIBAO_ID)
	if not items then return end
	sendQuery(human)
	-- 通用获得道具弹窗
    if items then
        for _, item in pairs(items) do
            BagLogic.addItem(human, item[1], item[2], "daily_libao")
        end
    end
	BagLogic.sendItemGetList(human, items, "daily_libao")
    for k, v in pairs(funcID) do
        YunYingLogic.updateIcon(YYInfo[k], human)
        YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3107)
        break
    end
end

-- 购买礼包
function buyLibao(human, id, buyNum)
	-- ObjHuman.updateDaily(human)
	local itemsAll = nil
	-- if id == QUICK_LIBAO_ID then
	-- 	for tid, config in ipairs(DailyLibaoExcel) do
	-- 		if tid ~= QUICK_LIBAO_ID then
	-- 			local items = giveLibaoItems(human, tid)
	-- 			if items then
	-- 				itemsAll = itemsAll or {}
	-- 				for _, item in ipairs(items) do
	-- 					itemsAll[#itemsAll + 1] = item
	-- 				end
	-- 			end
	-- 		end
	-- 	end	
	-- 	itemsAll = BagLogic.sameItemTogether(itemsAll)
	-- else
	-- 	itemsAll = giveLibaoItems(human, id)
	-- end

	local quick_gift_id, gift_start_id, gift_end_id = getCfgIdxByChannel(human)
	if id == quick_gift_id then
		for i=gift_start_id, gift_end_id do
			local items = giveLibaoItems(human, i)
			if items then
				itemsAll = itemsAll or {}
				for _, item in ipairs(items) do
					itemsAll[#itemsAll + 1] = item
				end
			end
		end

		itemsAll = BagLogic.sameItemTogether(itemsAll)
	else
		itemsAll = giveLibaoItems(human, id)
	end


	if not itemsAll then return end

    if itemsAll then
        for _, item in pairs(itemsAll) do
            BagLogic.addItem(human, item[1], item[2], "mail")
        end
    end
	-- 通用获得道具弹窗
	BagLogic.sendItemGetList(human, itemsAll, "daily_libao")

    sendQuery(human)
end

-- 红点
function isRed(human)
	-- 有免费礼包可以领取
	if getLeftCnt(human, FREE_LIBAO_ID) > 0 then
		return true
	end
end


function isOpen(human, YYInfo, funcConfig)
	-- 没有充值过, 不显示
	if not BuyLogic.isChange(human) then
		return false
	end

	local openDay = CommonDB.getServerOpenDay()
    if openDay < COND_OPEN_SRV_DAYS then
        return false
    end

	return true
end



function ShowAct(human)
	local YunYingExcel = require("excel.yunying")
	local config = YunYingExcel.func[DAILY_GITFT_ACTID]
	YunYingLogic.sendGroupUpdate(YYInfo[DAILY_GITFT_ACTID], human, config.panelID)
end


-- function GetRemainNum(human, nBuyID)
-- 	local _, gift_start_id, gift_end_id = getCfgIdxByChannel(human)
	
-- 	for i = gift_start_id, gift_end_id do
-- 		local config = DailyLibaoExcel[i]
-- 		-- 剩余购买次数
-- 		if config.buyID == nBuyID then
-- 			local nNowBuyNum = getCnt(human, i)
-- 			if nNowBuyNum < config.cnt  then
-- 				return config.cnt - nNowBuyNum
-- 			else
-- 				return 0
-- 			end
-- 		end
-- 	end

-- 	return 0
-- end