local m_interactionmanip = require "interactionmanip"
local m_common = require "interactionmanip.common"

module(..., package.seeall)

interaction = {}

--[[ *****************************************************************************
	GENERIC (ALL Interactions Types)
**********************************************************************************]]--

-- Retrieves a full interaction JSON from redis
interaction.Get = function(interactionId, redis)
	assert(type(interactionId) == "string")
	assert(redis)
	local encodedInteraction = redis:get("InteractionDetail:" .. interactionId)
	if not encodedInteraction or encodedInteraction == "" then
		return
	end
	local success, decodedInteraction = pcall(json.decode, encodedInteraction)
	if success and decodedInteraction then
		return decodedInteraction
	else
		LogError("[REPORTER.redis_values:Get] Interaction '%s' detail is undecodable: '%s'", interactionId, encodedInteraction)
	end
end

interaction.Type = function(conversation)
	if not conversation then
		return
	end
	return conversation.type
end

interaction.State = function(conversation)
	if not conversation then
		return
	end
	return conversation.state
end

interaction.FirstPendingDate = function(conversation)
	local manipModule = m_common.require_module(conversation)
	if not manipModule then
		return
	end
	manipModule.loadFirstPendingStep(conversation)
	if not conversation.__firstPendingStep then
		return
	end
	return conversation.__firstPendingStep.timestampDouble
end

interaction.StatusInteraction = function(conversation)
	local manipModule = m_common.require_module(conversation)
	if not manipModule then
		return
	end
	manipModule.loadStatusInteraction(conversation)
	return conversation.__statusInteraction
end

interaction.FirstTakenDate = function(conversation)
	if conversation.initiative == "OUTBOUND" then
	-- Cuando es una Interacción Saliente, no aplica el visualizar la primera fecha de Atendida
		return
	end
	local manipModule = m_common.require_module(conversation)
	if not manipModule then
		return
	end
	manipModule.loadFirstTakenStep(conversation)
	if not conversation.__firstTakenStep then
		return
	end
	return conversation.__firstTakenStep.timestampDouble
end

interaction.StartAttention = function(conversation)
	local manipModule = m_common.require_module(conversation)
	if not manipModule then
		return
	end
	manipModule.loadStartAttention(conversation)
	if not conversation.__startAttention then
		return
	end
	return conversation.__startAttention
end

interaction.Ticket = function(conversation)
	local manipModule = m_common.require_module(conversation)
	if not manipModule then
		return
	end
	manipModule.loadTicket(conversation)
	return conversation.__ticket
end

interaction.FirstAgentResponse = function(conversation)
	local manipModule = m_common.require_module(conversation)
	if not manipModule then
		return
	end
	manipModule.loadFirstAgentResponse(conversation)
	return conversation.__firstAgentResponse
end

interaction.LastAgentResponse = function(conversation)
	local manipModule = m_common.require_module(conversation)
	if not manipModule then
		return
	end
	manipModule.loadLastAgentResponse(conversation)
	return conversation.__lastAgentResponse
end

--[[ *****************************************************************************
	MAIL
**********************************************************************************]]--
interaction.FirstMailFrom = function(conversation)
	local conversationType = assert(conversation.type):upper()
	if conversationType ~= "MAIL" then
		return
	end
	local manipModule = m_common.require_module(conversation)
	if not manipModule then
		return
	end
	manipModule.loadDisplayFirstMailFrom(conversation)
	return conversation.__displayFirstMailFrom
end

interaction.FirstMailTo = function(conversation)
	local conversationType = assert(conversation.type):upper()
	if conversationType ~= "MAIL" then
		return
	end
	local manipModule = m_common.require_module(conversation)
	if not manipModule then
		return
	end
	manipModule.loadDisplayFirstMailTo(conversation)
	return conversation.__displayFirstMailTo
end

interaction.FirstMailSubject = function(conversation)
	local conversationType = assert(conversation.type):upper()
	if conversationType ~= "MAIL" then
		return
	end
	local manipModule = m_common.require_module(conversation)
	if not manipModule then
		return
	end
	manipModule.loadFirstMailSubject(conversation)
	return conversation.__firstMailSubject
end

interaction.MailAttachCount = function(conversation)
	local conversationType = assert(conversation.type):upper()
	if conversationType ~= "MAIL" then
		return
	end
	local manipModule = m_common.require_module(conversation)
	if not manipModule then
		return
	end
	manipModule.loadFirstMailAttachmentsCount(conversation)
	return conversation.__firstMailAttachmentsCount
end

--[[ *****************************************************************************
	FACEBOOK, INSTAGRAM, YOUTUBE, TWITTER, APPSTORE, GOOGLEPLAY
**********************************************************************************]]--
interaction.SubType = function(conversation)
	local allowedConversationsTypes = {
		APPSTORE   = true,
		FACEBOOK   = true,
		GOOGLEPLAY = true,
		INSTAGRAM  = true,
		TWITTER    = true,
		YOUTUBE    = true
	}
	local conversationType = assert(conversation.type):upper()
	if not allowedConversationsTypes[conversationType] then
		return
	end
	local manipModule = m_common.require_module(conversation)
	if not manipModule then
		return
	end
	manipModule.loadSubType(conversation)
	return conversation.__displaySubType
end

--[[ *****************************************************************************
	FACEBOOK, INSTAGRAM
**********************************************************************************]]--
interaction.Page = function(conversation)
	local allowedConversationsTypes = {
		FACEBOOK  = true,
		INSTAGRAM = true
	}
	local conversationType = assert(conversation.type):upper()
	if not allowedConversationsTypes[conversationType] then
		return
	end
	local manipModule = m_common.require_module(conversation)
	if not manipModule then
		return
	end
	manipModule.loadPage(conversation)
	return conversation.__displayPage
end

