--------------------------------
-- 文件名       ：  OpenServerActAddUpCharge.lua
-- 文件说明     ：  开服7天活动 - 累积充值
-- 创建时间     :   2025/1/14
-- 创建人       :   FC
--------------------------------

local Util = require("common.Util")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local OpenAct = require("present.OpenAct")
local PresentExcel = require("excel.present")
local OpenActExcel = require("excel.openAct")
local MailExcel = require("excel.mail")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local MailManager = require("mail.MailManager")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local KingWorldLogic = require("present.KingWorldLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")
local SevenDayGiftLogic = require("present.SevenDayGiftLogic")
local CommonDB = require("common.CommonDB")
local BuyLogic = require("topup.BuyLogic")
local GuideLogic = require("guide.GuideLogic")
local Log = require("common.Log")

local tPrizeByID = nil
local nShowAllMoney = nil             -- 展示所有的奖励的充值数
local nMailID = 7011                    -- 邮件ID

ADDUPCHARGEGIFT_STATE_NONE    =   0       -- 不可领取
ADDUPCHARG_STATE_CANGET  =   1       -- 可领取
ADDUPCHARG_STATE_FINISH  =   2       -- 已领取

----------------------------------------- DB数据操作 -------------------------------------

-- 初始化DB数据
local function ActAddUpCharge_InitDB(human)
    if not human then
        return false
    end

    human.db.AddUpChargeGift = {}
    return true
end

-- 获取DB数据
local function ActAddUpCharge_GetDB(human)
    if not human or not human.db.AddUpChargeGift then
        return nil
    end
    
    return human.db.AddUpChargeGift
end

-- 获取累充金额
local function ActAddUpCharge_GetDBCharge(human)
    if not human.db.AddUpCharge then
        human.db.AddUpCharge = 0
    end

    return  human.db.AddUpCharge
end

-- 设置累充金额
local function ActAddUpCharge_SetDBCharge(human, nValue)
    if not human.db.AddUpCharge then
        human.db.AddUpCharge = 0
    end

    human.db.AddUpCharge = nValue
end

-- 创建DB数据
local function ActAddUpCharge_CreateDB(human)
    if not human then
        return false
    end

    if not human.db.AddUpChargeGift then
        local bRet = ActAddUpCharge_InitDB(human)
        if false == bRet then
            print("[ActAddUpCharge_CreateDB] 初始化BD数据失败")
            return false
        end

        if tPrizeByID then
            for nID, value in pairs(tPrizeByID) do
                human.db.AddUpChargeGift[nID] = ADDUPCHARGEGIFT_STATE_NONE
            end
        else
            print("[ActAddUpCharge_CreateDB] 不存在对应的ID对应配置")
            return false
        end
    end

    if not human.db.AddUpCharge then
        human.db.AddUpCharge = 0
    end

    human.db.AddUpChargeSendMail = 0

    return true
end

-- 获取某个数据状态
local function ActAddUpCharge_GetDBIDState(human, nID)
    if not human or 0 >= nID then
        return ADDUPCHARGEGIFT_STATE_NONE
    end

    local tDBPrize = ActAddUpCharge_GetDB(human)
    if nil == tDBPrize then
        return ADDUPCHARGEGIFT_STATE_NONE
    end

    return tDBPrize[nID]
end

-- 设置某个数据状态
local function ActAddUpCharge_SetDBIDState(human, nID, nState)
    if not human or 0 >= nID then
        return false
    end

    local tDBPrize = ActAddUpCharge_GetDB(human)
    if nil == tDBPrize then
        return false
    end

    tDBPrize[nID] = nState
    return true
end
----------------------------------------- 内部函数判断 -------------------------------------
-- 获取配置
local function ActAddUpCharge_GetConfig()
    return OpenActExcel.AddUpChargeGift
end

-- 日志写入
local function ActAddUpCharge_WriteLog(szText)
    Log.write(Log.LOGID_OSS_OPENSERVER_ACT, szText)
end

-- 报错打印
local function ActAddUpCharge_PrintInfo(szFuncName,szText)
    print(szFuncName.." "..szText)
end

-- 客户端回包
local function ActAddUpCharge_SendClient(fd, data)
    Msg.send(data, fd)
    ActAddUpCharge_PrintInfo("[ActAddUpCharge_SendClient]", "发送给客户端结束")
end

-- 活动是否开启
local function ActAddUpCharge_IsOpen(human)
    local szFuncName = "ActAddUpCharge_IsOpen"
    local szText = ""
    if not human then
        szText = "参数不正确 "
        ActAddUpCharge_PrintInfo(szFuncName, szText)
        return false
    end

    local nOpenLv = YunYingLogic.getOpenLvByPanelID(PanelDefine.PANEL_ID_3609)
    if human.db.lv < nOpenLv then
        return false
    end

    local flag = OpenAct.getOpenActTime(OpenAct.OPEN_ACT_SERVER_GIFT)
    if not flag then
		return false
	end

    return true
