--------------------------------
-- 文件名       ：  WeekendLoopActCard.lua
-- 文件说明     ：  周末冲刺活动-抽卡
-- 创建时间     :   2024/12/03
-- 创建人       :   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")

----------------------------------------- 内部处理开始 -------------------------------------
tCardPrize = nil

-- 写日志
local function WeekActCard_WriteLog(human, szFuncName, sztext)
    Log.write(Log.LOGID_OSS_WEEKLOOP_ACT, szFuncName..sztext.." _id = "..human.db._id.." name = "..human.db.name)
end

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

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

    return WeekLoopActCof.Card
end

-- 获取配置
local function WeekActCard_GetConfigByType(nType)
    if not tCardPrize or not tCardPrize[nType] then
        return nil
    end

    return tCardPrize[nType]
end

-- 初始化抽卡类型
local function WeekActCard_ResetCardType(human)
    if not human then
        print("[WeekActCard_ResetCardType] 参数不正确")
        return false
    end

    if not human.db.nWeekCardType then
        human.db.nWeekCardType = 1
    else
        -- math.fmod()
        local nLen = (human.db.nWeekCardType + 1) % WeekLoopActDef.WEEKACT_CARD_TYPE_LEN
        human.db.nWeekCardType = (0 == nLen) and WeekLoopActDef.WEEKACT_CARD_TYPE_LEN or nLen
    end

    return true
end

-- 获取抽卡类型
local function WeekActCard_GetCardType(human)
    if not human then
        print("[WeekActCard_GetCardType] 参数不正确")
        return -1
    end

    return WeekendLoopActManger.WeekLoopACT_GetCardBatch()
    -- if not human.db.nWeekCardType then
    --     local bRet = WeekActCard_ResetCardType(human)
    --     if false == bRet then
    --         WeekActCard_WriteLog(human, "[WeekActCard_GetCardType]", "获取抽卡类型,不存在对应字段,重新初始化失败")
    --         return -1
    --     end

    --     WeekActCard_WriteLog(human, "[WeekActCard_GetCardType]", "获取抽卡类型,不存在对应字段,初始化顺序有问题, 重新初始化成功")
    -- end

    -- return human.db.nWeekCardType
end

-- 通过人物抽卡类型获取配置
local function WeekActCard_GetConfigByHuman(human)
    if not human then
        return nil
    end

    local nNowType = WeekActCard_GetCardType(human)
    if -1 >= nNowType then
        return nil
    end

    return WeekActCard_GetConfigByType(nNowType)
end

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

    local nNowType = WeekActCard_GetCardType(human)
    if -1 >= nNowType then
        print("[WeekActCard_ResetPrize] 获取抽卡类型失败")
        return false
    end

    local tConfig = WeekActCard_GetConfigByType(nNowType)
    if not tConfig then
        return false
    end

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

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

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

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

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

    local tPrize = WeekActCard_GetDBPrize(human)
    if not tPrize or not tPrize[nID] then
        return WeekLoopActDef.WEEKACT_STATE_NONE
    end
    
    return tPrize[nID]
end

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

    local tPrize = WeekActCard_GetDBPrize(human)
    if not tPrize or not tPrize[nID] then
        print("[WeekActCard_SetPrizeStatus] 获取不到对应的奖励表 "..nID.." nStatus = "..nStatus)
        return false
    end

    tPrize[nID] = nStatus

    return true
end

-- 初始化抽卡数量
local function WeekActCard_ResetCardNum(human)
    if not human then
        print("[WeekActCard_ResetCardNum] 参数不正确")
        return false
    end

    human.db.nWeekCardNum = 0

    return true
end

-- 获取抽卡数量
local function WeekActCard_GetCardNum(human)
    if not human then
        print("[WeekActCard_GetCardNum] 参数不正确")
        return -1
    end

    if not human.db.nWeekCardNum then
        local bRet = WeekActCard_ResetCardNum(human)
        if false == bRet then
           WeekActCard_WriteLog(human, "[WeekActCard_GetGuYuNum]", "获取抽卡数量,不存在对应字段,重新初始化失败")
            return -1
        end

       WeekActCard_WriteLog(human, "[WeekActCard_GetGuYuNum]", "获取抽卡数量,不存在对应字段,初始化顺序有问题, 重新初始化成功")
    end

    return human.db.nWeekCardNum
end

-- 设置抽卡数量
local function WeekActCard_SetCardNum(human, nValue)
    if not human then
        print("[WeekActCard_SetCardNum] 参数不正确")
        return
    end

    local nNowCard = WeekActCard_GetCardNum(human)
    if -1 >= nNowCard then
       WeekActCard_WriteLog(human, "[WeekActCard_SetCardNum]", "获取抽卡数量失败")
        return
    end

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

    human.db.nWeekCardNum = nNewGuYu
end

----------------------------------------- 外部调用 -------------------------------------
function Init()
    tCardPrize = {}
    local tConfig = WeekActCard_GetConfig()
    if not tConfig then
        return false
    end

    for nID, v in pairs(tConfig) do
        local nType = v.nType
        if not tCardPrize[nType] then
            tCardPrize[nType] = {}
        end

        local nTrueID = nID % WeekLoopActDef.WEEKACT_CARD_PRIZEID_LEN
        tCardPrize[nType][nTrueID] = v
    end

    return true
end

