-----------------------------------------------------------
-- 防沉迷逻辑
-----------------------------------------------------------

local Util = require("common.Util")
local Lang = require("common.Lang")
local CommonDefine =  require("common.CommonDefine")
local ObjHuman = require("core.ObjHuman")
local Broadcast = require("broadcast.Broadcast")
local FcmDefine = require("fcm.FcmDefine")

-- 是否在健康时间段
HEALTH_TIME_INFO = HEALTH_TIME_INFO or nil
local function inHealthTime()
	local nowTime = os.time()
	if not HEALTH_TIME_INFO or nowTime >= HEALTH_TIME_INFO.dayEndTime then
		local dayStartTime = Util.getDayStartTime(nowTime)
		HEALTH_TIME_INFO = {}
		HEALTH_TIME_INFO.dayEndTime = dayStartTime + 86400
		HEALTH_TIME_INFO.startTime = dayStartTime + FcmDefine.CHILD_HEALTH_START_HOUR * 3600
		HEALTH_TIME_INFO.endTime = dayStartTime + FcmDefine.CHILD_HEALTH_END_HOUR * 3600
	end

	if HEALTH_TIME_INFO.startTime <= nowTime and 
		nowTime <= HEALTH_TIME_INFO.endTime then
		return true
	end
end

-- 检查是否通过防沉迷
local function check(human)
	if not FcmDefine.IS_FCM_OPEN then
		return true
	end

	local fcmState = human.fcmState or FcmDefine.STATE_NOPEN
	if fcmState == FcmDefine.STATE_NOPEN then -- 默认不开
		return true
	end

	-- 如果已验证&成年
	if fcmState == FcmDefine.STATE_OK_ADULT then
		return true
	end

	-- 在不健康时间段游戏
	if not inHealthTime() then
		return false, CommonDefine.DISCONNECT_FCM_HEALTH
	end

	-- 今日在线时间
	local time = os.time() - human.db.lastLoginTime
	local onlineTimeDay = (human.db.onlineTimeDay or 0) + time 

	--print("onlineTimeDay", onlineTimeDay)

	if onlineTimeDay >= FcmDefine.CHILD_DAILY_TIME then
		return false, CommonDefine.DISCONNECT_FCM_CHILD
	end
	return true
end

-- 防沉迷校验含踢人
function checkFcm(human)
	if not human.db then return true end
	local isok, code = check(human)
	if not isok then
		ObjHuman.doDisconnect(human, code)
	end
	if human.fcmState then
		--print("isok, code",isok, code, human.fcmState)
	end
	return isok
end

-- 充值限额
local BUY_PRICE_ONE2 = 50 	-- 未满16单笔金额
local BUY_PRICE_MONTH2 = 200 -- 未满16每月金额
local BUY_PRICE_ONE3 = 100 	-- 未满18单笔金额
local BUY_PRICE_MONTH3 = 400 -- 未满18每月金额
local function checkBuyPrice(human, price, dayPrice, monthPrice, age)
	if price > dayPrice then -- 单笔不能超过50元
		Broadcast.sendErr(human, Util.format(Lang.BUY_CHECK_ERR_FCM_AGE, age))
		Broadcast.sendErr(human, Util.format(Lang.BUY_CHECK_ERR_FCM_ONE, dayPrice))
		return false
	end

	local sum = human.db.topupAccountMonth or 0
	if (sum + price) > monthPrice then -- 每月不能超过200元
		Broadcast.sendErr(human, Util.format(Lang.BUY_CHECK_ERR_FCM_AGE, age))
		Broadcast.sendErr(human, Util.format(Lang.BUY_CHECK_ERR_FCM_MONTH, monthPrice))
		return false
	end
	return true
end

function checkCanBuy(human, price)	
	if not human.db then return true end

	local fcmState = human.fcmState or FcmDefine.STATE_NOPEN
	print("checkCanBuy",price,fcmState)
	if fcmState == FcmDefine.STATE_NOPEN then -- 默认不开
		return true
	end

	-- 如果已验证&成年
	if fcmState == FcmDefine.STATE_OK_ADULT then
		return true
	end

	-- 未满8岁
	if fcmState == FcmDefine.STATE_OK_CHILD then
		Broadcast.sendErr(human, Util.format(Lang.BUY_CHECK_ERR_FCM_AGE, 8))
		Broadcast.sendErr(human, Lang.BUY_CHECK_ERR_FCM_NOCHARGE)
		return false
	end

	-- 未满16岁
	if fcmState == FcmDefine.STATE_OK_CHILD2 then
		return checkBuyPrice(human, price, BUY_PRICE_ONE2, BUY_PRICE_MONTH2, 16)
	end

	-- 未满18岁
	if fcmState == FcmDefine.STATE_OK_CHILD3 then
		return checkBuyPrice(human, price, BUY_PRICE_ONE3, BUY_PRICE_MONTH3, 18)
	end	

	-- 未通过
	Broadcast.sendErr(human, Lang.BUY_CHECK_ERR_FCM)
	Broadcast.sendErr(human, Lang.BUY_CHECK_ERR_FCM_NOCHARGE)
	return false
end

function onTimer()
	for _, human in pairs(ObjHuman.onlineUuid) do
		if human.fd then
			checkFcm(human)
		end
	end
end

function CG_FMC_STATE(human, state)
	human.fcmState = state
	checkFcm(human)
end