--------------------------------
-- 文件名       :   CommonActCharge.lua
-- 文件说明     :   通用节日活动-连充豪礼
-- 创建时间     :   2025/09
-- 创建人       :   FC
--------------------------------

local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local AbsActLogic = require("absAct.AbsActLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local Config = require("excel.commonact").findprize
local CommonDefine = require("common.CommonDefine")
local Util = require("common.Util")
local YunYingLogic = require("yunying.YunYingLogic")
local AbsActExcel = require("excel.absAct")

local COMMONACTFINDABSID = 7504        -- 对应ABS活动ID
local COMMONACTFINDUSEITEM = 1243      -- 寻宝使用道具ID

local COMMONACTDOMAXNUM = nil    -- 最大抽取保底次数

----------------------------------------- 内部处理开始 -------------------------------------
local function ComActFindTreasure_CreateDB(human)
    if not human.db.absAct[COMMONACTFINDABSID] then
        human.db.absAct[COMMONACTFINDABSID] = {}
    end
    
    human.db.absAct[COMMONACTFINDABSID].tExtract = {
        nFreeNum = 1,          -- 免费次数
        nFreeRefreshTime = os.time(),   -- 免费刷新次数
        nAllNum = 0,           -- 总次数
    }
end

-- 检测并创建DB数据
local function ComActFindTreasure_CheckAndCreateDB(human)
    if not human.db.absAct[COMMONACTFINDABSID] or not human.db.absAct[COMMONACTFINDABSID].tExtract then
        ComActFindTreasure_CreateDB(human)
    end
end

local function ComActFindTreasure_GetDoMaxNum()
    if nil ~= COMMONACTDOMAXNUM then
        return COMMONACTDOMAXNUM
    end

    for _, v in ipairs(Config) do
        if not COMMONACTDOMAXNUM then
           COMMONACTDOMAXNUM = v.nNeedNum
        else
            COMMONACTDOMAXNUM = math.max(COMMONACTDOMAXNUM, v.nNeedNum)
        end
    end

    return COMMONACTDOMAXNUM
end

-- 获取免费次数
local function ComActFindTreasure_GetDBFreeNum(human)
    return human.db.absAct[COMMONACTFINDABSID].tExtract.nFreeNum
end

-- 设置免费次数
local function ComActFindTreasure_SetDBFreeNum(human, nNum)
    human.db.absAct[COMMONACTFINDABSID].tExtract.nFreeNum = nNum
end

-- 获取刷新时间
local function ComActFindTreasure_GetDBFreeRefreshTime(human)
    return human.db.absAct[COMMONACTFINDABSID].tExtract.nFreeRefreshTime
end

-- 设置刷新时间
local function ComActFindTreasure_SetDBFreeRefreshTime(human, nTime)
    human.db.absAct[COMMONACTFINDABSID].tExtract.nFreeRefreshTime = nTime
end

-- 获取总次数
local function ComActFindTreasure_GetDBAllNum(human)
    return human.db.absAct[COMMONACTFINDABSID].tExtract.nAllNum
end

-- 设置总次数
local function ComActFindTreasure_SetDBAllNum(human, nNum)
    human.db.absAct[COMMONACTFINDABSID].tExtract.nAllNum = nNum
end

-- 抽取动作
local function ComActFindTreasure_FindDo(human, nDoNum)
    local nAllNum = ComActFindTreasure_GetDBAllNum(human)
    local nMaxDoNum = ComActFindTreasure_GetDoMaxNum()

    local nAllWeight, tItemInfo = 0, {}
    for _, v in ipairs(Config) do
        nAllWeight = nAllWeight + v.nWeight
        local bBaoDi = false
        if nMaxDoNum == v.nNeedNum then
            bBaoDi = true
        end

        table.insert(tItemInfo, {nWeight = nAllWeight, tPrize = v.tPrize, bBaoDi = bBaoDi});
    end
    
    local tGoodsInfo = {}
    for i = 1, nDoNum, 1 do
        local bUseBaoDi = false
        nAllNum = nAllNum + 1
        if nAllNum >= nMaxDoNum then
            nAllNum = 0
            bUseBaoDi = true
        end

        local nRandWeight = math.random(1, nAllWeight)
        for _, v in ipairs(tItemInfo) do
            if true == bUseBaoDi then
                if v.bBaoDi == true then
                    if not tGoodsInfo[v.tPrize[1]] then
                        tGoodsInfo[v.tPrize[1]] = 0
                    end
    
                    tGoodsInfo[v.tPrize[1]] = tGoodsInfo[v.tPrize[1]]  + v.tPrize[2]
                    break
                end
            else
                if nRandWeight <= v.nWeight then
                    if not tGoodsInfo[v.tPrize[1]] then
                        tGoodsInfo[v.tPrize[1]] = 0
                    end

                    tGoodsInfo[v.tPrize[1]] = tGoodsInfo[v.tPrize[1]]  + v.tPrize[2]
                    if v.bBaoDi == true then
                        nAllNum = 0
                    end

                    break
                end
            end
        end
    end

    ComActFindTreasure_SetDBAllNum(human, nAllNum)
    return true, tGoodsInfo
end

----------------------------------------- 内部处理结束 -------------------------------------
function isOpen(human, YYInfo, funcConfig)
    local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID and funcConfig.funcID or COMMONACTFINDABSID)
    if not state then
        print("[CommonActFindTreasure_isOpen] 当前活动未开启")
        return
    end
    
    -- print("[CommonActFindTreasure_isOpen] 进入判断 endTime = "..endTime.." startTime = "..startTime)
    return state, endTime, startTime
