local Msg = require("core.Msg")
local Util = require("common.Util")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local PanelDefine = require("broadcast.PanelDefine")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local DrawCardLogic = require("drawCard.DrawCardLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local AbsActLogic = require("absAct.AbsActLogic")
local AbsActExcel = require("excel.absAct")
--[[
	开服活动-累计消耗类活动 累计消耗钻、累计招募
		human.db.absAct[funcID] = {   
			value = xxx,		-- 累计消耗值
			[id] = xxx,			-- 档位领取状态(0不可领,1可领,2已领)
			... 
        },
	local:
        reachTouchHanlde()          -- 消耗达标处理
		wrapTConsumeList()			-- 包装档位数据
	public:
		isRed()                 	-- 红点提醒	
        isActive()                  -- 激活状态
		isOpen()                	-- 活动开启	
        getLeftTime()               -- 得到活动剩余时间 
		getAndSendMsg()			    -- 发送活动信息
		get()						-- 领取活动奖励
		onDecZuanshi()				-- 消耗钻石回调
        onDrawCard()                -- 抽卡回调
        onZhandouli()               -- 战力回调
--]]

STATE_0 = 0	                        -- 不可领
STATE_1 = 1                         -- 可领
STATE_2 = 2                         -- 已领完

local function reachTouchHanlde(human, funcID, value, isReplace)
    local funcConfig = YunYingLogic.getFuncConfig(funcID)
    if not funcConfig then return end

    AbsActLogic.checkAbsActClean(human, funcID)

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

    if isReplace then
        absActDB.value = value
    else
        local tValue = (absActDB.value or 0) + value
        absActDB.value = tValue
    end
    
    -- 取之前是不是有红点
    local beforeRed = isRed(human, nil, funcConfig)
    local absActConfig = AbsActExcel[funcConfig.param[2]] 

    local isActive
    for id,info in pairs(absActConfig) do
        if info.actId == funcConfig.actId and 
            info.needConsume <= absActDB.value and 
            absActDB[id] ~= STATE_2 then
            absActDB[id] = STATE_1
            isActive = true
        end
    end
    
    -- 之前没有红点,现在激活了红点通知(只需要通知banner上的红点)
    if not beforeRed and isActive then
        if funcConfig.param[2] == "zhanliReach" then
            YunYingLogic.updateIcon(YYInfo[funcID], human)
	        YunYingLogic.sendGroupUpdate(YYInfo[funcID], human, funcConfig.panelID)
        else
            YunYingLogic.sendBanner(human)
        end
    end
end

local function wrapTConsumeList(net, id, tConsumeInfo, absActDB)
	net.id = id
	net.needConsume = tConsumeInfo.needConsume
 	net.state = absActDB[id] or STATE_0

 	local len = 0
	for index,itemInfo in ipairs(tConsumeInfo.rewards) do
		len = len + 1
		Grid.makeItem(net.items[index], itemInfo[1], itemInfo[2])
	end
	net.items[0] = len
end

function isRed(human, YYInfo, funcConfig)
    AbsActLogic.checkAbsActClean(human, funcConfig.funcID)

	local absActDB = human.db.absAct[funcConfig.funcID]
	if not absActDB then return end

    local absActConfig = AbsActExcel[funcConfig.param[2]]
	for id,info in pairs(absActConfig) do
        if info.actId == funcConfig.actId and 
	        absActDB[id] and absActDB[id] == STATE_1 then return true end
	end
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, YInfo, funcConfig)
    local ret, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    if ret == true then
        return endTime - os.time()
    else
        return 0
    end
end

function getAndSendMsg(human, funcID, actId)
    local state,endTime,starTime = AbsActLogic.isStarted(human, funcID)
    if not state then return end

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

    local funcConfig = YunYingLogic.getFuncConfig(funcID)
    if not funcConfig then return end

    local leftTime = getLeftTime(human, nil, funcConfig)
    local absActConfig = AbsActExcel[funcConfig.param[2]] 

    local msgRet = Msg.gc.GC_ABS_OPEN_SERVER_REACH_QUERY
    msgRet.funcID = funcID
    msgRet.type = funcConfig.param[1]
	msgRet.value = absActDB.value or 0
    msgRet.leftTime = leftTime

	local len = 0
	for id,info in pairs(absActConfig) do
        if actId == info.actId then
		    len = len + 1
		    wrapTConsumeList(msgRet.list[len], id, info, absActDB)
        end
	end
	msgRet.list[0] = len
   
	Msg.send(msgRet, human.fd)
end

function get(human, funcID, id)
    local state,endTime, starTime = AbsActLogic.isStarted(human, funcID)
    if not state then return end

    local funcConfig = YunYingLogic.getFuncConfig(funcID)
    if not funcConfig then return end

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

	if absActDB[id] ~= STATE_1 then return end

    local absActConfig = AbsActExcel[funcConfig.param[2]]
    local tConsumeInfo = absActConfig[id]

	absActDB[id] = STATE_2
	BagLogic.addItemList(human, tConsumeInfo.rewards, "openServerTConsume_get")

	getAndSendMsg(human, funcID, funcConfig.actId)

    -- 现在没有红点了通知
    if not isRed(human, nil, funcConfig) then
        YunYingLogic.sendBanner(human)
        YunYingLogic.updateIcon(YYInfo[funcID], human)
        YunYingLogic.sendGroupUpdate(YYInfo[funcID], human, funcConfig.panelID)
    end
end

function onDecZuanshi(human, funcID, value)
    local funcConfig = YunYingLogic.getFuncConfig(funcID)
    if not funcConfig then return end

    if not isOpen(human, nil, funcConfig) then return end

    if funcConfig.param[2] == "openTotalConsume" then
        reachTouchHanlde(human, funcID, value)
    end
end

function onDrawCard(human, funcID, value, drawType)
    local funcConfig = YunYingLogic.getFuncConfig(funcID)
    if not funcConfig then return end

    if not isOpen(human, nil, funcConfig) then return end

    if funcConfig.param[2] == "openDrawCardConsume" then
        if drawType == DrawCardLogic.DRAWCARD_ID2 or 
            drawType == DrawCardLogic.DRAWCARD_ID5 or 
            drawType == DrawCardLogic.DRAWCARD_ID6 then
            reachTouchHanlde(human, funcID, value)
        end
    end
end

function onZhandouli(human, funcID, value)
    local funcConfig = YunYingLogic.getFuncConfig(funcID)
    if not funcConfig then return end

    if not isOpen(human, nil, funcConfig) then return end
     
    if funcConfig.param[2] == "zhanliReach" then
        reachTouchHanlde(human, funcID, value, true)
    end
end