end

-- 活动剩余时间
local function ActAddUpCharge_GetLeftTime()
    local openDay = CommonDB.getServerOpenDay()
    if openDay == nil then
        return 0
    end

    local OpenActConfig = OpenActExcel.openAct[1]
    if not OpenActConfig then return 0 end

    local sDay = OpenActConfig.sDay
    local eDay = OpenActConfig.eDay

    if openDay >= sDay and openDay <= eDay then
        local time = os.time()
        local openTime = CommonDB.getServerOpenTime()
        if openTime == 0 then
            return 0
        end

        local endTime = Util.getDayStartTime(openTime) + eDay * 86400
        return endTime - time
    end

    return 0
end

-- 红点
local function ActAddUpCharge_CheckRed(human)
    local szFuncName = "ActAddUpCharge_CheckRed"
    local szText = ""
    if not human then
        szText = "参数不正确"
        ActAddUpCharge_PrintInfo(szFuncName, szText)
        return false
    end

    ActAddUpCharge_CreateDB(human)

    local tDBData = ActAddUpCharge_GetDB(human)
    if not tDBData then
        szText = "获取玩家DB奖励数据状态表失败"
        ActAddUpCharge_PrintInfo(szFuncName, szText)
        return false
    end

    for k, v in pairs(tDBData) do
        if ADDUPCHARG_STATE_CANGET == v then
            print("[ActAddUpCharge_CheckRed] 存在红点")
            return true
        end
    end

    return false
end

-- 是否显示隐藏
local function ActAddUpCharge_Show(human)
    local nNowMoney = ActAddUpCharge_GetDBCharge(human)
    if not nNowMoney or not nShowAllMoney then
        return false
    end
   
    return nNowMoney >= nShowAllMoney
end
----------------------------------------- 活动模板调用 -------------------------------------
-- 是否开启
function isOpen(human, YYInfo, funcConfig)
    print("[ActAddUpCharge_isOpen] 进入了判断")
    return ActAddUpCharge_IsOpen(human)
end

-- 活动剩余时间
function getLeftTime()
    print("[ActAddUpCharge_getLeftTime] 获取剩余时间")

    return ActAddUpCharge_GetLeftTime()
end

-- 是否存在红点
function isRed(human)
    print("[ActAddUpCharge_isRed] 获取红点")
    return ActAddUpCharge_CheckRed(human)
end

function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

----------------------------------------- 客户端协议请求 -------------------------------------
-- 请求活动协议
function ActAddUpCharge_Query(human)
    if not human then
        return
    end

    local nCreatRet = ActAddUpCharge_CreateDB(human)
    if false == nCreatRet then
        ActAddUpCharge_PrintInfo("[ActAddUpCharge_Query]", "初始化DB数据失败")
        return
    end

    local tPrize = tPrizeByID
    if not tPrize then
        return
    end

    local nNowMoney = ActAddUpCharge_GetDBCharge(human)
    local bShowAll = ActAddUpCharge_Show(human)

    local tMsgData = Msg.gc.GC_PRESEN_OPEN_ADDUP_CHARGE_QUERY
    tMsgData.nNowMoney = nNowMoney
    tMsgData.leftTime = ActAddUpCharge_GetLeftTime()
    tMsgData.list[0] = 0

    for nID, v in pairs(tPrize) do
        if 1 == v.nShow or (0 == v.nShow and true == bShowAll ) then
            tMsgData.list[0] = tMsgData.list[0] + 1
            local tPrizeData = tMsgData.list[tMsgData.list[0]]
            tPrizeData.nID = nID
            tPrizeData.nNeedMoney = v.nMoney
            tPrizeData.nState = ActAddUpCharge_GetDBIDState(human, nID)
            
            local nPrizeLne = #v.tPrize
            tPrizeData.item[0] = nPrizeLne
            
            -- ActAddUpCharge_PrintInfo("[ActAddUpCharge_Query]", "遍历的奖励信息 nID = "
            --         .. nID .. " nState = ".. tPrizeData.nState.." nPrizeLne = "..nPrizeLne)
    
            for j = 1, nPrizeLne do
                local nGoodsID = v.tPrize[j][1]
                local nGoodsNum = v.tPrize[j][2]
                -- ActAddUpCharge_PrintInfo("[ActAddUpCharge_Query]", "物品信息 nGoodsID = "..nGoodsID .. " nGoodsNum = "..nGoodsNum)
    
                Grid.makeItem(tPrizeData.item[j], nGoodsID, nGoodsNum)
            end
        end
    end

    Msg.send(tMsgData, human.fd)
    --ActAddUpCharge_PrintInfo("[ActAddUpCharge_Query]", "获取到的奖励长度为 nIndex = "..tMsgData.list[0])
