-- 周年活动 - 活跃转转乐
-- db
--[=[
    human.db.absAct[ACT_ID] = {
        lotteryTimes = 0,  -- 可用抽奖次数（日常/每周达100活跃各+1，可叠加）
    }
]=]--

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 YunYingLogic = require("yunying.YunYingLogic")
local AbsActExcel  = require("excel.absAct")
local RewardCfg    = require("excel.anniversaryAct").activeWheel  -- 活跃转转乐奖励配置表

local LOGTYPE = "AnniversaryActiveWheel"
local ACT_ID  = 7701  -- 活跃转转乐活动ID（按实际分配）


local function getData(human)
    return human.db.absAct[ACT_ID]
end

local function addTimes(human, val)
    local d = getData(human)
    d.lotteryTimes = (d.lotteryTimes or 0) + val
end


local function isOpenAct(human, funcID)
    return AbsActLogic.isStarted(human, funcID or ACT_ID)
end

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

local function doLottery()
    local totalWeight = 0
    for _, row in ipairs(RewardCfg) do
        totalWeight = totalWeight + row.weight
    end
    if totalWeight <= 0 then return 0 end

    local r, acc = math.random(1, totalWeight), 0
    for i, row in ipairs(RewardCfg) do
        acc = acc + row.weight
        if r <= acc then return i end
    end
    return 0
end

local function doMultiLottery(count)
    local results = {}
    for _ = 1, count do
        local idx = doLottery()
        if idx == 0 then return nil end
        local row = RewardCfg[idx]
        results[#results + 1] = { itemId = row.reward[1], itemNum = row.reward[2] }
    end
    return results
end


function genAbsActData(config, human)
    local times = 0
    local DailyTaskLogic = require("dailyTask.DailyTaskLogic")
    local isReach = DailyTaskLogic.isReachMaxDailyHuoYue(human)
    if isReach then
        times = times + 1
    end

    isReach = false

    local WeekTaskLogic = require("dailyTask.WeekTaskLogic")
    isReach = WeekTaskLogic.isReachMaxWeekHuoYue(human)
    if isReach then
        times = times + 1
    end

    return { lotteryTimes = times }
end

function isRed(human, YYInfo, funcConfig)
    if not isOpenAct(human, funcConfig and funcConfig.funcID) then return false end
    local d = getData(human)
    if not d then return false end
    return (d.lotteryTimes or 0) > 0
end

function isOpen(human, YYInfo, funcConfig)
    return isOpenAct(human, funcConfig and funcConfig.funcID)
end

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

-- 触发入口：日常/每周任务达100活跃
function OnHuoYueReach100(human)
    if not isOpenAct(human) then return end
    addTimes(human, 1)
    updateRedDot(human)
end


function ActiveWheel_Query(human)
    if not isOpenAct(human) then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local d = getData(human)
    local _, endTime, startTime = isOpenAct(human)  -- 取活动起止时间

    local msgRet = Msg.gc.GC_ANNIV_ACTIVE_WHEEL_QUERY
    msgRet.lotteryTimes = d.lotteryTimes or 0
    msgRet.startTime    = startTime or 0
    msgRet.endTime      = endTime   or 0

    -- 下发完整奖励配置表，客户端据此渲染转盘格子
    msgRet.rewardList[0] = 0
    for i, row in ipairs(RewardCfg) do
        msgRet.rewardList[0] = i
        Grid.makeItem(msgRet.rewardList[i], row.reward[1], row.reward[2])
    end

    Msg.send(msgRet, human.fd)
end

local function doSpin(human, count)
    if not isOpenAct(human) then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local d = getData(human)
    local curTimes = d.lotteryTimes or 0
    if curTimes < count then
        return Broadcast.sendErr(human, Lang.JINBI_EXCHANGE_ERR_CNT)
    end

    local results = doMultiLottery(count)
    if not results then
        return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
    end

    -- 扣除次数
    d.lotteryTimes = curTimes - count

    for _, res in ipairs(results) do
        BagLogic.addItem(human, res.itemId, res.itemNum, LOGTYPE)
    end

    -- 通知客户端抽奖结果（客户端根据此结果播放动画）
    local msgRet = Msg.gc.GC_ANNIV_ACTIVE_WHEEL_RESULT
    msgRet.count      = count
    msgRet.results[0] = 0
    for i, res in ipairs(results) do
        msgRet.results[0] = i
        Grid.makeItem(msgRet.results[i], res.itemId, res.itemNum)
    end
    msgRet.lotteryTimes      = d.lotteryTimes
    Msg.send(msgRet, human.fd)

    updateRedDot(human)
end

function ActiveWheel_Single(human)
    doSpin(human, 1)
end

function ActiveWheel_Ten(human)
    doSpin(human, 10)
end