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

function sms.module_type ()
	return "sms"
end

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

function sms.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 sms.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 sms.getDisplayName (interaction)
	return interaction.interactionData and interaction.interactionData.contactName or ""
end

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

	interaction.properties = interaction.properties or {}

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

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

---
-- Takes an interaction and returns a block of text to be sent to the search engine
--
function sms.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 sms.calculateConversationTotalCharacters (interaction)
	assert(interaction)
	local messages = interaction.events or {}
	return common.calculateConversationTotalCharactersCommonChats(messages, interaction)
end

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

return sms