--[[
    absAct[id] = {
        count = 1000
        reward[id] = {
            state = 1
        }
    }

]]


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")
local MailExcel = require("excel.mail")

TOTAL_REACH_TYPE_1 = 1          -- �߳�
TOTAL_REACH_TYPE_2 = 2          -- ��ֵ
TOTAL_REACH_TYPE_3 = 3          -- �����ٻ�
TOTAL_REACH_TYPE_4 = 4          -- ��ʯ����

local ActivityCleanMailId = 8001

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 getLeftTime(human,YYInfo,funcConfig)
    local ok,start,finish = isOpen(human,YYInfo,funcConfig)
    if not ok then 
        return 0
    end
    return finish - os.time()
end

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

local function fontDabiaoNet(db,net,config,id)
    net.id = id
    net.needCnt = config.needCnt
    net.desc = Util.format(config.desc, config.needCnt)
    net.state = db.reward and (db.reward[id] or 0) or 0
    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.totalReach
    local msgRet = Msg.gc.GC_ABS_XS_REACH_QUERY
    msgRet.nowCnt = absAct.count or 0
    local len = #config
    local count = 0
    for i = 1,len do
        local v = config[i]
        if v.actId == absConfig.actId and v.mainId == id 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 onDrawCard(human,funcID, parameter)
    changeReach(human,funcID,TOTAL_REACH_TYPE_1,parameter)
end

-- ����
function onTMDrawCard(human,funcID, parameter)
    changeReach(human,funcID,TOTAL_REACH_TYPE_3,parameter)
end

-- ��ֵ
function onCharge(human, parameter,funcID)
    changeReach(human,funcID,TOTAL_REACH_TYPE_2,parameter)
end

-- ������ʯ
function onDecZuanshi(human,funcID,parameter)
    changeReach(human,funcID,TOTAL_REACH_TYPE_4,parameter)
end


function changeReach(human,id,type,param)
    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then return end

    if type ~= absConfig.param[1] then
        return
    end
    
    AbsActLogic.checkAbsActClean(human, id)
    local absAct = human.db.absAct[id]
    if not absAct then
        return
    end

    --�������� 
    absAct.count = absAct.count or 0
    absAct.count = absAct.count + param
    absAct.reward = absAct.reward or {}

    local config = AbsActExcel.totalReach
    local len = #config
    for i = 1,len do
        if config[i].actId == absConfig.actId and config[i].mainId == id then
            -- �ж������Ƿ�����
            if not absAct.reward[i] and absAct.count >= config[i].needCnt then
                absAct.reward[i] = 1
            end
        end
    end

    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, absConfig.panelID)
end


function getTotalReachReward(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
    
    AbsActLogic.checkAbsActClean(human, id)
    local absAct = human.db.absAct[id]
    if not absAct then
        return
    end

    -- ״̬��Ϊ���죬����
    absAct.reward = absAct.reward or {}
    if absAct.reward[giftId] ~= 1 then
        return
    end

    local config = AbsActExcel.totalReach[giftId]

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

    -- ����״̬Ϊ����ȡ
    absAct.reward[giftId] = 2

    -- ������
    BagLogic.addItemList(human, config.item, "abs_totalReach")   
    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 == 1 then
            return true
        end
    end

    return false
end

-- 活动过期相关处理
function clean(human,id) 
    local config = AbsActExcel.absActivity[id]

    local mailCfg = MailExcel.mail[ActivityCleanMailId]
    local items = {}
    local absAct = human.db.absAct[id]
    local reachCfg = AbsActExcel.totalReach
    for idx,state in pairs(absAct.reward) do 
        if state == 1 then 
            for _,item in pairs(reachCfg[idx].item) do 
                items[#items + 1] = item
            end
        end
    end
    -- 处理充值相关
    if config.param[1] == TOTAL_REACH_TYPE_2 then 
        MailManager.add(1, human.db._id, mailCfg.title, mailCfg.content, items, mailCfg.senderName)
    end
end