--- 限时盲盒




local CommonDB = require("common.CommonDB")
local Util = require("common.Util")
local Lang = require("common.Lang")
local ObjHuman = require("core.ObjHuman")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local BuyLogic = require("topup.BuyLogic")
local AbsActDefine = require("absAct.AbsActDefine")
local AbsActLogic = require("absAct.AbsActLogic")
local OpenActExcel = require("excel.openAct")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")

PRESENT_LIMIT_MANGHE_LIST = nil
function initAfterHot()

 --   if PRESENT_LIMIT_MANGHE_LIST then return end   -- 策划测试 OS 修改可能会导致 不会重复 载入 去掉
    local oepenTime = CommonDB.getServerOpenTime()
    if oepenTime == 0 then return end
    PRESENT_LIMIT_MANGHE_LIST = {}
    local oneDay = 24 * 60 * 60
    for k, config in pairs(OpenActExcel.limitMangHe) do
        if not PRESENT_LIMIT_MANGHE_LIST[config.actId] then
            PRESENT_LIMIT_MANGHE_LIST[config.actId] = {}
            local time  =  (config.openStart - 1) * oneDay + oepenTime
            local time2 =  config.openEnd * oneDay + oepenTime
            local startTime = Util.getDayStartTime(time)
            local endTime = Util.getDayStartTime(time2) - 1
            -- 如果是当天就结束的活动
            if config.openStart == config.openEnd then
                endTime = startTime + oneDay - 1
            end
            PRESENT_LIMIT_MANGHE_LIST[config.actId].startTime = startTime
            PRESENT_LIMIT_MANGHE_LIST[config.actId].endTime = endTime 
            config.startTime = startTime
            config.endTime = endTime
        end
        PRESENT_LIMIT_MANGHE_LIST[config.actId].list = PRESENT_LIMIT_MANGHE_LIST[config.actId].list or {}
        local len = #PRESENT_LIMIT_MANGHE_LIST[config.actId].list
        PRESENT_LIMIT_MANGHE_LIST[config.actId].list[len + 1] = {}
        PRESENT_LIMIT_MANGHE_LIST[config.actId].list[len + 1] = k
    end
end

function isOpen(human, YYInfo, funcConfig)
    if not PRESENT_LIMIT_MANGHE_LIST then return end
    local now = os.time()
    local config = PRESENT_LIMIT_MANGHE_LIST[funcConfig.param]
    if not config then return end
    
    if now >= config.startTime and now < config.endTime then
        return true
    end 
end

--
function getLeftTime(human, YYInfo, funcConfig)
    if isOpen(human, YYInfo, funcConfig) then
        local config = PRESENT_LIMIT_MANGHE_LIST[funcConfig.param]
        return config.endTime - os.time()
    end
    return 0
end

function getBuyCnt(human, id)
    if human.db.limitMangHe and human.db.limitMangHe[id] then
        return human.db.limitMangHe[id]
    end
    return 0
end

function getBoxState(human, param)
    local now = os.time()
    local limitConfig =  PRESENT_LIMIT_MANGHE_LIST[param]
    if now >= limitConfig.startTime and now < limitConfig.endTime then
        if not human.db.limitMangHe or not human.db.limitMangHe.boxGet or not human.db.limitMangHe.boxGet[param] or not Util.isSameDay(human.db.limitMangHe.boxGet[param]) then
           return true
        end
   end      
     
    return false
end

function fontGift(human, net , id)
    local config =  OpenActExcel.limitMangHe[id]
    net.id = id
    for i = 1, #config.reward do
        Grid.makeItem(net.item[i], config.reward[i][1], config.reward[i][2])
    end
    net.item[0] = #config.reward
    net.cnt = getBuyCnt(human, id)
    net.maxCnt = config.cnt
    net.name = config.name or ""
    BuyLogic.fontBuyItem(human, net.buyMsg, config.buyID)
end

function query(human, param)
    local now = os.time()
    local limitConfig =  PRESENT_LIMIT_MANGHE_LIST[param]
    if now < limitConfig.startTime or now >= limitConfig.endTime then
        return 
    end  

    local msgRet = Msg.gc.GC_OA_LIMIT_MANGHE_QUERY
    for k, id in ipairs(limitConfig.list) do
        local net = msgRet.list[k]
        fontGift(human, msgRet.list[k], id)
    end
    msgRet.list[0] = #limitConfig.list
    msgRet.startTime = limitConfig.startTime
    msgRet.endTime = limitConfig.endTime
    msgRet.param = param
    msgRet.state = getBoxState(human, param) and 1 or 0
 --  Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

function buy(human, buyID)

    -- 存在多个特惠礼包 同时   开放
    local actId
    local id 
    for k, config in pairs(OpenActExcel.limitMangHe) do
        if config.buyID == buyID then
            actId = config.actId
            id = k
            break
        end
    end
    if not id then return end

    local now = os.time()
    local limitConfig =  PRESENT_LIMIT_MANGHE_LIST[actId]
    if now < limitConfig.startTime or now >= limitConfig.endTime then
        return 
    end  

    local config = OpenActExcel.limitMangHe[id]

    if getBuyCnt(human, id) >= config.cnt then
        return
    end

    -- 当金币购买之后超过最大金币时，不允许购买
    for j = 1, #config.reward do
        local itemID = config.reward[j][1]
        local itemCnt = config.reward[j][2]
        if itemID == ItemDefine.ITEM_JINBI_ID then 
            if not ObjHuman.canAddJinbi(human, itemCnt) then
               return
            end
        end
    end

    human.db.limitMangHe = human.db.limitMangHe or {}
    human.db.limitMangHe[id] = human.db.limitMangHe[id] or 0
    human.db.limitMangHe[id] = human.db.limitMangHe[id] + 1

    BagLogic.addItemList(human, config.reward , "limit_mangHe")
    Broadcast.sendErr(human, Lang.ITEM_BUY_SUCCESS)
    query(human, config.actId)
end

function getBox(human, param)
    if getBoxState(human, param) then
        local now = os.time()
        local get = false
        local limitConfig = PRESENT_LIMIT_MANGHE_LIST[param]
        if now >= limitConfig.startTime and now < limitConfig.endTime then
            if not human.db.limitMangHe or not human.db.limitMangHe.boxGet or not human.db.limitMangHe.boxGet[param] 
                 or not Util.isSameDay(human.db.limitMangHe.boxGet[param]) then
                   human.db.limitMangHe = human.db.limitMangHe or {}
                   human.db.limitMangHe.boxGet = human.db.limitMangHe.boxGet or {}
                   human.db.limitMangHe.boxGet[param] = now 
                   get = true
            end
       end     

       if get then
            for k, config in pairs(OpenActExcel.limitMangHe) do
                if config.actId == param and #config.dayReward > 0 then
                    BagLogic.addItemList(human, config.dayReward, "limit_mangHe")
                    break
                end
            end
            
            query(human, param)
            
	        for k, v in pairs(funcID) do
                YunYingLogic.updateIcon(YYInfo[k], human)
	            YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3606,param)
                break
            end
       end
      
    end 
end

function isRed(human,YYInfo, funcConfig)
    local now = os.time()
    local limitConfig = PRESENT_LIMIT_MANGHE_LIST[funcConfig.param]
    if now >= limitConfig.startTime and now < limitConfig.endTime then
        if getBoxState(human, funcConfig.param) then
            return true
        end
    end
end