--[[
Author: Puddy
Filename: libChat.lua
Date: 2014-4-10
Version: 1.0.0

BSD 2-clause license

Copyright (c) 2014, Puddy
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
]]--

local MAJOR, MINOR = "libChat-1.0", 2
--noinspection UnusedDef
local libchat, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
if not libchat then
	return
end

local funcName = nil
local funcText = nil
local funcFormat = nil

-- Listens for EVENT_CHAT_MESSAGE_CHANNEL event from ZO_ChatSystem
local function libChatReceiver(channelID, from, text)
	local message = ""

	-- Get channel information
	local ChanInfoArray = ZO_ChatSystem_GetChannelInfo()
	local info = ChanInfoArray[channelID]

	if not info or not info.format then
		return
	end

	-- Function to affect name
	if funcName then
		from = funcName(channelID, from, text)
		if not from then return end
	end

	-- Function to affect text message
	if funcText then
		text = funcText(channelID, from, text)
		if not text then return end
	end

	-- Function to format message
	if funcFormat then
		message = funcFormat(channelID, from, text)
		if not message then return end
	else
		-- No formatting addon, so do default stuff.

		-- Create channel link
		local channelLink
		if info.channelLinkable then
			local channelName = GetChannelName(info.id)
			channelLink = ZO_LinkHandler_CreateChannelLink(channelName)
		end

		-- Create player link
		local playerLink
		if info.playerLinkable and not from:find("%[") then
            --noinspection UnusedDef
            playerLink = ZO_LinkHandler_CreatePlayerLink(from)
		else
			playerLink = from
		end

		-- Create default formatting
		if channelLink then
            --noinspection UnusedDef
            message = zo_strformat(info.format, channelLink, playerLink, text)
		else
			message = zo_strformat(info.format, playerLink, text)
		end
	end

	return message, info.saveTarget
end

-- Register a function to be called to modify sender name
function libchat:registerName(func)
	if not funcName then
		funcName = func
	end
end

-- Register a function to be called to modify message text
function libchat:registerText(func)
	if not funcText then
		funcText = func
	end
end

-- Register a function to be called to format message
function libchat:registerFormat(func)
	if not funcFormat then
		funcFormat = func
	end
end

ZO_ChatSystem_AddEventHandler(EVENT_CHAT_MESSAGE_CHANNEL, libChatReceiver)