---  限时礼包
--[[
    db.limitGiftBag = {
        actTime   =  {}         --  激活方式 的激活时间   [1] =  time
        actNum    =  {}         -- 激活方式  的激活次数   [1] =  10
        bagBuy    =  {}         -- 礼包购买状态        [1]  = 1  重新激活时 清除

    }

]]

local Msg = require("core.Msg")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local BuyLogic = require("topup.BuyLogic")
local LimitGiftBagExcel = require("excel.limitBuy").limitGiftBag


LIMITGIFTBAG_TYPE_1    =  1    -- 获得X星英雄
LIMITGIFTBAG_TYPE_2    =  2    -- 达到X级
LIMITGIFTBAG_TYPE_3    =  3    -- 爬塔X层

-- 激活方式 (同种激活方式激活 一起激活   策划需求 这里定义是为了防止 策划忘记  )
local LIMITGIFTBAG_ACT_TYPE_1 = 1     -- 获得5X英雄
local LIMITGIFTBAG_ACT_TYPE_2 = 2     -- 获得6X英雄
local LIMITGIFTBAG_ACT_TYPE_3 = 3     -- 达到20级
local LIMITGIFTBAG_ACT_TYPE_4 = 4     -- 达到30级
local LIMITGIFTBAG_ACT_TYPE_5 = 5     -- 爬塔100层




function inintDB(human)
    if not human.db.limitGiftBag then
        human.db.limitGiftBag = {} 
        human.db.limitGiftBag.actTime = {}
        human.db.limitGiftBag.actNum = {}
        human.db.limitGiftBag.bagBuy = {}
    end
end

local PERSON_LIMITGIFTBAG_MAX_TIME = nil          -- actType 最大持续时间
local PERSON_LIMITGIFTBAG_ID_ACTTYPE = nil        -- ID 所对应的 actType
local PERSON_LIMITGIFTBAG_ACTTPE_ID = nil         -- 同激活方式的 ID

local function updateExcel()
    if PERSON_LIMITGIFTBAG_MAX_TIME then return end
    PERSON_LIMITGIFTBAG_MAX_TIME = {}
    PERSON_LIMITGIFTBAG_ID_ACTTYPE = {}
    PERSON_LIMITGIFTBAG_ACTTPE_ID = {}
    for k, v in ipairs(LimitGiftBagExcel) do
        PERSON_LIMITGIFTBAG_ID_ACTTYPE[k] = v.actType
        PERSON_LIMITGIFTBAG_ACTTPE_ID[v.actType] =  PERSON_LIMITGIFTBAG_ACTTPE_ID[v.actType] or {}
        PERSON_LIMITGIFTBAG_ACTTPE_ID[v.actType][#PERSON_LIMITGIFTBAG_ACTTPE_ID[v.actType] + 1] = k
        if not PERSON_LIMITGIFTBAG_MAX_TIME[v.actType] then
            PERSON_LIMITGIFTBAG_MAX_TIME[v.actType] = v.endTime
        elseif PERSON_LIMITGIFTBAG_MAX_TIME[v.actType] and PERSON_LIMITGIFTBAG_MAX_TIME[v.actType] < v.endTime then
            PERSON_LIMITGIFTBAG_MAX_TIME[v.actType] = v.endTime
        end
    end       
end

-- 获取最老结束时间
local function getMaxEndTime(actType)
    updateExcel()
    return PERSON_LIMITGIFTBAG_MAX_TIME[actType]
end

-- 获取该激活类型 是否已经激活
local function getActTypeState(human, actType, now)
    now = now or os.time()
    local maxEndTime = getMaxEndTime(actType)
    if maxEndTime <= 0 then return assert(nil,  "actType  endTime is not find : " .. actType .. "  .") end 
    local lastTime = human.db.actTime[actType] or 0
    return now < lastTime + maxEndTime or false
end

local function getIdState(time, id, now)
    local config = LimitGiftBagExcel[id]
    local endTime = time + config.endTime
    return endTime - now > 0 and true or false
end

-- 清理掉上次 激活时  购买次数
local function cleanGiftBagBuy(human, actType)
    for _, id in ipairs(PERSON_LIMITGIFTBAG_ACTTPE_ID[actType]) do
        human.db.limitGiftBag.bagBuy[id] = nil
    end
end

-- 获取 限时礼包是否能购买
local function getGiftBagBuyState(human, config)
    if not human.db.limitGiftBag.bagBuy[config.id] then return true end
    if config.canBuy > 0 and human.db.limitGiftBag.bagBuy[config.id] >= config.canBuy then
        return false
    end
    return true
end

function fontLimitBagNet(human, msgRet, id, actTime, now)
    local config = LimitGiftBagExcel[id]
    local state = getIdState(actTime, id, os.time())
    if not state then return end
    msgRet.list[0] = msgRet.list[0] + 1
    local net = msgRet.list[msgRet.list[0]] 
    net.id = id
    net.name = config.name
    net.buy = human.db.limitGiftBag.bagBuy[id] and human.db.limitGiftBag.bagBuy[id] or 0
    net.maxBuy = config.canBuy
    net.endTime = actTime + config.endTime - now
    net.endTime = net.endTime > 0 and net.endTime or 0
    BuyLogic.fontBuyItem(human,net.buyItem, config.buyID) 

    for k, v in ipairs(config.item) do
        Grid.makeItem(net.reward[k], v[1], v[2])
    end
    net.reward[0] = #config.item
    
end

-- 查询
function query(human)
    local msgRet = Msg.gc.GC_PRESENT_LIMITGIFT_BAG_QUERY
    if not human.db.limitGiftBag then
        msgRet.list[0] = 0
        Msg.send(msgRet, human.fd)
    end
    
    local now = os.time()
    for k, v in pairs(human.db.limitGiftBag.actTime) do
        if getActTypeState(human, k, now) then
            for _,id in ipairs(PERSON_LIMITGIFTBAG_ACTTPE_ID[k]) do
                fontLimitBagNet(human, msgRet, id, v, now)
            end
        end
    end 
    Msg.send(msgRet, human.fd)
end

-- 激活
local function active(human, actType)
    if true then
        return
    end

    inintDB(human)
    if not getActTypeState(human, actType, os.time()) then
        human.db.limitGiftBag.actTime[actType] = os.time()
        cleanGiftBagBuy(human, actType)
    end
end

-- 尝试激活
function touchActive(human, type, cnt)
    updateExcel()
    local activeList = {}
    for k, v in ipairs(LimitGiftBagExcel) do
        if v.type == type and v.need <= cnt then
            activeList[v.actType] = 1
        end
    end

    for actType in pairs(activeList) do
        active(human, actType)
    end
end

-- 购买 后台触发 发送
function buy(human, id)
    if true then
        return
    end

    local config = LimitGiftBagExcel[id]
    if not config then return end
    local state = getGiftBagBuyState(human, config)
    if not state then return end
    BagLogic.addItemList(human, config.item, "limitBag")
    
end