local common = require "interactionmanip.common"
local cjson = require "cjson"
local utf8 = require "icu.utf8"
local rcs = {}

function rcs.module_type ()
	return "rcs"
end

local function getMessageText(message)
	return message.text or ""
end

function rcs.getExtendedInfo (interaction)

	if not interaction.events or not interaction.events[1] then
		return ""
	end

	local event = interaction.events[1]

	local message = getMessageText(event.message)
	if type(message) == "string" then
		return utf8.sub(message, 1, 200)
	end

	return ""
end

---
-- returns the conversation contact or member display text
--
function rcs.getUserName (interaction, index)
	assert(type(index) == "number")

	if not interaction.events or not interaction.events[index] then
		return ""
	end

	local event = interaction.events[index]
	local agent_id = event.agent
	if not agent_id then
		--contact name
		if event.from and event.from.name and event.from.name ~= "" then
			return "text:"..event.from.name
		--address
		elseif event.address and event.address.Number and event.address.Number ~= "" then
			return "phone:"..event.address.Number
		--hidden address
		else
			return ""
		end
	else
		return "agent_id:"..agent_id
	end
end

---
-- Returns the first user name from the events array
--
function rcs.getDisplayName (interaction)
	return interaction.interactionData and interaction.interactionData.contactName or ""
end

---
-- Takes an interaction and precomputes properties based on events
--
function rcs.updateInteraction (interaction)

	interaction.properties = interaction.properties or {}

	rcs.updateReaders(interaction)
	rcs.updateLabels(interaction)
	rcs.isTransferred(interaction)
	rcs.getLastAgentEndsConversation(interaction)
	rcs.getLastStep(interaction)

	interaction.properties.display_name = rcs.getDisplayName(interaction)
	interaction.properties.summary = rcs.getExtendedInfo(interaction)
	interaction.properties.event_count = rcs.getEventsCountWithoutAutoResponse(interaction)
	interaction.properties.unread_event_count = rcs.getUnreadEventCount(interaction)
	interaction.properties.unanswered_event_count = rcs.getUnAnsweredContactEventCount(interaction)
	return interaction
end

---
-- Takes an interaction and returns a block of text to be sent to the search engine
--
function rcs.getSearchableContent (interaction)
	if not interaction.events then return "" end
	local data = {}

	for _, event in ipairs(interaction.events) do
		table.insert(data, getMessageText(event.message))
	end

	if interaction.comments then
		for _, comment in pairs(interaction.comments) do
			table.insert(data, comment.comment)
			table.insert(data, comment.authorName)
		end
	end

	return table.concat(data, " ")
end

function rcs.calculateConversationTotalCharacters (interaction)
	assert(interaction)
	local messages = interaction.events or {}
	return common.calculateConversationTotalCharactersCommonChats(messages, interaction)
end

-----------------------------------------------------------------
rcs.isTransferred                        = common.isTransferred
rcs.isTaken                              = common.isTaken
rcs.isWrappedUp                          = common.isWrappedUp
rcs.isFinished                           = common.isFinished
rcs.getConversationOwner                 = common.getConversationOwner
rcs.getFirstAssignedAgent                = common.getFirstAssignedAgent
rcs.getLastAgentEndsConversation         = common.getLastAgentEndsConversation
rcs.getLastEventTimestamp                = common.getLastEventTimestamp
rcs.updateReaders                        = common.updateReaders
rcs.updateLabels                         = common.updateLabels
rcs.getLastStep                          = common.getLastStep
rcs.isAutoResponseEvent                  = common.isAutoResponseEvent
rcs.getUnreadEventCount                  = common.getUnreadEventCount
rcs.getUnAnsweredContactEventCount		 = common.getUnAnsweredContactEventCount
rcs.getEventsCountWithoutAutoResponse    = common.getEventsCountWithoutAutoResponse
rcs.getAllEventsCount                    = common.getAllEventsCount
rcs.newInteractionEvent                  = common.newInteractionEvent
rcs.newInteractionEvents                 = common.newInteractionEvents
rcs.getSocialNetworkPostId               = common.getSocialNetworkPostId
rcs.getFirstTransferringInfo             = common.getFirstTransferringInfo
rcs.getBatchId                           = common.getBatchId
rcs.loadFirstPendingStep                 = common.loadFirstPendingStep
rcs.loadStatusInteraction                = common.loadStatusInteraction
rcs.loadFirstTakenStep                   = common.loadFirstTakenStep
rcs.loadStartAttention                   = common.loadStartAttention
rcs.loadTicket                           = common.loadTicket
rcs.loadFirstAgentResponse               = common.loadFirstAgentResponse
rcs.loadLastAgentResponse                = common.loadLastAgentResponse
rcs.getConversationTotals                = common.getConversationTotals
rcs.calculateConversationTimeSec         = rcs.calculateConversationTimeSec or common.calculateConversationTimeSec
rcs.calculateConversationTotalCharacters = rcs.calculateConversationTotalCharacters or common.calculateConversationTotalCharacters

return rcs