-- 重置数据
function WeekActCard_ResetData(human)
    if not human then
        return
    end

    -- 先更新抽卡类型
    --WeekActCard_ResetCardType(human)

    -- 重置抽卡数量
    WeekActCard_ResetCardNum(human)

    -- 重置奖励信息
    if false == WeekActCard_ResetPrize(human) then
        print("[WeekActCard_ResetData] 重置抽卡奖励数据失败")
        return
    end

    WeekActCard_WriteLog(human, "[WeekActCard_ResetData]", "抽卡奖励相关重置完成")

    print("[WeekActCard_ResetData] 抽卡 数据重置结束 ")
end

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

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

    return false
end

-- 抽卡回调
function WeekActCard_UseCard(human, nAddNum, nType)
    if not human or 0 >= nAddNum then
        return
    end

    WeekActCard_WriteLog(human, "WeekActCard_UseCard", "玩家进行了抽卡回调开始 nAddNum = "..nAddNum.." 类型 nType = "..nType)

    local nNowType = WeekActCard_GetCardType(human)
    print("[WeekActCard_UseCard] nNowType = "..nNowType.." nType = "..nType)
    if nNowType ~= nType then
        return
    end

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

    WeekActCard_SetCardNum(human, nAddNum)
    
    local nNowCard = WeekActCard_GetCardNum(human)
    if -1 >= nNowCard then
        WeekActCard_WriteLog(human, "[WeekActCard_UseCard]", "获取玩家当前抽卡次数失败 nAddNum = "..nAddNum)
        print("[WeekActCard_UseCard] 获取不到抽卡次数 DBID = "..DBID.." name = "..szName.." nAddNum = "..nAddNum)
        return
    end

    local bChange = false
    for nID, v in pairs(tConfig) do
        local nStatus = WeekActCard_GetPrizeStatus(human, nID)
        if nNowCard >= v.nNum and WeekLoopActDef.WEEKACT_STATE_NONE == nStatus then
            bChange = true
            local bRet = WeekActCard_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_CANGET)
            if false == bRet then
                print("[WeekActCard_UseCard] 设置失败 nID = "..nID.." nStatus = "..nStatus.." num = "..v.nNum)
            end
        end
    end

    if true == bChange then
        WeekActCard_Query(human)
        WeekendLoopActManger.WeekLoopACT_SendActInfo(human)
    end

    WeekActCard_WriteLog(human, "WeekActCard_UseCard", "玩家进行了抽卡回调处理结束 nAddNum = "..nAddNum.." 类型 nType = "..nType)
end

-- 获取名称
function GetName(human)
    if not human then
        return WeekLoopActDef.TWEEKACT_CARD_PANELID_NAME[1]
    end

    local nNowCardType = WeekActCard_GetCardType(human)
    if -1 >= nNowCardType then
        return WeekLoopActDef.TWEEKACT_CARD_PANELID_NAME[1]
    end

    if not WeekLoopActDef.TWEEKACT_CARD_PANELID_NAME[nNowCardType] then
        return WeekLoopActDef.TWEEKACT_CARD_PANELID_NAME[1]
    end

    return WeekLoopActDef.TWEEKACT_CARD_PANELID_NAME[nNowCardType]
end

----------------------------------------- 客户端请求 -------------------------------------
-- 客户端请求-抽卡信息
function WeekActCard_Query(human)
    if not human then
        return
    end

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

    local tConfig = WeekActCard_GetConfigByHuman(human)
    if not tConfig then
        WeekActCard_WriteLog(human, "[WeekActCard_Query]", "获取奖励配置失败重新检查是否生成 ")
        if nil == tCardPrize then
            local bRet = Init()
            if false == bRet then
                WeekActCard_WriteLog(human, "[WeekActCard_Query]", "重置奖励配置表失败 ")
            else
                WeekActCard_WriteLog(human, "[WeekActCard_Query]", "重置奖励配置表成功 ")
            end
        end

        tConfig = WeekActCard_GetConfigByHuman(human)
        if not tConfig then
            WeekActCard_WriteLog(human, "[WeekActCard_Query]", "还是获取不到奖励配置")
            return
        end
    end

    local tMsgData = Msg.gc.GC_WEEKLOOP_ACT_CARDQUERY
    tMsgData.byType = WeekActCard_GetCardType(human)
    tMsgData.nNowNum = WeekActCard_GetCardNum(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.nNum = v.nNum
        tData.nID = nID
        tData.desc = v.szdesc
        tData.nState = WeekActCard_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

    WeekActCard_SendData(tMsgData, human.fd)
end

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

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

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

    local tItemList = {}
    for nID, v in pairs(tConfig) do
        if WeekLoopActDef.WEEKACT_STATE_CANGET == WeekActCard_GetPrizeStatus(human, nID) then
            if false == WeekActCard_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_FINISH) then
                print("[WeekActCard_GetPrize] 奖励领取失败 nID = "..nID)
                WeekActCard_WriteLog(human, "[WeekActCard_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})

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

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

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

    WeekActCard_Query(human)
    WeekendLoopActManger.WeekLoopACT_SendActInfo(human)
end

function onLogin(human)
    if not human.db.nWeekCardNum or not human.db.tWeekCardPrize then
        return
    end

    local nNowCard = WeekActCard_GetCardNum(human)
    if -1 >= nNowCard then
        return
    end

    local tConfig = WeekActCard_GetConfigByHuman(human)
    if tConfig then
        for nID, v in pairs(tConfig) do
            local nStatus = WeekActCard_GetPrizeStatus(human, nID)
            if nNowCard >= v.nNum and WeekLoopActDef.WEEKACT_STATE_NONE == nStatus then
                local bRet = WeekActCard_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_CANGET)
                if false == bRet then
                    print("[WeekActCard_UseCard] 设置失败 nID = "..nID.." nStatus = "..nStatus.." num = "..v.nNum)
                end
            end
        end
    end
end