--[[
    absAct[id] = {
        drum = {
            [id] = cnt,state
            [id] = cnt,state
        }
        blasting = {
            [id] = cnt,state
        }
        box = {
            [id] = state
        }
        blastingCnt = 0
        buling = 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 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


-- 初始化数据库
function initDB(human,id)
    local absAct = human.db.absAct[id]
    absAct.drum = absAct.drum or {} 
    absAct.blasting = absAct.blasting or {} 
    absAct.box = absAct.box or {} 
    absAct.blastingCnt = absAct.blastingCnt or 0 
    absAct.buling = absAct.buling or 0 
end

-- 发送数据
function getAndSendMsg(human,id,actID)
    -- 判断活动是否开启
    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
    initDB(human,id)
    
    local nowDay = Util.diffDay(starTime) + 1
    -- 构造数据
    local msgRet = Msg.gc.GC_ABS_ND_TASK_QUERY
    -- 鼓数据
    local taskConfig = AbsActExcel.drum
    local len = #taskConfig
    local count = 0
    msgRet.day = nowDay
    BuyLogic.fontBuyItem(human, msgRet.buyItem, AbsActExcel.blastingBox[1].buyID)
    for i = 1,len do
        local v = taskConfig[i]
        if v.day == nowDay then
            count = count + 1
            local net = msgRet.drumList[count]
            net.id = i
            net.desc = v.desc
            net.name = v.name
            net.needCnt = v.needCnt
            net.panelID = v.panelID
            net.nowCnt = absAct.drum[i] and absAct.drum[i].cnt or 0
            net.state = absAct.drum[i] and absAct.drum[i].state or 0
            for j = 1,#v.item do
                Grid.makeItem(net.item[j],v.item[j][1],v.item[j][2])
            end
            net.item[0] = #v.item
        end
    end
    msgRet.drumList[0] = count
    -- 炮数据
    taskConfig = AbsActExcel.blasting
    len = #taskConfig
    for i = 1,len do
        local v = taskConfig[i]
        local net = msgRet.blastingList[i]
        net.id = i 
        net.desc = v.desc
        net.name = v.name
        net.needCnt = v.needCnt
        net.nowCnt = absAct.blasting[i] and absAct.blasting[i].cnt or 0
        net.state = absAct.blasting[i] and absAct.blasting[i].state or 0
        for j = 1,#v.item[nowDay] do
            Grid.makeItem(net.item[j],v.item[nowDay][j][1],v.item[nowDay][j][2])
        end
        net.item[0] = #v.item[nowDay]
        net.panelID = v.panelID or 0
    end
    msgRet.blastingList[0] = len
    -- 宝箱数据
    taskConfig = AbsActExcel.blastingBox
    len = #taskConfig
    for i = 1,len do
        local v = taskConfig[i]
        local net = msgRet.boxList[i]
        net.id = i
        net.needCnt = v.needCnt
        net.state = absAct.box[i] or 0
        for j = 1,#v.item do
            Grid.makeItem(net.item[j],v.item[j][1],v.item[j][2])
        end
        net.item[0] = #v.item
    end
    msgRet.boxList[0] = len
    msgRet.blastingCnt = absAct.blastingCnt
    Msg.send(msgRet,human.fd)
end

-- 领取物品
-- type 1鼓,2炮,3箱
function get(human,index,type,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
    initDB(human,id)

    local nowDay = Util.diffDay(starTime) + 1
    local item = nil

    -- 选择对应类型
    local config = nil
    local state = nil
    if type == 1 then
        config = AbsActExcel.drum[index]
        state = absAct.drum[index] and absAct.drum[index].state or 0
    elseif type == 2 then
        config = AbsActExcel.blasting[index]
        state = absAct.blasting[index] and absAct.blasting[index].state or 0
    elseif type == 3 then
        config = AbsActExcel.blastingBox[index]
        state = absAct.box[index] or 0
    end

    -- 传入参数错误
    if not config then
        return 
    end

    -- 状态不为可领取
    if state ~= 1 then
        return
    end

    -- 改变状态
    if type == 1 then
        item = config.item
        absAct.drum[index].state = 2
    elseif type == 2 then
        item = config.item[nowDay]
        absAct.blasting[index].state = 2
        -- 放炮一次,增加累计次数，并判断是否达到宝箱领取数值
        absAct.blastingCnt = absAct.blastingCnt + 1
        for i = 1,#AbsActExcel.blastingBox do
            if absAct.blastingCnt - 1 < AbsActExcel.blastingBox[i].needCnt and absAct.blastingCnt == AbsActExcel.blastingBox[i].needCnt then
                absAct.box[i] = 1
                break
            end
        end
    elseif type == 3 then
        item = config.item
        absAct.box[index] = 2
    else
        return
    end

    BagLogic.addItemList(human, item, "abs_drumblasting")
    getAndSendMsg(human,id,actID)
    
    YunYingLogic.sendBanner(human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, absConfig.panelID)
end

-- 每日任务活跃度
function onDailyTask(human,funcID, parameter)
    drumTaskCallBack(human,funcID,1,parameter)
end

-- 召唤
function onDrawCard(human,funcID, parameter)
    drumTaskCallBack(human,funcID,2,parameter)
end

-- 恶魔之塔
function onHuangjingTower(human,funcID, parameter)
    drumTaskCallBack(human,funcID,3,parameter)
end

-- 充值
function onCharge(human, parameter,funcID)
    drumTaskCallBack(human,funcID,4,parameter)
end

-- 勇者试炼
function onDrill(human,funcID, parameter)
    drumTaskCallBack(human,funcID,5,parameter)
end

-- 女巫森林
function onCopyChallenge(human,funcID, parameter)
    drumTaskCallBack(human,funcID,6,parameter)
end

-- 冠军联赛
function onJjcFight(human,funcID, parameter)
    drumTaskCallBack(human,funcID,7,parameter)
end

-- 随机商店购买
function onShopBuy(human,funcID,parameter)
    drumTaskCallBack(human,funcID,8,parameter)
end

-- 冒险扫荡
function onMopup(human,funcID,parameter)
    drumTaskCallBack(human,funcID,9,parameter)
end

-- 冰龙巢穴
function onBinglong(human,funcID,parameter)
    drumTaskCallBack(human,funcID,10,parameter)
end

-- 点金
function onExchangeJinbi(human,funcID,parameter)
    drumTaskCallBack(human,funcID,11,parameter)
end

-- 悬赏
function onBarTaskgetDo(human,funcID,parameter)
    drumTaskCallBack(human,funcID,12,parameter)
end

-- 捐赠
function onUnionDonate(human,funcID,parameter)
    drumTaskCallBack(human,funcID,13,parameter)
end

-- 公会副本
function onUnionEctypeFight(human,funcID,parameter)
    drumTaskCallBack(human,funcID,14,parameter)
end

-- 占卜
function onZhanBuBuyCard(human,funcID,parameter)
    drumTaskCallBack(human,funcID,15,parameter)
end

-- 消耗钻石
function onDecZuanshi(human,funcID,parameter)
    drumTaskCallBack(human,funcID,16,parameter)
end

-- 绝望深渊
function onLianyu(human,funcID,parameter)
    drumTaskCallBack(human,funcID,17,parameter)
end

-- 合成符文
function onFuwenHecheng(human,funcID,parameter)
    drumTaskCallBack(human,funcID,18,parameter)
end



-- 回调
function drumTaskCallBack(human,id,type,cnt)
    AbsActLogic.checkAbsActClean(human, id)
    initDB(human,id)

    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    
    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then return end

    local nowDay = Util.diffDay(starTime) + 1
    -- 完成任务，增加任务次数
    local taskConfig = AbsActExcel.drum
    for k = 1,#taskConfig do
        if taskConfig[k].type == type and taskConfig[k].day == nowDay then
            local taskID = k
            human.db.absAct[id].drum[taskID] = human.db.absAct[id].drum[taskID] or {}
            human.db.absAct[id].drum[taskID].cnt = human.db.absAct[id].drum[taskID].cnt or 0
            human.db.absAct[id].drum[taskID].state = human.db.absAct[id].drum[taskID].state or 0
            human.db.absAct[id].drum[taskID].cnt = human.db.absAct[id].drum[taskID].cnt + cnt
            if human.db.absAct[id].drum[taskID].state ~= 0 and cnt < 0 then
                human.db.absAct[id].drum[taskID].cnt = human.db.absAct[id].drum[taskID].cnt - cnt
            end
            local config = AbsActExcel.drum[taskID]
            -- 鼓任务达成
            if human.db.absAct[id].drum[taskID].cnt >= config.needCnt and human.db.absAct[id].drum[taskID].state == 0 then
                human.db.absAct[id].drum[taskID].state = 1
                -- 判断是否达成炮任务
                for i = 1,#config.blasting do
                    local blastingID = config.blasting[i]
                    local blastingConfig = AbsActExcel.blasting[blastingID]
                    human.db.absAct[id].blasting[blastingID] = human.db.absAct[id].blasting[blastingID] or {}
                    human.db.absAct[id].blasting[blastingID].cnt = human.db.absAct[id].blasting[blastingID].cnt or 0
                    human.db.absAct[id].blasting[blastingID].state = human.db.absAct[id].blasting[blastingID].state or 0
                    human.db.absAct[id].blasting[blastingID].cnt = human.db.absAct[id].blasting[blastingID].cnt + 1
                    if human.db.absAct[id].blasting[blastingID].cnt >= 3 and human.db.absAct[id].blasting[blastingID].state ~= 2 then
                        human.db.absAct[id].blasting[blastingID].state = 1
                    end
                end
            end
        end
    end
    
    YunYingLogic.sendBanner(human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, absConfig.panelID)
end

-- 每日重置鼓，炮状态
function updateDaily(human,id)
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then return end

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

    AbsActLogic.checkAbsActClean(human, id)

    local absAct = human.db.absAct[id]
    absAct.drum = {} 
    absAct.blasting = {} 
    absAct.box = absAct.box or {} 
    absAct.blastingCnt = absAct.blastingCnt or 0 
end

function isRed(human, YYInfo, funcConfig)
    local state = 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 true end

    initDB(human,funcConfig.funcID)

    for k,v in pairs(absAct.drum) do
        if v.state == 1 then
            return true
        end
    end

    for k,v in pairs(absAct.blasting) do
        if v.state == 1 then
            return true
        end
    end

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

    return false
end

-- 购买补领
function buling(human,funcID,conf)
    -- 将达标次数设为30次
    AbsActLogic.checkAbsActClean(human, funcID)
    initDB(human,funcID)
    local absConfig = AbsActExcel.absActivity[funcID]
    -- 
    local absAct = human.db.absAct[funcID]
    
    if absAct.buling == 1 then
        return
    end
    absAct.buling = 1

    local config = AbsActExcel.blastingBox
    local len = #config
    for i = 1,len do
        if config[i].needCnt > absAct.blastingCnt then
            absAct.blastingCnt = config[i].needCnt
            absAct.box[i] = 1
        end
    end
    getAndSendMsg(human,id,absConfig.actID)
end