--[[
UI ChatFrame:AddChannel(chatFrame, "channelName") - Activate channel in chatFrame. 
UI ChatFrame:AddMessageEventFilter("event", filterFunc) - Add a chat message filtering function (added 2.4) 
UI ChatFrame:GetMessageEventFilters("event") - Retreive the list of chat message filtering functions. (added 2.4) 
UI ChatFrame:OnHyperlinkShow(reference, link, button) - called when the user clicks on a chatlink. 
UI ChatFrame:RemoveMessageEventFilter("event", filterFunc) - Unregister a chat message filtering function (added 2.4) 
]]

--[[
/run ChatFilter.Words[#ChatFilter.Words + 1] = "roar"
/dump ChatFilter.Words
/dump ChatFilter.RemovedLines
 
/fr
/fa
/fu
--]]

print("POGGIES")

SLASH_FADD1 = "/fadd"
SLASH_FADD2 = "/fa"
SlashCmdList["FADD"] = add

SLASH_FREMOVE1 = "/fremove"
SLASH_FREMOVE2 = "/fr"
SlashCmdList["FREMOVE"] = cleanse

SLASH_FUPDATE1 = "/fupdate"
SLASH_FUPDATE2 = "/fu"
SlashCmdList["FUPDATE"] = function(msg)
	cleanse()
	add()
end

local chatEvents = {
	"CHAT_MSG_SAY",
	"CHAT_MSG_RAID",
	"CHAT_MSG_YELL",
	"CHAT_MSG_PARTY",
	"CHAT_MSG_GUILD",
	"CHAT_MSG_WHISPER",
	"CHAT_MSG_CHANNEL",
}

if not ChatFilter then ChatFilter = {} end
if not ChatFilter.People then ChatFilter.People = {} end
if not ChatFilter.Words then ChatFilter.Words = {} end
if not ChatFilter.RemovedLines then ChatFilter.RemovedLines = {} end
if not ChatFilter.RemovedCommon then ChatFilter.RemovedCommon = {} end

--Own filter
local function myChatFilter(self, e, msg, author, lang)
	print(msg, author, lang)

	local function getDate()
		local date = date()
		local year = date:match("%d%d%/%d%d/(%d%d)") + 2000 --01/07/(19) 14:36:42
		local month = date:match("(%d%d)") --(01)/07/19 14:36:42
		local day = date:match("%d%d%/(%d%d)") --01/(07)/19 14:36:42
		--Remove first place zeros
		if day:find("0") == 1 then day = day:gsub("0", "") end
		if month:find("0") == 1 then month = month:gsub("0", "") end
		local localdate = day .. "." .. month .. "." .. year
		return localdate
	end

	local function getTime()
		local date = date()
		local localtime = date:match("%d%d%/%d%d%/%d%d%s(%d%d%p%d%d%p%d%d)")
		return localtime
	end

	author = author:match("(.+)-")

	--Remove Alliance
	if lang == "Common" then
		-- print("Caught one!")
		ChatFilter.RemovedCommon[#ChatFilter.RemovedCommon + 1] =
			format("[%s][%s]%s: %s", getDate(), getTime(), author, msg)
		return true
	end

	--Remove People
	for k, v in pairs(ChatFilter.People) do
		if author == v then
			if e == "CHAT_MSG_WHISPER" and author ~= UnitName("player") then
				SendChatMessage(
					format('Your message "%s" has been filtered out because I hate fun', msg),
					"WHISPER",
					nil,
					author
				)
			end
			ChatFilter.RemovedLines[#ChatFilter.RemovedLines + 1] =
				format("[%s][%s]%s: %s", getDate(), getTime(), author, msg)
			-- print("Caught one!")
			return true
		end
	end

	--Remove Words
	for k, v in pairs(ChatFilter.Words) do
		if msg:match(v) then
			if e == "CHAT_MSG_WHISPER" and author ~= UnitName("player") then
				SendChatMessage(
					format('Your message "%s" has been filtered out because I hate fun', msg),
					"WHISPER",
					nil,
					author
				)
			end
			ChatFilter.RemovedLines[#ChatFilter.RemovedLines + 1] =
				format("[%s][%s]%s: %s", getDate(), getTime(), author, msg)
			-- print("Caught one!")
			return true
		end
	end
end

--Remove all filters
local function cleanse()
	for k, v in pairs(chatEvents) do
		if ChatFrame_GetMessageEventFilters(v) then
			print(v, unpack(ChatFrame_GetMessageEventFilters(v)))
			for k2, v2 in pairs(ChatFrame_GetMessageEventFilters(v)) do
				ChatFrame_RemoveMessageEventFilter(v, v2)
			end
		end
		print("Done?")
		if ChatFrame_GetMessageEventFilters(v) then print(unpack(ChatFrame_GetMessageEventFilters(v))) end
		print("Got it.")
	end
end

--Add filter
local function add()
	for k, v in pairs(chatEvents) do
		print("Adding filter to", v)
		ChatFrame_AddMessageEventFilter(v, myChatFilter)
	end
end
