local agentService = require "lua_agentserviceclient"
module(..., package.seeall)
local m_utils = require "helpers.utils"
local m_agentclient

function GetAgentClient (options)
	if not m_agentclient then
		assert(options)
		assert(type(options) == "table")
		assert(options.host)
		assert(options.port)
		assert(options.cluster)
		m_agentclient = agentService.LuaAgentServiceClient(options.cluster)
		m_agentclient:AddKnownAgentService(options.host, options.port)
	end
	return m_agentclient
end

--[[
 
]]--
function GetVccLicense (options, vcc)
	--
	--	GetAgentClient():GetVccLicense returns a table with
	--		Channels whose value is boolean (Ex: FACEBOOK: false)
	--		On line Users configured on vcc (Ex: ONLINE_USERS: 31)
	--		Max available on line users for vcc, including ONLINE_USERS (Ex: MAX_ONLINE_USERS: 600)
	--		External apps integration (Ex: {HelpDesk:10})
	--		Available users for external apps integartions (Ex: MAX_AppsIntegrations:{HelpDesk:10})
	--		Limits and quotas (Ex: CustomMetricsLimit: 0, CustomFiltersLimit: 0, ExportSpaceQuota: 0, SharedFilesSpaceQuota: 0)
	--		Available Limits and quotas (Ex: MAX_CustomMetricsLimit: 0, MAX_CustomFiltersLimit: 0, MAX_ExportSpaceQuota: 0, MAX_SharedFilesSpaceQuota: 0)
	--
	return m_utils:HasLicenseSystem() and vcc and GetAgentClient(options):GetVccLicense(vcc) or {}
end

--[[
 
]]--
function GetAvailableLicenseResources (options, vcc)
	--
	--	GetAgentClient():GetAvailableLicenseResources returns a table with
	--		Channels whose value is boolean (Ex: FACEBOOK: false)
	--		Max available on line users for vcc, including ONLINE_USERS (Ex: MAX_ONLINE_USERS: 600)
	--		Available users for external apps integartions (Ex: MAX_AppsIntegrations:{HelpDesk:10})
	--		Available Limits and quotas (Ex: MAX_CustomMetricsLimit: 0, MAX_CustomFiltersLimit: 0, MAX_ExportSpaceQuota: 0, MAX_SharedFilesSpaceQuota: 0)
	--
	return m_utils:HasLicenseSystem() and vcc and GetAgentClient(options):GetAvailableLicenseResources(vcc) or {}
end

--[[
 
]]--
function CompanyIsEditable (options)
	--
	--	GetAgentClient():CompanyIsEditable returns a table with an attribute company_is_editable which has a value of true or false
	--
	return m_utils:HasLicenseSystem() and GetAgentClient(options):CompanyIsEditable() or {}
end

--[[
 
]]--
function CompanyIsRequired (options)
	--
	--	GetAgentClient():CompanyIsEditable returns a table with an attribute company_is_editable which has a value of true or false
	--
	return m_utils:HasLicenseSystem() and GetAgentClient(options):CompanyIsRequired() or {}
end

--[[
 
]]--
function ValidateCompany (options, company)
	--
	--	GetAgentClient():ValidateCompany returns a table withan attribute company_is_valid which has a value of true or false
	--
	return m_utils:HasLicenseSystem() and company and GetAgentClient(options):ValidateCompany(company) or {}
end

--[[
 
]]--
function ValidateWhatsappActivationCode (options, code)
	--
	--	GetAgentClient():ValidateWhatsappActivationCode returns a table withan attribute code_is_valid which has a value of true or false
	--
	return m_utils:HasLicenseSystem() and code and GetAgentClient(options):ValidateWhatsappActivationCode(code) or {}
end

--[[
 
]]--
function EnableWhatsappActivationCode (options, code, vcc, account, user)
	--
	--	GetAgentClient():EnableWhatsappActivationCode returns a table withan attribute success which has a value of true or false
	--
	return m_utils:HasLicenseSystem() and code and GetAgentClient(options):EnableWhatsappActivationCode(code, vcc, account, user) or {}
end

--[[
 
]]--
function SetVccLicense (options, vcc, vccData)
	--
	--	GetAgentClient():SetVccLicense returns a table with
	--		success: true | false
	--		error: when success false, a string associated error 
	--

	return m_utils:HasLicenseSystem() and vcc and vccData and GetAgentClient(options):SetVccLicense(vcc, vccData) or {}
end

--[[
 
]]--
function GetChannelLicenses (options, vcc)
	--
	--	Here we are just interested on channels, so we apply a filter on GetVccLicense returned table based on a type of each value
	--

	local channelLicenses = {}
	if m_utils:HasLicenseSystem() then
		local vccLicense = GetVccLicense(options, vcc)
		for name, value in pairs(vccLicense) do
			if type(value) == "boolean" then
				channelLicenses[name] = value
			end 
		end
	end
	return channelLicenses
end

-- Add licensed attributes to vccInfo
function GetVccWithLicenseInfo(options, vccInfo)
	local vccInfoWithLicense = vccInfo or {}
	if m_utils:HasLicenseSystem() then
		local vccLicense = GetVccLicense(options, vccInfo.Id)

		local licensedAttr = {
			OnlineUsers = true,
			MAX_AppsIntegrations = true,
			CustomMetricsLimit = true,
			MAX_CustomMetricsLimit = true,
			CustomFiltersLimit = true,
			MAX_CustomFiltersLimit = true,
			ExportSpaceQuota = true,
			MAX_ExportSpaceQuota = true,
			SharedFilesSpaceQuota = true,
			MAX_SharedFilesSpaceQuota = true,
			company = true
		}
		-- Gets vccInfo attributes except those included in license
		for name, value in pairs(vccInfo) do
			if not licensedAttr[name] and not string.find(name, "_licensed") then
				vccInfoWithLicense[name] = value
			end
		end
		-- Add license info
		for name, value in pairs(vccLicense) do
			if name == "ONLINE_USERS" then
				vccInfoWithLicense["OnlineUsers"] = value
			elseif name == "MAX_ONLINE_USERS" then
				vccInfoWithLicense["MaxOnlineUsers"] = value
			elseif name == "AppsIntegrations" then
				--Set AppsIntegrations[app].Licenses.Users for every App with vccLicense.AppsIntegrations[app]
				if vccInfoWithLicense.AppsIntegrations then
					if type(vccInfoWithLicense.AppsIntegrations) == "string" then
						vccInfoWithLicense.AppsIntegrations = json.decode(vccInfoWithLicense.AppsIntegrations)
					end
					for appId, appConfig in pairs(vccInfoWithLicense.AppsIntegrations) do
						if type(value) == "string" then
							value = json.decode(value)
						end
						appConfig.Licenses = appConfig.Licenses or {}
						appConfig.Licenses.Users = value and value[appId] or "0"
					end
				end
			elseif name == "company_is_editable" then
				vccInfoWithLicense["company_is_editable"] = type(value) == "boolean" and value or false
			elseif type(value) == "boolean" then
				vccInfoWithLicense[name .. "_licensed"] = type(value) == "boolean" and value
			elseif licensedAttr[name] then
				vccInfoWithLicense[name] = value
			end
		end
	end
	return vccInfoWithLicense
end
