-- 在线奖励 --
-- 1、在线计时：当前倒计时完毕，会继续下一物品倒计时
-- 2、不在线计时：倒计当前物品剩余时间，不会继续下一物品倒计时
local OnlineRewardExcel = require("excel.onlineReward")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")

local STATUS_CANTGET = 0  -- 不可领
local STATUS_CANGET = 1   -- 可领
local STATUS_HADGET = 2   -- 已领

-- 初始
function initDB(human)
	human.db.onlineReward = {}
    human.db.onlineReward.ts = 0 		-- 累计时间：在线+部分离线
    human.db.onlineReward.getId = {} 	-- 已领取id
end

-- 在线时间 
function getOnlineTime(human)
	if not human.db.onlineReward then
		initDB(human)
	end
	return human.db.onlineReward.ts + os.time() - human.db.lastLoginTime	
end

-- 剩余时间
function getLeftTime(human, id)
	local config = OnlineRewardExcel.onlineReward[id]
	if not config then
		return 0
	end
	return math.max(config.useTime - getOnlineTime(human), 0)
end

-- 状态
function getStatus(human, id)
	local config = OnlineRewardExcel.onlineReward[id]
	if not config then
		return STATUS_CANTGET
	end	

	if human.db.onlineReward.getId[id] then
		return STATUS_HADGET
	end

	if getLeftTime(human, id) > 0 then
		return STATUS_CANTGET
	end

	return STATUS_CANGET
end

function getIDByTime(time)
	for id, config in ipairs(OnlineRewardExcel.onlineReward) do
		if config.useTime > time then
			return id, config
		end
	end
end

-- 是否全部领取
function isAllGet(human)
    if not human.db.onlineReward then
		initDB(human)
	end
    for k,_ in pairs(OnlineRewardExcel.onlineReward) do
        if type(human.db.onlineReward.getId) == "table" and not human.db.onlineReward.getId[k] then
            return nil,k
        end
    end
    return true 
end

-- 登陆
function onLogin(human)
	if isAllGet(human) then return end

	if (human.db.lastLogoutTime or 0) > 0 then
		local id, config = getIDByTime(human.db.onlineReward.ts)

		local time = os.time() - human.db.lastLogoutTime
		if config then
			human.db.onlineReward.ts = math.min(human.db.onlineReward.ts + time, config.useTime)
		end 		
	end

	sendOnlineReward(human)
end

-- 登出
function onLogout(human, time)
	if not human.db.onlineReward then
		initDB(human)
	end

	human.db.onlineReward.ts = human.db.onlineReward.ts + time	
end

-- 发送信息
function sendOnlineReward(human)
	local msgRet = Msg.gc.GC_ONLINE_REWARD_DATA
	local _,nowID = isAllGet(human)
    if nowID then
	    local nowConfig = OnlineRewardExcel.onlineReward[nowID]
        Grid.makeItem(msgRet.item, nowConfig and nowConfig.itemID or 0, nowConfig and nowConfig.itemCnt or 0)
    else
        Grid.makeItem(msgRet.item, 0, 0)
    end
	local onlineRewardConfig = OnlineRewardExcel.onlineReward
    msgRet.leftTime = 0
  
  	msgRet.allItem[0] = 0
    for id, config in ipairs(OnlineRewardExcel.onlineReward) do
    	msgRet.allItem[0] = msgRet.allItem[0] + 1
    	local net = msgRet.allItem[msgRet.allItem[0]]
    	net.id = id
    	net.status = getStatus(human, id)
	    Grid.makeItem(net.item, config.itemID, config.itemCnt)
	    if net.status == STATUS_CANTGET and msgRet.leftTime < 1 then
        	msgRet.leftTime = getLeftTime(human, id)
        end
    end
    --Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

-- 获取奖励
function getReward(human, id)
	-- local status = getStatus(human, id)
	-- if status ~= STATUS_CANGET then
	-- 	return
	-- end

	-- local config = OnlineRewardExcel.onlineReward[id]    
    -- human.db.onlineReward.getId[id] = 1
	-- -- 增加物品
	-- BagLogic.cleanMomentItemList()
	-- BagLogic.updateMomentItem(1, config.itemID, config.itemCnt)
	-- BagLogic.addMomentItemList(human, "online_reward")
	-- sendOnlineReward(human)

	-- 改为一键领取
	local itemList = {}
	for k, v in ipairs(OnlineRewardExcel.onlineReward) do
		local status = getStatus(human, k)
		if status == STATUS_CANGET then
			human.db.onlineReward.getId[k] = 1
			itemList[v.itemID] = (itemList[v.itemID] or 0) + v.itemCnt
		end
	end

	if next(itemList) then
		BagLogic.addItemList(human, itemList, "online_reward")
		sendOnlineReward(human)
	end
end

function isDot(human)
	if isAllGet(human) then return end
    for id in ipairs(OnlineRewardExcel.onlineReward) do
    	local status = getStatus(human, id)
    	if status == STATUS_CANTGET then
    		break
    	end
    	if status == STATUS_CANGET then
    		return true
    	end
    end
end

