-----------------------------------------------------------------
-- 超值首充

-- db.firstCharge
-- firstCharge[type].ts 激活时间
-- firstCharge.getList 奖励领取次数 firstCharge.getList[id] = cnt
-- firstCharge.freeTime 每日登陆奖励领取时间 firstCharge.freeTime[type] = time
-----------------------------------------------------------
local Msg = require("core.Msg")
local Util = require("common.Util")
local PresentExcel = require("excel.present")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local PanelDefine = require("broadcast.PanelDefine")
local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local SceneHandler = require("scene.Handler")
local ObjHuman = require("core.ObjHuman")


local FIRST_CHARGE_TYPE1 = 1         -- 首充赠礼
local FIRST_CHARGE_TYPE2 = 2         -- 100元赠礼

-- 状态
STATE_CANT_GET = 0      -- 不可领
STATE_CAT_GET = 1       -- 可领
STATE_HAD_GET = 2       -- 已领

function isOpenByType(human,giftType)
    -- 老首充只有数据才显示true了
    if not human.db.firstCharge or not human.db.firstCharge[giftType] then 
        --return true
        return
    end

    for id,config in ipairs(PresentExcel.firstCharge) do
        if giftType == config.type then
            if getState(human, id, true) ~= STATE_HAD_GET then
                return true
            end
        end
    end
end

function isRedByType(human,giftType)
    if not human.db.firstCharge or not human.db.firstCharge[giftType] then 
        return 
    end

    for id,config in ipairs(PresentExcel.firstCharge) do
        if giftType == config.type then
            if getState(human, id) == STATE_CAT_GET then
                return true
            end
        end
    end
end

function isOpen(human,YYInfo,funcConfig)
	if not SceneHandler.canCharge(human) then
		return
	end

    return
    -- if isOpenByType(human,FIRST_CHARGE_TYPE1) or isOpenByType(human,FIRST_CHARGE_TYPE2) then
    --    return true
    -- end 
end

function isRed(human,YYInfo,funcConfig)
	if isRedByType(human,FIRST_CHARGE_TYPE1) or isRedByType(human,FIRST_CHARGE_TYPE2) then
       return true
    end
end

-- 封装结构体
local function fontNet(net, human, id, cf)
    if not net then return end
    net.id = id 
    net.status = getState(human, id)
    net.items[0] = #cf.reward  
    for i, item in ipairs(cf.reward) do
        Grid.makeItem(net.items[i], item[1], item[2])
    end
end

function sendQuery(human,giftType)
    ObjHuman.updateDaily(human)

    local msgRet = Msg.gc.GC_FIRST_CHARGE_GIFT_QUERY
    msgRet.value = human.db.topupAcount or 0
    msgRet.type = giftType
    
    msgRet.list[0] = 0
    msgRet.red[0] = 0
    for id, cf in ipairs(PresentExcel.firstCharge) do
        if cf.type == giftType then
            msgRet.list[0] = msgRet.list[0] + 1
            local index = msgRet.list[0]
            fontNet(msgRet.list[index], human, id, cf)
        end
		
        if id == 2 or id == 4 then
		   msgRet.red[0] = msgRet.red[0] + 1
		   msgRet.red[msgRet.red[0]] = getState(human, id)
        end
    end
    --Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

function getItems(human, type)
    ObjHuman.updateDaily(human)

    -- 领取奖励
    local items = {}
    local len = 0
    for id, cf in ipairs(PresentExcel.firstCharge) do
       if cf.type == type then
           local state = getState(human, id)    
           if state == STATE_CAT_GET then
              local cf = PresentExcel.firstCharge[id]
              if cf then 
                 human.db.firstCharge.getList = human.db.firstCharge.getList or {}
                 human.db.firstCharge.getList[id] = (human.db.firstCharge.getList[id] or 0) + 1
                 if cf.day == 0 then
                     human.db.firstCharge.freeTime = human.db.firstCharge.freeTime or {}
                     human.db.firstCharge.freeTime[cf.type] = os.time()
                 end
                 for k,v in pairs(cf.reward) do
                     local itemID = v[1]
                     local itemCnt = v[2]
                     items[itemID] = items[itemID] or 0
                     items[itemID] = items[itemID] + itemCnt

                     len = len + 1
                 end
              end
           end
       end
    end 
    
    if len > 0 then
       BagLogic.addItemList(human, items, "shouchong")

       for k, v in pairs(funcID) do
          YunYingLogic.updateIcon(YYInfo[k], human)
          YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3302)
          break
       end
       sendQuery(human, type)
    end
end

local TYPE_2_NEED = nil
function getNeedList()
    if not TYPE_2_NEED then
        TYPE_2_NEED = {}

        for id, cf in pairs(PresentExcel.firstCharge) do
            TYPE_2_NEED[cf.type] = cf.need
        end
    end
    return TYPE_2_NEED
end

-- 状态
function getState(human, id, byMax)
    local cf = PresentExcel.firstCharge[id]
    if not cf then
        return STATE_CANT_GET
    end

    local data = human.db.firstCharge and human.db.firstCharge[cf.type]
    if not data or not data.ts then
        return STATE_CANT_GET
    end

    if cf.day == 0 and not byMax then
        local freeTime = human.db.firstCharge.freeTime and human.db.firstCharge.freeTime[cf.type] or 0
        if Util.isSameDay(freeTime) then
            return STATE_HAD_GET
        end
    end

    local getCnt = human.db.firstCharge.getList and human.db.firstCharge.getList[id] or 0
    if getCnt >= cf.getMax then
        return STATE_HAD_GET
    end

    local day = Util.diffDay(data.ts) + 1
    if cf.day > day then
        return STATE_CANT_GET
    end

    return STATE_CAT_GET
end

-- 激活首充
function activateFirstCharge(human, ftype)
    human.db.firstCharge = human.db.firstCharge or {}
    if human.db.firstCharge[ftype] then return end

    human.db.firstCharge[ftype] = {}
    human.db.firstCharge[ftype].ts = os.time()

    if ftype == FIRST_CHARGE_TYPE1 then
        ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE15, 0)
    end
    return true
end

-- 充值回调
function onCharge(human)
    return
    -- local needList = getNeedList()
    -- local isChange = nil
    -- for ftype, need in pairs(needList) do
    --     if (human.db.topupAcount or 0) >= need and
    --         activateFirstCharge(human, ftype) then
    --         isChange = true
    --         sendQuery(human, ftype) 
    --     end
    -- end
    -- if isChange then
    --     for k, v in pairs(funcID) do
    --         YunYingLogic.updateIcon(YYInfo[k], human)
    --         break
    --     end
    -- end
end

function updateDaily(human)
    if human.db.firstCharge then
        human.db.firstCharge.freeTime = nil
    end
end


-- 是否已激活首充
function isActive(human, YYInfo, funcConfig)
--    if not human.db.firstCharge or not human.db.firstCharge[FIRST_CHARGE_TYPE1] then 
--       return 
--    end

--    return true
    return
end
