--------------------------------
-- 文件名       ：  WeekendLoopActUseGuYu.lua
-- 文件说明     ：  周末冲刺活动-消费古玉
-- 创建时间     :   2024/12/02
-- 创建人       :   FC
--------------------------------

local Util = require("common.Util")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local MailExcel = require("excel.mail")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local WeekLoopActDef = require("WeekendLoopActivity.WeekendLoopActDefine")
local WeekLoopActCof = require("excel.WeekLoopAct")
local CommonDB = require("common.CommonDB")
local MailManager = require("mail.MailManager")
local Grid = require("bag.Grid")
local HeroGrid = require("hero.HeroGrid")
local HeroLogic = require("hero.HeroLogic")
local BagLogic = require("bag.BagLogic")
local Log = require("common.Log")
local HeroExcel = require("excel.hero")
local WeekendLoopActManger = require("WeekendLoopActivity.WeekendLoopActManager")

----------------------------------------- 内部处理开始 -------------------------------------
-- 写日志
local function WeekActGuYu_WriteLog(human, szFuncName, sztext)
    Log.write(Log.LOGID_OSS_WEEKLOOP_ACT, szFuncName..sztext.." _id = "..human.db._id.." name = "..human.db.name)
end

-- 下发数据
local function WeekActGuYu_SendData(tMsgData, fd)
    Msg.send(tMsgData, fd)
end

-- 获取配置
local function WeekActGuYu_GetConfig()
    if not WeekLoopActCof then
        return nil
    end

    return WeekLoopActCof.GuYu
end

-- 初始化古玉数量
local function WeekActHeroStar_ResetGuYuNum(human)
    if not human then
        print("[WeekActHeroStar_SetHeroID] 参数不正确")
        return false
    end

    human.db.nWeekUseGuYu = 0

    return true
end

-- 获取古玉数量
local function WeekActHeroStar_GetGuYuNum(human)
    if not human then
        print("[WeekActHeroStar_SetHeroID] 参数不正确")
        return -1
    end

    if not human.db.nWeekUseGuYu then
        local bRet = WeekActHeroStar_ResetGuYuNum(human)
        if false == bRet then
            WeekActGuYu_WriteLog(human, "[WeekActHeroStar_GetGuYuNum]", "获取古玉数量,不存在对应字段,重新初始化失败")
            return -1
        end

        WeekActGuYu_WriteLog(human, "[WeekActHeroStar_GetGuYuNum]", "获取古玉数量,不存在对应字段,初始化顺序有问题, 重新初始化成功")
    end

    return human.db.nWeekUseGuYu
end

-- 设置古玉数量
local function WeekActHeroStar_SetGuYuNum(human, nValue)
    if not human then
        print("[WeekActHeroStar_SetHeroID] 参数不正确")
        return
    end

    local nNowGuYu = WeekActHeroStar_GetGuYuNum(human)
    if -1 >= nNowGuYu then
        WeekActGuYu_WriteLog(human, "[WeekActHeroStar_SetGuYuNum]", "获取古玉数量失败")
        return
    end

    local nNewGuYu = nNowGuYu + nValue
    if 0 > nNewGuYu then
        nNewGuYu = 0
    end

    human.db.nWeekUseGuYu = nNewGuYu
end

-- 初始化奖励信息
local function WeekActGuYu_ResetPrize(human)
    if not human then
        return false
    end

    local tConfig = WeekActGuYu_GetConfig()
    if not tConfig then
        return false
    end

    if not human.db.tWeekGuYuPrize then
        human.db.tWeekGuYuPrize = {}
    end

    for nID, v in pairs(tConfig) do
        human.db.tWeekGuYuPrize[nID] = WeekLoopActDef.WEEKACT_STATE_NONE
    end
    
    return true
end

-- 获取奖励表
local function WeekActGuYu_GetDBPrize(human)
    if not human then
        return nil
    end

    if not human.db.tWeekGuYuPrize then
        return nil
    end
    
    return human.db.tWeekGuYuPrize
end

-- 获取奖励ID状态
local function WeekActGuYu_GetPrizeStatus(human, nID)
    if not human then
        return WeekLoopActDef.WEEKACT_STATE_NONE
    end

    local tPrize = WeekActGuYu_GetDBPrize(human)
    if not tPrize or not tPrize[nID] then
        print("[WeekActGuYu_GetPrizeStatus] 不存在对应的ID nID = "..nID)
        return WeekLoopActDef.WEEKACT_STATE_NONE
    end
    
    return tPrize[nID]
end

-- 设置奖励ID状态
local function WeekActGuYu_SetPrizeStatus(human, nID, nStatus)
    if not human then
        return false
    end

    local tPrize = WeekActGuYu_GetDBPrize(human)
    if not tPrize or not tPrize[nID] then
        return false
    end

    tPrize[nID] = nStatus

    return true
end


----------------------------------------- 外部调用 -------------------------------------
-- 重置数据
function WeekActGuYu_ResetData(human)
    if not human then
        return
    end

    -- 重置奖励信息
    if false == WeekActGuYu_ResetPrize(human) then
        print("[WeekActGuYu_ResetData] 重置古玉奖励数据失败")
        return
    end

    -- 重置消耗古玉数量
    WeekActHeroStar_ResetGuYuNum(human)

    WeekActGuYu_WriteLog(human, "[WeekActGuYu_ResetData]", "古玉奖励相关重置完成")

    print("[WeekActGuYu_ResetData] 古玉 数据重置结束 ")
