
---  打赏返利  


local Util = require("common.Util")
local ObjHuman = require("core.ObjHuman")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local ItemDefine = require("bag.ItemDefine")
local BuyLogic = require("topup.BuyLogic")
local BagLogic = require("bag.BagLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")
local YyHandler = require("yunying.Handler")
local KingWorldLogic = require("present.KingWorldLogic")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local AbsActLogic = require("absAct.AbsActLogic")
local AbsActExcel = require("excel.absAct")
local AbsActDefine = require("absAct.AbsActDefine")
local YunYingExcel = require("excel.yunying")

function sortomp(a, b)
    local r1 = AbsActExcel.rechargeBack[a].need
    local r2 = AbsActExcel.rechargeBack[b].need
    return r1 < r2
end

function query(human, id)
    local state,endTime, starTime = AbsActLogic.isStarted(human, id)
--    local state,id = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_10)
    if not state then  return end

    AbsActLogic.checkAbsActClean(human, id)
    local absAct = human.db.absAct[id]
    -- 没有活动数据
    if not absAct then
        return
    end
    local absConfig = AbsActExcel.absActivity[id]
    local msgRet = Msg.gc.GC_ABS_RECHARGE_BACK_QUERY

    msgRet.recharge = absAct.topupAcount or 0
    msgRet.maxCnt = 0
    msgRet.isEnd = 0
    msgRet.startTime = starTime
    msgRet.endTime = endTime
    msgRet.panelID = absConfig.panelID 

    if absConfig.panelID == PanelDefine.PANEL_ID_3106 then
        msgRet.startTime = 0
        msgRet.endTime = 0
    end
    local maxCnt = 0
    local len = 0
    local getDb = absAct.rechargeBackGet or {}

    local list = {}
    for k, config in pairs(AbsActExcel.rechargeBack) do
        if config and config.actID == absConfig.actId then
            maxCnt = maxCnt + 1
            list[#list + 1] = k
        end
    end

    table.sort( list , sortomp )

    for k, h in ipairs(list) do
        local config = AbsActExcel.rechargeBack[h]
        if k > 3 and msgRet.recharge <  AbsActExcel.rechargeBack[list[k - 1]].need then
        else
            len = len + 1
            local net = msgRet.list[len]
            net.id = h
            net.need = config.need
            net.state = getDb[h] and 1 or 0
            for j, h in ipairs(config.items) do
                Grid.makeItem(net.reward[j], h[1], h[2])
            end
            net.reward[0] = #config.items
            if len >= 10 then
                msgRet.isEnd = 0
                msgRet.list[0] = len
                Msg.send(msgRet, human.fd)
                len = 0
            end
        end

    end  
    msgRet.maxCnt = maxCnt
    msgRet.isEnd = 1
    msgRet.list[0] = len

   -- Msg.tarce(msgRet)
    Msg.send(msgRet, human.fd)
end


function get(human, panelID, id)
    local absID 
    if panelID == PanelDefine.PANEL_ID_3106 then
        absID = 11001 
    elseif panelID == PanelDefine.PANEL_ID_3309 then
        absID = 7001
    end
    local state,endTime, starTime = AbsActLogic.isStarted(human, absID)
    if not state then
        return
    end
    --[[
  local state,absID = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_10)
    if not state then
        return
    end
    ]]
  
    AbsActLogic.checkAbsActClean(human, absID)
    local absConfig = AbsActExcel.absActivity[absID]
    if absConfig.type ~= AbsActDefine.ABS_ACT_TYPE_10 then
        return
    end

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

    if config.actID ~= absConfig.actId then
        return
    end

    local absAct = human.db.absAct[absID]
    -- 没有活动数据
    if not absAct then
        return
    end

    local rechargeNum = absAct.topupAcount or 0
    if config.need > rechargeNum then
        return 
    end

    if absAct.rechargeBackGet and absAct.rechargeBackGet[id] then 
        return 
    end
    
    absAct.rechargeBackGet =  absAct.rechargeBackGet or {}
    absAct.rechargeBackGet[id] = 1

    local logType = "abs_rechargeBack"
    if absConfig.panelID == PanelDefine.PANEL_ID_3106 then
        logType = "rechargeBack"
    end
    BagLogic.addItemList(human, config.items,  logType)
    YunYingLogic.updateIcon(YYInfo[absID], human)
    YunYingLogic.sendGroupUpdate(YYInfo[absID], human, panelID)

    query(human, absID)

end

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

    local absConfig = AbsActExcel.absActivity[funcID]
    if absConfig.type == AbsActDefine.ABS_ACT_TYPE_10 then
        AbsActLogic.checkAbsActClean(human, funcID)
        local absAct = human.db.absAct[funcID]
        absAct.topupAcount = absAct.topupAcount or 0 
        absAct.topupAcount = absAct.topupAcount + price
        YunYingLogic.updateIcon(YYInfo[funcID], human)
        YunYingLogic.sendGroupUpdate(YYInfo[funcID], human, absConfig.panelID)
    end   
end


function isRed(human,YYInfo, funcConfig)
    local id = funcConfig.funcID
    --[[
    if funcConfig.panelID == PanelDefine.PANEL_ID_3106 then
        id = 11001 
    elseif funcConfig.panelID == PanelDefine.PANEL_ID_3309 then
        id = 7001
    end
    ]]
  

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

    AbsActLogic.checkAbsActClean(human, id)
    local absConfig = AbsActExcel.absActivity[id]
    local absAct = human.db.absAct[id]
    local now = os.time()
    local getDb = absAct.rechargeBackGet or {}
    local rechargeNum = absAct.topupAcount or 0
    for k, config in pairs(AbsActExcel.rechargeBack) do
        if config.actID == absConfig.actId and rechargeNum >= config.need and not getDb[k] then
            return true
        end
    end
    return false         
end



function isOpen(human, YYInfo, funcConfig)
    local state,endTime, starTime = AbsActLogic.isStarted(human, funcConfig.funcID) 
    if state then
        return true
    end    
    
    --[[
 local state,id = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_10)
    if not state then return end

    return true
    ]]
   
end

--
function getLeftTime(human, YYInfo, funcConfig)
    if isOpen(human, YYInfo, funcConfig) then
        if funcConfig.panelID == PanelDefine.PANEL_ID_3309 then
            local absConfig = AbsActExcel.absActivity[7001]
            return absConfig.realEndTime - os.time()
        end
    end
    return 0
end

