--[[
    absAct[id] = {
        gift[id] = state
    }


]]


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")


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

local function fontSeckillNet(human,net,absAct,i,v)
    net.id = i
    net.state = 1
    if absAct.gift and absAct.gift[i] then
        net.state = absAct.gift[i]
    end
    net.price = v.price
    net.name = v.name
    BuyLogic.fontBuyItem(human, net.buyItem, v.buyID)
    local rewardLen = #v.item
    for j = 1,rewardLen do
        local itemID = v.item[j][1]
        local itemCnt = v.item[j][2]
        Grid.makeItem(net.item[j],itemID,itemCnt)
    end
    net.item[0] = rewardLen
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 nowDay = Util.diffDay(starTime) + 1
    -- 构造协议
    local config = AbsActExcel.seckill
    local msgRet = Msg.gc.GC_ABS_HF_SECKILL_GIFT_QUERY
    local len = #config
    local count = 0
    msgRet.oneKeyGift[0] = 0
    for i = 1,len do
        local v = config[i]
        if v.actId == absConfig.actId and v.day == nowDay then
            -- v.cont 里面没有配置数据，代表非一键购买配置
            if not next(v.cont) then
                count = count + 1
                local net = msgRet.giftList[count]
                fontSeckillNet(human,net,absAct,i,v)
            elseif msgRet.oneKeyGift[0] == 0 then
                -- 一键购买
                local contLen = #v.cont
                local continue = false
                for s = 1,contLen do
                    local index = v.cont[s]
                    -- 配置中的礼包已被购买，跳出，到下一个
                    if absAct.gift and absAct.gift[index] then
                        continue = true
                        break
                    end
                end
                if not continue then
                    local net = msgRet.oneKeyGift[1]
                    fontSeckillNet(human,net,absAct,i,v)
                    msgRet.oneKeyGift[0] = 1
                end
            end
        end
    end
    msgRet.giftList[0] = count
    Msg.send(msgRet,human.fd)
end

-- 礼包购买回调
function seckillBuy(human,id,conf)
    seckillOnBuy(human,id,conf.args[1])
end

function seckillOnBuy(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

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

    local nowDay = Util.diffDay(starTime) + 1

    absAct.gift = absAct.gift or {}
    local config = AbsActExcel.seckill[giftId]
    if nowDay ~= config.day then
        return
    end
    -- 判断是否为一键购买
    if next(config.cont) then
        local len = #config.cont
        -- 一键购买需判断内部所有子礼包是否已被购买，若已被购买，则跳出
        for i = 1,len do
            if absAct.gift[config.cont[i]] == 2 then
                return
            end
        end

        -- 设置每个子礼包状态为已购买
        for i = 1,len do
            absAct.gift[config.cont[i]] = 2
        end
    else
        -- 判断当前礼包是否已被购买
        if absAct.gift[giftId] == 2 then
            return
        end
        -- 设置状态
        absAct.gift[giftId] = 2
    end

    BagLogic.addItemList(human, config.item, "abs_seckill")   
    getAndSendMsg(human,id)
    
    YunYingLogic.sendBanner(human)
    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
    
    local nowDay = Util.diffDay(starTime) + 1

    absAct.gift = absAct.gift or {}
    local config = AbsActExcel.seckill
    local len = #config
    for i = 1,len do
        if config[i].actId == absConfig.actId and nowDay == config[i].day then
            if not next(config[i].cont) then
                if absAct.gift[i] ~= 2 then
                    return true
                end
            end
        end
    end
    return false
end