local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local RoleDBLogic = require("role.RoleDBLogic")
local Broadcast = require("broadcast.Broadcast")
local LuaMongo = _G.lua_mongo
local DB = require("common.DB")

BAN_LIST_MAX_CNT = 20 -- 最多屏蔽20人

fields = {name = 1, lv = 1}

function setBan(human, uuid, op)
	--自己返回掉
	if human.db._id == uuid then
		Broadcast.sendErr(human, Lang.CHAT_BAN_NOT_SET_SELF)
        return
    end
 
	human.db.chatBan = human.db.chatBan or {}
	
	--1屏蔽 0解除屏蔽
	if op == 1 then
		for i = 1, #human.db.chatBan do
			if human.db.chatBan[i].uuid == uuid then
				return
			end
		end
		
		local target = RoleDBLogic.getDb(uuid, fields)
		if target == nil then
			return
		end
		
		human.db.chatBan[#human.db.chatBan + 1] = uuid
		
		for i = 1, 100 do
			if #human.db.chatBan > BAN_LIST_MAX_CNT then
				table.remove(human.db.chatBan, 1)
			else
				break
			end
		end
		
		Broadcast.sendErr(human, Lang.CHAT_BAN_SET)
	elseif op == 0 then
		local index = nil
		for i = 1, #human.db.chatBan do
			if human.db.chatBan[i] == uuid then
				index = i
				break
			end
		end
	
		if index == nil then
			return
		end
		
		table.remove(human.db.chatBan, index)
		
		Broadcast.sendErr(human, Lang.CHAT_BAN_UNSET)
	else
		return
	end
	
	sendBanList(human)
end

function sendBanList(human)
	local msgRet = Msg.gc.GC_CHAT_BAN_LIST
	
	if human.db.chatBan == nil then
		msgRet.banList[0] = 0
	else
		msgRet.banList[0] = #human.db.chatBan
		
		for i = 1, #human.db.chatBan do
			local tempUuid = human.db.chatBan[i]
			msgRet.banList[i].uuid = tempUuid
		end
	end
	
	Msg.send(msgRet, human.fd)
end

function jubaoChat(human,msg)
    local fakeHuman = ObjHuman.onlineUuid[msg.uuid]
    local db = nil
    if fakeHuman ~= nil then
        db = fakeHuman.db
    else
        db = RoleDBLogic.getDb(msg.uuid,{banSay = 1,banSayTime = 1,banSayReason = 1})
    end
    if db == nil then
        return
    end
    if db.banSayTime and os.time() > db.banSayTime and db.banSayTime ~= -1 then
        db.banSay = nil
        db.banSayTime = nil
    end
    if db.banSay ~= nil and db.banSay.flag ~= nil and db.banSay.flag == 1 then
        return
    end
    db.banSay = db.banSay or {}
    db.banSay.array = db.banSay.array or {}
    local len = #db.banSay.array
    -- 处理一下老unionID
    for i = 1,len do
        if db.banSay.array[i].unionID ~= nil then
            db.banSay.array[i].unionID = nil
            db.banSay.array[i].unionUuid = ""
        end
    end
    for i = 1,len do
        if db.banSay.array[i].uuid == human.db._id then
            return
        end
    end
    local now = os.time()
    db.banSay.array[len + 1] = {}
    db.banSay.array[len + 1].uuid = human.db._id
    db.banSay.array[len + 1].unionUuid = human.db.unionUuid or "" 
    db.banSay.array[len + 1].time = now
    db.banSay.flag = 0
    if len + 1 >= 5 then
        if db.banSay.array[len + 1 - 5 + 1].time >= now - 10*60 then
            db.banSay.flag = 1
        end
    else
        local unionCnt = {}
        local lenth = #unionCnt
        local isBreak = 0
        for i = 1, len + 1 do
            local config = db.banSay.array
            if i == 1 then
                if config[i].time >= now - 1*60 then
                    lenth = lenth + 1
                    unionCnt[lenth] = {}
                    unionCnt[lenth].cnt = 1
                    unionCnt[lenth].id = config[i].unionUuid
                end
            else
                for j = 1,lenth do
                    if config[i].time >= now - 1*60 then
                        if config[i].unionUuid == unionCnt[j].id then
                            unionCnt[j].cnt = unionCnt[j].cnt + 1
                        else
                            lenth = lenth + 1
                            unionCnt[lenth] = {}
                            unionCnt[lenth].cnt = 1
                            unionCnt[lenth].id = config[i].unionUuid
                            if lenth >= 3 then
                                db.banSay.flag = 1
                                isBreak = 1
                                break
                            end
                        end
                    end
                end
            end
            if isBreak == 1 then
                break
            end
        end
    end
    local banTime = nil
    if db.banSay.flag == 1 then
         banTime = os.time() + 60*60
         db.banSayReason = Lang.CHAT_BAN_REASON_JUBAO
    end
    if db.banSayTime == nil or db.banSayTime == -1 then
        db.banSayTime = banTime
    end
    if fakeHuman == nil then
        local QueryCharByUuid = {}
        QueryCharByUuid._id = db._id
        local update = {}
        update.banSay = db.banSay
        update.banSayTime = db.banSayTime
        update.banSayReason = db.banSayReason
        local tbTemp = {}
        tbTemp["$set"] = update
        LuaMongo.update(DB.db_char,QueryCharByUuid,tbTemp)
    end
    Broadcast.sendErr(human, Lang.CHAT_BAN_JUBAO_SUCCESS)
end

function initAfterHot()
    local update = {}
    update.banSay = 1
    update.banSayTime = 1
    update.banSayReason = ""
    local tbTemp = {}
    tbTemp["$unset"] = update
    LuaMongo.update(DB.db_char,{},tbTemp,2)
end