--[[
    absAct[id] = {
        use = 1000
        count = 0
        reward[id] = {
            state = 1
        }
    }

]]

local AbsActLogic = require("absAct.AbsActLogic")
local AbsActExcel = require("excel.absAct")
local BuyExcel = require("excel.buy")
local Util = require("common.Util")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local BuyLogic = require("topup.BuyLogic")
local ObjHuman = require("core.ObjHuman")
local Lang  = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local YunYingLogic = require("yunying.YunYingLogic")

function isOpen(human, YYInfo, funcConfig)
    local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    if not state then return end
    local absAct = human.db.absAct[funcConfig.funcID]
    if absAct and absAct.dayCnt and absAct.dayCnt <= 0 and #absAct.item <= 0 then
        return false
    end
    return true, endTime, startTime
end

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

local function fontUseDiamondNet(net,absAct,id,v)
    net.id = id
    net.state = 1
    if absAct.reward[id] then
        net.state = absAct.reward[id]
    end
    net.needUse = v.needUse
    net.desc = v.desc
    net.nowUse = 0
    if absAct.use then
        net.nowUse = absAct.use
    end
    if v.special == 1 then
        net.nowUse = absAct.count or 0
    end
    local rewardLen = #v.item
    for j = 1,rewardLen do
        local itemID = v.item[j][1]
        local itemCnt = v.item[j][2]
        Grid.makeItem(net.item[j],itemID,itemCnt)
    end
    net.item[0] = rewardLen
end

function getAndSendMsg(human,id)
    -- 判断活动是否开启
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then return end

    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then return end

    local absAct = human.db.absAct[id]
    if not absAct then
        return
    end

    absAct.reward = absAct.reward or {}

    local config = AbsActExcel.useDiamond
    local msgRet = Msg.gc.GC_ABS_HF_USE_DIAMOND_QUERY
    local len = #config
    local count = 0
    local cnt = 0
    for i = 1,len do
        if config[i].special == 1 then
            cnt = cnt + 1
            fontUseDiamondNet(msgRet.backReward[cnt],absAct,i,config[i])
        else
            count = count + 1
            fontUseDiamondNet(msgRet.useRewardList[count],absAct,i,config[i])
        end
    end
    msgRet.backReward[0] = cnt
    msgRet.useRewardList[0] = count 
    Msg.send(msgRet,human.fd)
end


function onDecZuanshi(human,id,value)
    useDiamondBack(human,value,id)
end
function useDiamondBack(human,value,id)
    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then return end

    AbsActLogic.checkAbsActClean(human, id)
    local absAct = human.db.absAct[id]
    if not absAct then
        return
    end

    --增加钻石使用数量 
    absAct.count = absAct.count or 0
    absAct.use = absAct.use or 0
    absAct.reward = absAct.reward or {}
    absAct.use = absAct.use + value

    local config = AbsActExcel.useDiamond
    local len = #config
    for i = 1,len do
        if config[i].special ~= 1 then
            -- 判断钻石使用数量是否达标了
            if not absAct.reward[i] and absAct.use >= config[i].needUse then
                absAct.reward[i] = 0
                -- 达标，则增加任务完成数量
                absAct.count = absAct.count + 10
            end
        else
            -- 判断任务完成数量是否达标
            if not absAct.reward[i] and absAct.count >= config[i].needUse then
                absAct.reward[i] = 0
            end
        end
    end
    
    YunYingLogic.sendBanner(human)
    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, absConfig.panelID)
end

function getUseDiamondReward(human,id,giftId)
     -- 判断活动是否开启
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then return end

    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then return end

    local absAct = human.db.absAct[id]
    if not absAct then
        return
    end

    -- 状态不为可领，返回
    absAct.reward = absAct.reward or {}
    if absAct.reward[giftId] ~= 0 then
        return
    end

    -- 设置状态为已领取
    absAct.reward[giftId] = 2

    -- 发奖励
    local config = AbsActExcel.useDiamond[giftId]
    BagLogic.addItemList(human, config.item, "abs_useDiamond")   
    getAndSendMsg(human,id)

    YunYingLogic.sendBanner(human)
    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, absConfig.panelID)
end


function isRed(human, YYInfo, funcConfig)
    local state,endTime,starTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    if not state then return end
    local absConfig = AbsActExcel.absActivity[funcConfig.funcID]
    AbsActLogic.checkAbsActClean(human, funcConfig.funcID)
    local absAct = human.db.absAct[funcConfig.funcID]
    if not absAct then return end
    
    absAct.reward = absAct.reward or {}

    for k,v in pairs(absAct.reward) do
        if v == 0 then
            return true
        end
    end

    return false
end
