--古玉充能活动
--db
--[=[
    human.db.absAct[id] = {
        points = nil, --普通抽奖累积的积分
        speLotteryTimes = nil, --稀有抽奖次数
        lastFreeTime = nil, --上一次免费抽奖时间,如果为nil或者与当前时间不是同一天则可以免费单抽一次
    }
]=]--

local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local Util = require("common.Util")
local BagLogic = require("bag.BagLogic")
local AbsActLogic = require("absAct.AbsActLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local LotteryCfg = require("excel.absAct").lotteryByDiamonds
local TriggerDefine = require("trigger.TriggerDefine")
local TriggerLogic = require("trigger.TriggerLogic")

local LOTTERYLOG = "lotteryByDiamonds" --日志标识
local MAXPOINTS = 300               --每三百积分可以获得一次稀有抽奖次数
local RANDMIN = 5                   --普通奖池每次抽奖获得积分随机值最小值
local RANDMAX = 15                  --普通奖池每次抽奖获得积分随机值最大值
local LOTTERYACTID = 31005          -- 古玉充能活动ID

local lotteryCostID = 0             --普通奖池抽奖消耗道具ID
local lottery1CostCnt = 0           --普通奖池单抽消耗道具数量
local lottery10CostCnt = 0          --普通奖池十连消耗道具数量
local comTotalWeight = 0            --普通奖池道具总权重
local speTotalWeight = 0            --稀有奖池道具总权重


local function updateCondValue()
    --没热更,所以缓存
    local comCfg = LotteryCfg[1]
    local speCfg = LotteryCfg[2]
    lotteryCostID = comCfg.costItemId
    lottery1CostCnt = comCfg.lottery1CostCnt
    lottery10CostCnt = comCfg.lottery10CostCnt

    for _, v in ipairs(comCfg.prizeData) do
        comTotalWeight = comTotalWeight + v[3]
    end

    for _, v in ipairs(speCfg.prizeData) do
        speTotalWeight = speTotalWeight + v[3]
    end
end


--查询
function Query(human, id)
    local state = AbsActLogic.isStarted(human, id)
    if not state then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local actData = human.db.absAct[id]
    if not actData then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local msgRet = Msg.gc.GC_LOTTERYBYDIAMONDS_QUERY

    if comTotalWeight <= 0 then
        updateCondValue()
    end

    --普通奖池
    local nLotteryCostCnt = lottery1CostCnt
    if not actData.lastFreeTime or not Util.isSameDayByTimes(actData.lastFreeTime, os.time()) then
        nLotteryCostCnt = 0
    end

    local comLotteryData = msgRet.comLotteryData
    Grid.makeItem(comLotteryData.lottery1cost, lotteryCostID, nLotteryCostCnt)
    Grid.makeItem(comLotteryData.lottery10cost, lotteryCostID, lottery10CostCnt)

    comLotteryData.points = actData.points or 0
    comLotteryData.maxPoints = MAXPOINTS

    local comPrizeCfg = LotteryCfg[1].prizeData
    comLotteryData.prizePoolData[0] = #comPrizeCfg
    for i = 1, #comPrizeCfg do
        local itemData = comPrizeCfg[i]
        Grid.makeItem(comLotteryData.prizePoolData[i], itemData[1], itemData[2])
    end

    --稀有奖池
    local speLotteryData = msgRet.speLotteryData
    speLotteryData.lotteryTimes = actData.speLotteryTimes or 0
    local spePrizeCfg = LotteryCfg[2].prizeData
    speLotteryData.prizePoolData[0] = #spePrizeCfg
    for i = 1, #spePrizeCfg do
        local itemData = spePrizeCfg[i]
        Grid.makeItem(speLotteryData.prizePoolData[i], itemData[1], itemData[2])
    end

    Msg.send(msgRet, human.fd)
end

--抽奖
function Lottery(human, id, type)
    if not id or not type then
        return Broadcast.sendErr(human, Lang.SKIN_PARAM_ERR)
    end

    if type < 1 or type > 3 then
        return Broadcast.sendErr(human, Lang.SKIN_PARAM_ERR)
    end

    local state = AbsActLogic.isStarted(human, id)
    if not state then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local actData = human.db.absAct[id]
    if not actData then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    if comTotalWeight <= 0 then
        updateCondValue()
    end

    local itemCnt, nLotteryTimes = 0, 1
    local targetTotalWeight = comTotalWeight
    local targetPrizeCfg = LotteryCfg[1].prizeData
    if type == 1 then
        if not actData.lastFreeTime or not Util.isSameDayByTimes(actData.lastFreeTime, os.time()) then
            actData.lastFreeTime = os.time()
        else
            itemCnt = lottery1CostCnt
        end
    elseif type == 2 then
        itemCnt = lottery10CostCnt
        nLotteryTimes = 10
    else
        if not actData.speLotteryTimes or actData.speLotteryTimes <= 0 then
            return
        end
        actData.speLotteryTimes = actData.speLotteryTimes - 1
        targetPrizeCfg = LotteryCfg[2].prizeData
        targetTotalWeight = speTotalWeight
    end

    if itemCnt > 0 then
        if BagLogic.getItemCnt(human, lotteryCostID) < itemCnt then
            return Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
        end
        BagLogic.delItem(human, lotteryCostID, itemCnt, LOTTERYLOG)
    end

    --抽奖
    local awardList = {}
    for i=1, nLotteryTimes do
        local weight = 0
        local randWeight = math.random(0, targetTotalWeight)
        for _,v in ipairs(targetPrizeCfg) do
            weight = weight + v[3]
            if randWeight <= weight then
                awardList[#awardList+1] = {v[1], v[2]}
                if type < 3 then
                    actData.points = (actData.points or 0) + math.random(RANDMIN, RANDMAX)
                end
                break
            end
        end
    end
    BagLogic.addItemList(human, awardList, LOTTERYLOG)

    --更新数据
    local points = actData.points or 0
    if points >= MAXPOINTS then
        actData.speLotteryTimes = (actData.speLotteryTimes or 0) + math.floor(points / MAXPOINTS)
        actData.points = points % MAXPOINTS
    end

    --下发数据
    local msgRet = Msg.gc.GC_LOTTERYBYDIAMONDS_LOTTERY
    msgRet.points = actData.points
    msgRet.lotteryTimes = actData.speLotteryTimes or 0

    local nLotteryCostCnt = lottery1CostCnt
    if not actData.lastFreeTime or not Util.isSameDayByTimes(actData.lastFreeTime, os.time()) then
        nLotteryCostCnt = 0
    end
    Grid.makeItem(msgRet.lottery1cost, lotteryCostID, nLotteryCostCnt)
    Msg.send(msgRet, human.fd)

    if type == 3 then
        TriggerLogic.PublishEvent(TriggerDefine.ANCIENTG_CHARGING, human.db._id, 1)
    end
end

-- 是否有红点
function isRed(human, YYInfo, funcConfig)
    local tActData = human.db.absAct[LOTTERYACTID]
    if not tActData then
        return false
    end

    if not tActData.lastFreeTime or not Util.isSameDayByTimes(tActData.lastFreeTime, os.time()) then
        return true
    end

    return false
end