--[[----------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2014 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

----------------------------------------------------------------------------]]--

local Rx = require "Rx"

	-- WF.lrmobile
local LrmCatCollaboration = require 'LrmCatCollaboration'

--==============================================================================

local LrmSocialActivityFeedModel = {}

local Submodels = {}

--------------------------------------------------------------------------------

local function mapCommentsAndFavorites( comments, favorites, user )
	
	local result = {
		numComments = #comments,
		numFavorites = #favorites,
	}
	
	local activities = {}
	
	local favAuthor = {}
	
	for _, favorite in ipairs( favorites ) do
	
		local authorId = favorite.author.id
		favorite[ 'isMyActivity' ] = authorId == user:userId()
		activities[ #activities + 1 ] = favorite
		
		local favKey = string.format( '%s%s%s', favorite.assetId, favorite.spaceId, authorId )
		favAuthor[ favKey ] = true
	end
	
	for _, comment in ipairs( comments ) do
	
		local authorId = comment.author.id
		
		local isFavorited = false
		local favKey = string.format( '%s%s%s', comment.assetId, comment.spaceId, authorId )
		
		if favAuthor[ favKey ] then
			isFavorited = true
		end
		
		comment[ 'IsCommentFavorited' ] = isFavorited
		comment[ 'isMyActivity' ] = authorId == user:userId()
		activities[ #activities + 1 ] = comment
		
	end
	
	table.sort( activities, function( a, b ) return a.created > b.created end )
	
	result[ 'activities' ] = activities
	return result
	
end

--------------------------------------------------------------------------------

function Submodels:isMyFavoriteModel( assetId, albumId, userId )

	local isMyFavorite = LrmCatCollaboration.isMySpaceFavorite( self.wfCatalog, assetId, albumId, userId )
		:filter( function( isMyFavorite ) return isMyFavorite ~= nil end )
		
	return {
		value = isMyFavorite
	}
	
end

--------------------------------------------------------------------------------

function LrmSocialActivityFeedModel.makeSocialActivityModel( wfCatalog, activityContext,
															albumId, assetId )

	local socialFeed
	
	if activityContext == 'catalog' then
	
		socialFeed = Rx.Observable.combineLatest(
						mapCommentsAndFavorites,
						LrmCatCollaboration.getComments( wfCatalog ),
						LrmCatCollaboration.getSpaceFavorites( wfCatalog ),
						Rx.Observable.just( wfCatalog:ozUser() ) )

	elseif activityContext == 'album' then
	
		socialFeed = Rx.Observable.combineLatest(
						mapCommentsAndFavorites,
						LrmCatCollaboration.getCommentsForAlbum( wfCatalog, albumId ),
						LrmCatCollaboration.getSpaceFavoritesOnAlbum( wfCatalog, albumId ),
						Rx.Observable.just( wfCatalog:ozUser() ) )
	
	elseif activityContext == 'asset' then
	
		socialFeed = Rx.Observable.combineLatest(
						mapCommentsAndFavorites,
						LrmCatCollaboration.getCommentsOnAlbumAsset( wfCatalog, albumId, assetId ),
						LrmCatCollaboration.getSpaceFavoritesOnAlbumAsset( wfCatalog, albumId, assetId ),
						Rx.Observable.just( wfCatalog:ozUser() ) )
	else
	
		socialFeed = Rx.Observable.never()
		
	end
	
	return {
		wfCatalog = wfCatalog,
		
		value = socialFeed,
		
		models = Submodels,
		
	}
	
end

--------------------------------------------------------------------------------

return LrmSocialActivityFeedModel
