local AbsActLogic = require("absAct.AbsActLogic")
local AbsActExcel = require("excel.absAct")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local AbsActDefine = require("absAct.AbsActDefine")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")

HERO_LOG_TYPE_1 = 1 --每日登录
HERO_LOG_TYPE_2 = 2 --冠军联赛挑战X次
HERO_LOG_TYPE_3 = 3 --扫荡X次
HERO_LOG_TYPE_4 = 4 --领取X次5星以上悬赏任务
HERO_LOG_TYPE_5 = 5 --任意难度勇者试炼通关X次
HERO_LOG_TYPE_6 = 6 --累计消耗X钻石
HERO_LOG_TYPE_7 = 7 --获得任务中指定的英雄
HERO_LOG_TYPE_8 = 8 --获得任务中指定的英雄升到多少星

function getAndSendMsg(human,id)
    local state,endTime, starTime = AbsActLogic.isStarted(human, id)
	local absActConfig = AbsActExcel.absActivity[id]
    
    -- 活动未开
    if not state then 
        return
    end
    AbsActLogic.checkAbsActClean(human, id)
    local absAct = human.db.absAct[id]

    -- 没有活动数据
    if not absAct then
        return
    end

    local msgRet = Msg.gc.GC_ABS_ACT_HERO_LOG_QUERY
    local config = AbsActExcel.heroLog
    local len = 0
    for k,v in pairs(config) do
        if v.actID == id then
            len = len + 1
            msgRet.taskList[len].id = k
            msgRet.taskList[len].state = 0
            msgRet.taskList[len].maxCnt = v.maxCnt
            msgRet.taskList[len].desc = v.desc
            msgRet.taskList[len].panelID = v.panelID
            if not absAct.task or not absAct.task[k] then 
                msgRet.taskList[len].nowCnt = 0
            else
                msgRet.taskList[len].nowCnt = absAct.task[k].cnt
                msgRet.taskList[len].state = absAct.task[k].state
            end
            local lenItem = #v.item
            for i = 1,lenItem do
                Grid.makeItem(msgRet.taskList[len].item[i],v.item[i][1],v.item[i][2])
            end
            msgRet.taskList[len].item[0] = lenItem
            msgRet.box[len].id = len
            if not absAct.box or not absAct.box[len] then 
                msgRet.box[len].state = 0
            else
                msgRet.box[len].state = absAct.box[len]
            end
            lenItem = #v.boxItem
            for i = 1,lenItem do
                Grid.makeItem(msgRet.box[len].item[i],v.boxItem[i][1],v.boxItem[i][2])
            end
            msgRet.box[len].item[0] = lenItem
        end
    end
    msgRet.taskList[0] = len
    msgRet.box[0] = len

    Msg.send(msgRet,human.fd)
end

function getBoxReward(human,index,id)
    local state,endTime, starTime = AbsActLogic.isStarted(human, id)
	local absActConfig = AbsActExcel.absActivity[id]
    
    -- 活动未开
    if not state then 
        return
    end
    AbsActLogic.checkAbsActClean(human, id)
    
    local absAct = human.db.absAct[id]
    -- 没有活动数据
    if not absAct or not absAct.box then
        return
    end

    local configIndex = 0
    local key = nil
    for i = 1,#AbsActExcel.heroLog do
        if AbsActExcel.heroLog[i].actID == id then
            configIndex = configIndex + 1
            if configIndex == index then
                key = i
                break
            end
        end
    end

    if not key then
        return
    end

    -- 不可领或已领过
    if absAct.box[key] ~= 1 then
        return
    end

    -- 可领
    absAct.box[key] = 2

    BagLogic.addItemList(human, AbsActExcel.heroLog[key].boxItem, "abs_herolog")
    
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, PanelDefine.PANEL_ID_3705)
    getAndSendMsg(human,id)
end

function isComplRuleTask(type, rules, value1, value2)
    if not rules then return true end

    local isCompl
    -- 特殊完成规则
    if type == HERO_LOG_TYPE_7 then  
        for _,heroID in ipairs(rules) do
            if value1 == heroID then
                isCompl = true
                break
            end
        end
    elseif type == HERO_LOG_TYPE_8 then
        for _,rule in ipairs(rules) do
            if value1 == rule[1] and value2 == rule[2] then
                isCompl = true
                break
            end
        end
    else
        isCompl = true
    end

    return isCompl
end

function finishTaskCB(human, type, cnt, value1, value2)
    local state,id,endTime, starTime = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_5)
	local absActConfig = AbsActExcel.absActivity[id]

    -- 活动未开
    if not state then 
        return
    end
    
    AbsActLogic.checkAbsActClean(human, id)
    local absAct = human.db.absAct[id]

    -- 没有活动数据
    if not absAct then
        return
    end

    local config = AbsActExcel.heroLog
    for i = 1,#config do
        local v = config[i]
        if v.actID == absActConfig.actId and v.type == type and isComplRuleTask(type, v.rules, value1, value2) then
            absAct.task = absAct.task or {}
            if not absAct.task[i] or absAct.task[i].cnt < v.maxCnt then
                absAct.task[i] = absAct.task[i] or {}
                absAct.task[i].cnt = absAct.task[i].cnt or 0
                absAct.task[i].cnt = absAct.task[i].cnt + cnt
                absAct.task[i].state = 0
                if absAct.task[i].cnt >= v.maxCnt then
                    absAct.box = absAct.box or {}
                    local len = #absAct.box
                    absAct.box[len + 1] = 1
                    absAct.task[i].state = 1
                end
            end
        end
    end
end

function getTaskReward(human,index,id)
    local state,endTime, starTime = AbsActLogic.isStarted(human, id)
	local absActConfig = AbsActExcel.absActivity[id]
    
    -- 活动未开
    if not state then 
        return
    end
    AbsActLogic.checkAbsActClean(human, id)
    
    local absAct = human.db.absAct[id]
    -- 没有活动数据
    if not absAct or not absAct.box then
        return
    end

    -- 不可领或已领过
    if not absAct.task or not absAct.task[index] or absAct.task[index].state ~= 1 then
        return
    end

    absAct.task[index].state = 2
    BagLogic.addItemList(human, AbsActExcel.heroLog[index].item, "abs_herolog")
    getAndSendMsg(human,id)

    YunYingLogic.sendGroupUpdate(YYInfo[id], human, PanelDefine.PANEL_ID_3705)
end

function isRed(human, YYInfo, funcConfig)
    local state, endTime, starTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    if not state then return end

    -- 没有活动数据
    local absAct = human.db.absAct[id]
    if not absAct or (not absAct.box and not absAct.task) then
        return
    end

    local len = #absAct.box
    for i = 1,len do
        if absAct.box and absAct.box[i] == 1 then
            return true
        end
    end

    len = #absAct.task
    for i = 1 ,len do
        if absAct.task[i] and absAct.task[i].state == 1 then
            return true
        end
    end

    return false
end

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

function isOpen(human, YYInfo, funcConfig)
    return AbsActLogic.isStarted(human, funcConfig.funcID)
end

function getLeftTime(human)
    local ret, id, endTime, startTime = AbsActLogic.isStartedByType(human, AbsActDefine.ABS_ACT_TYPE_5)
    if ret == true then
        return endTime - os.time()
    else
        return 0
    end
end