--[[
    absAct[id] = {
        reward[id] = {
            state = 1
            cnt = 0
            getCnt = 0
        }
    }

]]


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 fontDabiaoNet(db,net,config,id)
    net.id = id
    net.limitCnt = config.maxCnt
    net.state = 0
    net.nowCnt = 0
    if db.reward and db.reward[id] then
        net.state = db.reward[id].state or 0
        net.nowCnt = db.reward[id].cnt or 0
    end
    net.desc = config.desc
    net.buyID = config.buyID

    local len = #config.item
    for i = 1,len do
        Grid.makeItem(net.item[i],config.item[i][1],config.item[i][2])
    end
    net.item[0] = len
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

    local config = AbsActExcel.singleReach
    local msgRet = Msg.gc.GC_ABS_XS_SINGLE_REACH_QUERY
    local len = #config
    local count = 0
    for i = 1,len do
        local v = config[i]
        if v.mainId == id and v.actId == absConfig.actId then
            count = count + 1
            local net = msgRet.dabiaoList[count]
            fontDabiaoNet(absAct,net,v,i)
        end
    end
    msgRet.dabiaoList[0] = count
    Msg.send(msgRet,human.fd)
end

function touchSingleReach(human,id,param)
    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.reward = absAct.reward or {}

    local config = AbsActExcel.singleReach
    local len = #config
    for i = 1,len do
        if config[i].actId == absConfig.actId and config[i].mainId == id then
             -- 判断钻石使用数量是否达标了
            absAct.reward[i] = absAct.reward[i] or {}
            absAct.reward[i].state = absAct.reward[i].state or 0
            absAct.reward[i].cnt = absAct.reward[i].cnt or 0
            absAct.reward[i].getCnt = absAct.reward[i].getCnt or 0
            if absAct.reward[i].state ~= 2 and param == config[i].buyID and (absAct.reward[i].cnt + absAct.reward[i].getCnt < config[i].maxCnt) then
                absAct.reward[i].state = 1
                absAct.reward[i].getCnt = absAct.reward[i].getCnt + 1
            end
        end
    end
    
    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, absConfig.panelID)
end


function getSingleReachReward(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 not absAct.reward[giftId] or absAct.reward[giftId].state ~= 1 or absAct.reward[giftId].getCnt <= 0 then
        return
    end
    
    local config = AbsActExcel.singleReach[giftId]

    if config.mainId ~= id or config.actId ~= absConfig.actId then
        return
    end

    -- 设置状态为已领取
    absAct.reward[giftId].state = 2
    absAct.reward[giftId].cnt = absAct.reward[giftId].cnt + 1
    absAct.reward[giftId].getCnt = absAct.reward[giftId].getCnt - 1

    if absAct.reward[giftId].getCnt > 0 and absAct.reward[giftId].cnt < config.maxCnt then
        absAct.reward[giftId].state = 1
    elseif absAct.reward[giftId].cnt < config.maxCnt then
        absAct.reward[giftId].state = 0
    end
    -- 发奖励
    BagLogic.addItemList(human, config.item, "abs_singleReach")   
    getAndSendMsg(human,id)
    
    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]
    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.state == 1 then
            return true
        end
    end

    return false
end

function updateDaily(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 = nil
end