end

-- 是否有红点
function isRed(human)
    local tPrize = WeekActGuYu_GetConfig()
    if not tPrize then
        return false
    end

    for nID, v in pairs(tPrize) do
        if WeekLoopActDef.WEEKACT_STATE_CANGET == WeekActGuYu_GetPrizeStatus(human, nID) then
            return true
        end
    end

    return false
end

-- 消耗古玉回调
function WeekActGuYu_UseGuYu(human, nGuYu)
    if not human or 0 >= nGuYu then
        return
    end

    local DBID = human.db._id
    local szName = human.db.name
    local tConfig = WeekActGuYu_GetConfig()
    if not tConfig then
        print("[WeekActGuYu_UseGuYu] 获取不到配置 DBID = "..DBID.." name = "..szName)
        return
    end

    WeekActHeroStar_SetGuYuNum(human, nGuYu)
  
    local nNowGuYu = WeekActHeroStar_GetGuYuNum(human)
    print("[onDecZuanshi] 消耗古玉回调开始 nNowGuYu = "..nNowGuYu)

    local bChange = false
    for nID, v in pairs(tConfig) do
        local nStatus = WeekActGuYu_GetPrizeStatus(human, nID)
        print("[onDecZuanshi] 消耗古玉回调开始 nNowGuYu = "..nNowGuYu.." nStatus = "..nStatus)
        
        if nNowGuYu >= v.guyu and WeekLoopActDef.WEEKACT_STATE_NONE == nStatus then
            bChange = true
            WeekActGuYu_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_CANGET)
        end
    end

    print("[onDecZuanshi] 古玉回调处理完成 nNowGuYu = "..nNowGuYu)

    if true == bChange then
        print("[onDecZuanshi] 古玉回调处理完成 开始请求 nNowGuYu = "..nNowGuYu)
        WeekActGuYu_Query(human)
        WeekendLoopActManger.WeekLoopACT_SendActInfo(human)
    end
end

----------------------------------------- 客户端请求 -------------------------------------
-- 客户端请求-古玉信息
function WeekActGuYu_Query(human)
    if not human then
        return
    end

    local DBID = human.db._id
    local szName = human.db.name

    local tConfig = WeekActGuYu_GetConfig()
    if not tConfig then
        print("[WeekActGuYu_Query] 获取不到配置 DBID = "..DBID.." name = "..szName)
        return
    end

    local tMsgData = Msg.gc.GC_WEEKLOOP_ACT_GUYUQUERY
    tMsgData.nGuYu = WeekActHeroStar_GetGuYuNum(human)
    tMsgData.list[0] = 0

    for nID, v in pairs(tConfig) do
        tMsgData.list[0] = tMsgData.list[0] + 1
        local tData = tMsgData.list[tMsgData.list[0]]
        tData.nID = nID
        tData.nGuYu = v.guyu
        tData.nState = WeekActGuYu_GetPrizeStatus(human, nID)

        tData.item[0] = #v.prize

        for nIndex, tItem in ipairs(v.prize) do
            local nGoodsID = tItem[1]
            local nGoodsNum = tItem[2]

            Grid.makeItem(tData.item[nIndex], nGoodsID, nGoodsNum)
        end
    end

    WeekActGuYu_SendData(tMsgData, human.fd)
end

-- 客户端请求领取奖励
function WeekActGuYu_GetPrize(human)
    if not human then
        return
    end

    local DBID = human.db._id
    local szName = human.db.name

    local tConfig = WeekActGuYu_GetConfig()
    if not tConfig then
        print("[WeekActGuYu_GetPrize] 获取不到配置 DBID = "..DBID.." name = "..szName)
        return
    end

    local tItemList = {}
    for nID, v in pairs(tConfig) do
        if WeekLoopActDef.WEEKACT_STATE_CANGET == WeekActGuYu_GetPrizeStatus(human, nID) then
            if false == WeekActGuYu_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_FINISH) then
                print("[WeekActGuYu_GetPrize] 奖励领取失败 nID = "..nID)
                WeekActGuYu_WriteLog(human, "[WeekActHeroStar_GetPrize]", "领取奖励, 设置奖励状态失败 nID = "..nID)
                break
            end

            for _, data in ipairs(v.prize) do
                local nItemID = data[1]
                local nItemNum = data[2]

                tItemList[nItemID] = tItemList[nItemID] or 0
                tItemList[nItemID] = tItemList[nItemID] + nItemNum
            end
        end
    end

    if nil ~= _G.next(tItemList) then
        local tGoodsInfo = {}
        for k, v in pairs(tItemList) do
            table.insert(tGoodsInfo, {k,v})

            -- 获取奖励写日志
            WeekActGuYu_WriteLog(human, "[WeekActHeroStar_GetPrize]", "玩家获取奖励 nGoodsID = "..k.." nGoodsNum = "..v)
        end

        BagLogic.addItemList(human, tGoodsInfo, "week_loop_act")

        BagLogic.sendItemGetList(human, tItemList, "week_loop_act")
    end

    WeekActGuYu_Query(human)
    WeekendLoopActManger.WeekLoopACT_SendActInfo(human)
end