end

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

function isRed(human)
    local state, nEndTime, nOpenTime = AbsActLogic.isStarted(human, COMMONACTFINDABSID)
    if not state then
        return false
    end

    ComActFindTreasure_CheckAndCreateDB(human)

    local nFreeNum  = ComActFindTreasure_GetDBFreeNum(human)
    return nFreeNum > 0
end

function onZero(human, funcID)
    local state, nEndTime, nOpenTime = AbsActLogic.isStarted(human, COMMONACTFINDABSID)
    if not state then
        return
    end

    ComActFindTreasure_SetDBFreeNum(human, 1)
    ComActFindTreasure_SetDBFreeRefreshTime(human, os.time())
end

function onLogin(human, funcID)
    local state, nEndTime, nOpenTime = AbsActLogic.isStarted(human, COMMONACTFINDABSID)
    if not state then
        return
    end

    ComActFindTreasure_CheckAndCreateDB(human)

    local nFreeRefreshTime = ComActFindTreasure_GetDBFreeRefreshTime(human)
    if true ~= Util.isSameDayByTimes(nFreeRefreshTime, os.time()) then
        ComActFindTreasure_SetDBFreeNum(human,  1)
        ComActFindTreasure_SetDBFreeRefreshTime(human, os.time())
    end
end

------------------------------------ 客户端请求处理开始 ---------------------------------
function CommonActFindTreasure_Query(human)
    local nStatus = AbsActLogic.isStarted(human, COMMONACTFINDABSID)
    if not nStatus then
        return
    end

    ComActFindTreasure_CheckAndCreateDB(human)

    local msgRet = Msg.gc.GC_ABS_FESTIVAL_SEVENDAY_CARD_QUERY
    msgRet.nFreeNum = ComActFindTreasure_GetDBFreeNum(human)
    msgRet.nNowNum = ComActFindTreasure_GetDBAllNum(human)
    msgRet.nMaxNum = ComActFindTreasure_GetDoMaxNum()
    Grid.makeItem(msgRet.tUseItem, COMMONACTFINDUSEITEM, 1)

    msgRet.tPrize[0] = #Config
    for index, v in ipairs(Config) do
        local tData = msgRet.tPrize[index]
        tData.nID = index
        tData.nRare = v.nRare
      
        Grid.makeItem(tData.tPrize, v.tPrize[1], v.tPrize[2])
    end

    Msg.send(msgRet, human.fd)
end

function CommonActFindTreasure_Do(human, nType)
    local nStatus = AbsActLogic.isStarted(human, COMMONACTFINDABSID)
    if not nStatus then
        return
    end

    if 1 ~= nType and 2 ~= nType then
        print("[CommonActFindTreasure_Do] 上传的类型不正确 nType = "..nType)
        return
    end

    local nUseNum = 1 == nType and 1 or 10

    ComActFindTreasure_CheckAndCreateDB(human)

    local nFreeNum = ComActFindTreasure_GetDBFreeNum(human)
    local nDelNum = nUseNum - nFreeNum
    if nDelNum < 0 then
        nDelNum = 0
    end
    if nDelNum > 0 and not BagLogic.checkItemCnt(human, COMMONACTFINDUSEITEM, nDelNum) then
        print("[CommonActFindTreasure_Do] 消耗的物品不足 nFreeNum = "..nFreeNum.." nDelNum = "..nDelNum)
        return
    end

    local bRet, tItem = ComActFindTreasure_FindDo(human, nUseNum)
    if false == bRet then
        print("[CommonActFindTreasure_Do] 抽取动作执行失败")
        return
    end

    if nFreeNum > 0 then
        nFreeNum = nFreeNum >= nUseNum and nFreeNum - nUseNum or 0
        ComActFindTreasure_SetDBFreeNum(human, nFreeNum)
    end

    BagLogic.delItem(human, COMMONACTFINDUSEITEM, nDelNum, "CommonActFindTreasure")

    local tGoods = {}
    for nID, nNum in pairs(tItem) do
        table.insert(tGoods, {nID, nNum})
    end

    if nil ~= _G.next(tGoods) then
       BagLogic.addItemList(human, tGoods, "CommonActFindTreasure")
       CommonActFindTreasure_Query(human)
    end

    YunYingLogic.sendBanner(human)
    local config = AbsActExcel.absActivity[COMMONACTFINDABSID]
    YunYingLogic.sendGroupUpdate(YYInfo[COMMONACTFINDABSID], human, config.panelID)
end