end


-- 请求领取奖励
function ActAddUpCharge_GetPrize(human, nID)
    if not human or 0 >= nID then
        return
    end

    if not tPrizeByID or not tPrizeByID[nID] then
        return
    end

    local nNowMoney = ActAddUpCharge_GetDBCharge(human)
    local nNowState = ActAddUpCharge_GetDBIDState(human, nID)
    if ADDUPCHARG_STATE_CANGET ~= nNowState then
        ActAddUpCharge_PrintInfo("ActAddUpCharge_GetPrize", "请求领取的奖励状态不正确 nID = "..nID)
        return
    end

    local tGoodsInfo = tPrizeByID[nID].tPrize

    -- 设置状态
    if false == ActAddUpCharge_SetDBIDState(human, nID, ADDUPCHARG_STATE_FINISH) then
        ActAddUpCharge_PrintInfo("ActAddUpCharge_GetPrize", "设置数据状态失败 nID = "..nID)
        return
    end

    -- 发奖励
    for i = 1, #tGoodsInfo do
        local nGoodsID = tGoodsInfo[i][1]
        local nGoodsNum = tGoodsInfo[i][2]

        BagLogic.addItem(human, nGoodsID, nGoodsNum,"open_server_PowerUpPrize")
    end

	BagLogic.sendItemGetList(human, tGoodsInfo, "open_server_PowerUpPrize")

    YunYingLogic.sendBanner(human)
	for k, v in pairs(funcID) do
	    YunYingLogic.updateIcon(YYInfo[k], human)
	    YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3605)
        break
    end
    
    ActAddUpCharge_Query(human)
end


----------------------------------------- 其他模块调用 -------------------------------------
-- 用于起服时映射奖励ID对应配置
function Init()
    local tPrize = ActAddUpCharge_GetConfig()
    if not tPrize then
        ActAddUpCharge_PrintInfo("Init", "起服获取不到配置")
        return
    end
    
    tPrizeByID = {}
    nShowAllMoney = 0

    for nID, v in pairs(tPrize) do
        if tPrizeByID[nID] then
            ActAddUpCharge_PrintInfo("Init", "配置了重复的奖励ID")
            return
        end

        tPrizeByID[nID] = v

        if 1 == v.nShow then
            nShowAllMoney = math.max(nShowAllMoney, v.nMoney)
        end
    end
end

-- 充值回调
function onCharge(human, price, funcID, buyID)
    if not human or 0 >= price then
        return
    end

    if false == ActAddUpCharge_IsOpen(human) then
        return
    end

    local nNowMoney = ActAddUpCharge_GetDBCharge(human)
    nNowMoney = nNowMoney + price

    ActAddUpCharge_SetDBCharge(human, nNowMoney)

    if not tPrizeByID then
        return
    end

    local nSendClient = false
    for nID, v in pairs(tPrizeByID) do
        local nState = ActAddUpCharge_GetDBIDState(human, nID)

        if ADDUPCHARGEGIFT_STATE_NONE == nState then
            if nNowMoney >= v.nMoney then
                ActAddUpCharge_SetDBIDState(human, nID, ADDUPCHARG_STATE_CANGET)
                nSendClient = true
            end
        end
    end

    if true == nSendClient then
        YunYingLogic.sendBanner(human)
    end
end

-- 登录回调
function onLogin(human, funcID)
    -- 在活动期间
    local bOpen = ActAddUpCharge_IsOpen(human)
    if true == ActAddUpCharge_IsOpen(human) then
        if not human.db.AddUpChargeSendMail then
            human.db.AddUpChargeSendMail = 1
        end
        return
    end

    if true == bOpen then
        print("[onLogin] 累充登录判断开启中")
    end

    if not tPrizeByID then
        return
    end

    if not human.db.AddUpChargeSendMail  then
        return
        -- human.db.AddUpChargeSendMail = 0
    end

    if human.db.AddUpChargeSendMail ~= 1 then
        return
    end

    human.db.AddUpChargeSendMail = 2

    local tMailConfig = MailExcel.mail[nMailID]
    if not tMailConfig then
        return
    end

    local items = { }
    for nID, v in pairs(tPrizeByID) do
        local nState = ActAddUpCharge_GetDBIDState(human, nID)
        if nState == ADDUPCHARG_STATE_CANGET then
            for _, data in pairs(v.tPrize) do
                items[#items + 1] = { data[1], data[2] }
            end
            ActAddUpCharge_SetDBIDState(human, nID, ADDUPCHARG_STATE_FINISH)
        end
    end

    if nil ~= _G.next(items) then
        local title = tMailConfig.title
        local content = tMailConfig.content
        local senderName = tMailConfig.senderName
        MailManager.add(MailManager.SYSTEM, human.db._id, title, content, items, senderName)
    end
end