-------------------------------------------------
-- 充值
-------------------------------------------------

local BuyExcel = require("excel.buy")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local BuyLogic = require("topup.BuyLogic")
local SceneHandler = require("scene.Handler")
local YunYingLogic = require("yunying.YunYingLogic")
local LeichongHaoli = require("present.LeichongHaoli")
local LeijiChongzhi = require("present.LeijiChongzhi")
local DailyLeichong = require("present.DailyLeichong")
local LoginSignLogic = require("loginSign.LoginSignLogic")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local MailExcel = require("excel.mail")
local MailManager = require("mail.MailManager")
local CommonDB = require("common.CommonDB")
local BreakThroughTheme = require("battle.BreakThroughTheme")
local BattleGift = require("battle.BattleGift")


-- 专属客服充值额度
EXCLUSIVE_SERVER_9937 = 1000

TOPUP_LIBAO_ID1 = 1   --盲盒礼包
TOPUP_LIBAO_ID2 = 2   --铂金周卡
TOPUP_LIBAO_ID3 = 3   --365王卡
local MAX_FIREWORKS_DURATION = 43200
local YANHUA_EMAIL = 7012 --烟花邮箱
local ITEMID=1022  --烟花道具ID
--封装直购基本信息
local function wrapTopupItem(net, config, human)
	net.id = config.id	
	BuyLogic.fontBuyItem(human, net.buyItem, config.buyID)
end

-- 充值列表
function query(human)
	if SceneHandler.canCharge(human) ~= true then
		return
	end
	local msgRet = Msg.gc.GC_TOPUP_QUERY
	msgRet.list[0] = #BuyExcel.topup
	for i, config in ipairs(BuyExcel.topup) do
		local net = msgRet.list[i]
		wrapTopupItem(net, config, human)
	end
	Msg.send(msgRet,human.fd)
end

-- 直购回调
function clacTopupAcount(human, price, buyID, buyNum)
    if price < 0 then return end

	if not buyNum then
		buyNum = 1
	end

    ObjHuman.updateDaily(human)

	local nAllPrice = price * buyNum
	human.db.topupAcountDaily = (human.db.topupAcountDaily or 0) + nAllPrice
	human.db.topupAccountMonth = (human.db.topupAccountMonth or 0) + nAllPrice
    human.db.topupAcount = (human.db.topupAcount or 0) + nAllPrice

    checkKf(human)

	YunYingLogic.onCharge(human, nAllPrice, buyID, buyNum)
	DailyLeichong.charge(human, nAllPrice)
	LeichongHaoli.onAddMoney(human, nAllPrice)
	LeijiChongzhi.onAddMoney(human, nAllPrice)
	LoginSignLogic.chargeAfter(human)
	BreakThroughTheme.charge(human, nAllPrice, buyID)
	BattleGift.charge(human, nAllPrice, buyID)

	sendFireworksMail(human,nAllPrice)
end

-- 发送烟花邮件
function sendFireworksMail(human, price)
	if price < 100 then return end  -- 如果充值金额小于100，不发送邮件

    -- 计算烟花数量
    local nFireWorksCount = math.floor(price / 100)
	if 0 >= nFireWorksCount then
		return
	end
	local mailCfg = MailExcel.mail[YANHUA_EMAIL]
	local title = mailCfg.title
	local content = mailCfg.content
	local senderName = mailCfg.senderName
    local items = {
        {ITEMID,nFireWorksCount}  -- 1022是烟花的道具ID
    }
    -- 调用邮件管理器发送邮件
    MailManager.add(MailManager.SYSTEM, human.db._id, title, content, items, senderName)
end

function checkKf(human)
	-- 激活专属客服
    if human.db.topupAcount ~= nil and human.db.topupAcount >= EXCLUSIVE_SERVER_9937 then
        Msg.send(Msg.gc.GC_EXCLUSIVE_SERVER,human.fd)
    end
end

--给客户端下发结束时间//定义的协议时间 //当客户端有点击请求的时候
function DurationofBonus(human)
	local tMsgData = Msg.gc.GC_FIREWORKS_QUERY
	local nowBufferDuration = CommonDB.getFireWorksBonusTime()-os.time()
	tMsgData.buffDuration = math.max(0,nowBufferDuration)
	Msg.send(tMsgData, human.fd)
end

function Addtime(human, fireworksCount)
	local nNowTime = os.time()
	if not human or fireworksCount <= 0 then
        return
    end
    local currentEndTime = CommonDB.getFireWorksBonusTime()
	local baseTime = nNowTime
	if currentEndTime > nNowTime then
		baseTime = currentEndTime
	end
    local addedTime = math.max(fireworksCount * 60,60)
	local newEndTime = math.min(
		baseTime + addedTime,
        nNowTime + MAX_FIREWORKS_DURATION
	)
	local totalDuration = newEndTime-nNowTime
	if totalDuration <=0 or totalDuration> MAX_FIREWORKS_DURATION then
		return
	end
    CommonDB.setFireWorkBonusTime(newEndTime)
	local tMsgData = Msg.gc.GC_FIREWORKS_SHOW
	local nowBuffDuration = CommonDB.getFireWorksBonusTime()-os.time()
	tMsgData.buffDuration = math.max(0,nowBuffDuration)
	tMsgData.playerName = human.db.name

    -- 下发时间和玩家名称
	for _, human in pairs(ObjHuman.onlineUuid) do
		if human and human.fd then
		    Msg.send(tMsgData, human.fd)
		end
	end    
end