g_BuddyMsgMgr = {}
g_BuddyMsgMgr.Messages = {}
g_BuddyMsgMgr.UserInfo = {}


function g_BuddyMsgMgr.AddMsg(nQQUin, szTime, strMsg)
	local t = {nQQUin, szTime, strMsg}
	table.insert(g_BuddyMsgMgr.Messages, t)
end

function g_BuddyMsgMgr.GetBuddyMsgInfo(nQQUin)
	local t=g_BuddyMsgMgr[nQQUin]
	if t==nil then
		t = {}
		g_BuddyMsgMgr[nQQUin] = t;
	end
	return t
end

function g_BuddyMsgMgr.ExistBuddy(nQQUin)
	local t = g_BuddyMsgMgr[nQQUin]
	local e = t~=nil
	if e == false then
		return e, false
	end
	
	local f = t[1]~=nil and t[2]~=nil
	return e, f
end

function g_BuddyMsgMgr.GetAutoAnswer(szText)
	local szAnswer = g_DictMgr.GetAnswerWords(szText)
	if szAnswer~=nil and string.len(szAnswer)>0 then
		return szAnswer
	end
	
	szAnswer = g_AIML.GetWords(szText)
	if szAnswer~=nil and string.len(szAnswer)>0 then
		return szAnswer
	end
	
	return nil
end

function g_BuddyMsgMgr.HandleMessage(t)
	local nQQUin = t[1]
	local szTime = t[2]
	local szText = t[3]
	local o = g_BuddyMsgMgr.GetBuddyMsgInfo(nQQUin)
	print(string.format("HandleMessage: %s (%u)", o[2], o[1]))
	g_ChatLogDB.AddMessageDB(o[1], o[2], g_BuddyMsgMgr.UserInfo[1], g_BuddyMsgMgr.UserInfo[2], szTime, szText)
	
	local szAnswer = g_BuddyMsgMgr.GetAutoAnswer(szText)
	if szAnswer==nil or string.len(szAnswer)==0 then
		local szTextNew = getIctclas(szText)
		if string.len(szTextNew)>0 then
			szAnswer = g_BuddyMsgMgr.GetAutoAnswer(szTextNew)
		end
	end
	
	if szAnswer==nil or string.len(szAnswer)==0 then
		szAnswer=g_DefaultMessage
	end
	
	sendMsg(0, nQQUin, szAnswer)
	g_ChatLogDB.AddMessageDB(g_BuddyMsgMgr.UserInfo[1], g_BuddyMsgMgr.UserInfo[2], o[1], o[2], getCurrentTime(), szAnswer)
end

function g_BuddyMsgMgr.RunMessages()
	local co = moc.createCo(function ()
		while true do
			local t = {}
			for _,v in ipairs(g_BuddyMsgMgr.Messages) do
				local e, f = g_BuddyMsgMgr.ExistBuddy(v[1])
				if f then
					g_BuddyMsgMgr.HandleMessage(v)
				else
					table.insert(t, v)
				end
			end
			g_BuddyMsgMgr.Messages = t
			
			moc.sleep(100)
		end
	end)
end


function OnUpdateBuddyNumber(nQQUin, nQQNum)
	print(string.format("OnUpdateBuddyNumber: %u, %u", nQQUin, nQQNum))
	
	local t=g_BuddyMsgMgr.GetBuddyMsgInfo(nQQUin)
	t [1] = nQQNum
end


function OnUpdateBuddyNickName(nQQUin, nickName)
	print(string.format("OnUpdateBuddyNickName: %u, %s", nQQUin, nickName))
	
	local t=g_BuddyMsgMgr.GetBuddyMsgInfo(nQQUin)
	t [2] = nickName
end


function OnUpdateBuddyInfo(nQQUin, nickName, nQQNum)
	print(string.format("OnUpdateBuddyInfo: %u, %s, %u", nQQUin, nickName, nQQNum))
	g_BuddyMsgMgr[nQQUin] = {nQQNum, nickName}
	
	if nQQUin == nQQNum then
		g_BuddyMsgMgr.UserInfo[1] = nQQNum
		g_BuddyMsgMgr.UserInfo[2] = nickName
	end
end


function OnRecvBudyMsg(nQQUin, szTime, szText)
	print(string.format("OnRecvBudyMsg: %u, %s, %s", nQQUin, szTime, szText))
	
	local e, _=g_BuddyMsgMgr.ExistBuddy(nQQUin)
	if e==false then
		updateStrangerInfo(nQQUin)
	end
	g_BuddyMsgMgr.AddMsg(nQQUin, szTime, szText)
end


function OnRecvSessMsg(nGroupId, nQQUin, szTime, szText)
	print(string.format("OnRecvSessMsg: %u, %u, %s, %s", nGroupId, nQQUin, szTime, szText))
end


g_BuddyMsgMgr.RunMessages()


