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

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.core.utils
local KSTableUtils = require 'KSTableUtils'
local WFRxRunUntilDone = require "WFRxRunUntilDone"

	-- WF.core.os
local KSDate = require 'KSDate'

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

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


local LrmSocialActivityStatusModel = {}

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

local function updateLastReadTimestamp( self )

	local now = KSDate.timeInIso8601Format( nil, false )
	
	return self.wfCatalog:docStore():setLocalDocValue {
			_id = 'lastReadSocialActivityTimestamp',
			value = now,
		}
		:call( WFRxRunUntilDone )

end

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

local function hasNewSocialActivity( newComments, newFavorites )
	
	
	if ( newComments and #newComments > 0 ) or ( newFavorites and #newFavorites > 0 ) then
		return true
	end
	
	return false
	
end

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

local function getFavorites( wfCatalog, lastReadTimestamp, context, albumId, assetId )

	local indexStore = wfCatalog:indexStore()
	
	if context == 'catalog' then
	
		return indexStore:query{
				sql = [[SELECT docId FROM spacefavorite
						WHERE datetime( created ) > datetime( ? ) LIMIT 1]],
				calm = 0.25,
				lastReadTimestamp }
			
	elseif context == 'album' then
		
		return indexStore:query{
			sql = [[SELECT
						spacefavorite.docId
					FROM
						spacefavorite
					LEFT JOIN
						 album_asset
					WHERE
						spacefavorite.assetId == album_asset.assetId AND
						datetime( created ) > datetime( ? ) AND
						album_asset.albumId == ? LIMIT 1]],
			calm = 0.25,
			lastReadTimestamp, albumId }
			
	else
	
		return indexStore:query{
			sql = [[SELECT
						docId
					FROM
						spacefavorite
					WHERE
						datetime( created ) > datetime( ? ) AND
						spacefavorite.assetId == ? LIMIT 1]],
			calm = 0.25,
			lastReadTimestamp, assetId }
			
	end
		
end

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

local function getComments( wfCatalog, lastReadTimestamp, context, albumId, assetId )

	local indexStore = wfCatalog:indexStore()
	
	if context == 'catalog' then
	
		return indexStore:query{
				sql = [[SELECT docId FROM comment
						WHERE datetime( created ) > datetime( ? ) LIMIT 1]],
				calm = 0.25,
				lastReadTimestamp }
			
	elseif context == 'album' then
	
		return indexStore:query{
				sql = [[SELECT
							comment.docId
						FROM
							comment
						LEFT JOIN
							 album_asset
						WHERE
							comment.assetId == album_asset.assetId AND
							datetime( created ) > datetime( ? ) AND
							album_asset.albumId == ? LIMIT 1]],
			calm = 0.25,
			lastReadTimestamp, albumId }
			
	else
	
		return indexStore:query{
			sql = [[SELECT
						docId
					FROM
						comment
					WHERE
						datetime( created ) > datetime( ? ) AND
						comment.assetId == ? LIMIT 1]],
			calm = 0.25,
			lastReadTimestamp, assetId }
			
	end
	
end

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

local function hasNewActivity( wfCatalog, context, albumId, assetId )

	return wfCatalog:docStore():ongoingLocalDocValue( 'lastReadSocialActivityTimestamp' )
		:map( function( activityTimestamp )

			if not activityTimestamp._empty then

				local now = KSDate.timeInIso8601Format( nil, false )

				return KSTableUtils.derefOpt( activityTimestamp, 'value' ) or now

			end

			return nil

		end )
		:flatMap( function( lastReadTimestamp )

			if lastReadTimestamp then

				local hasNewComments = getComments( wfCatalog, lastReadTimestamp,
					context, albumId, assetId )

				local hasNewFavorites = getFavorites( wfCatalog, lastReadTimestamp,
					context, albumId, assetId )

				return Rx.Observable.combineLatest( hasNewSocialActivity, hasNewComments, hasNewFavorites )

			end

			return Rx.Observable.just( {} )

		end )
		
end

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

local function supportsSocialActivity( hasComments, hasFavorites )
	
	if hasComments or hasFavorites then
		return true
	end
	
	return false
	
end

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

local function supportsActivity( wfCatalog, context, albumId, assetId )

	if context == 'asset' then
	
		return LrmAlbumUtils.isAlbumShared( wfCatalog:indexStore(), albumId )
		
	elseif context == 'album' then
	
		return Rx.Observable.combineLatest( supportsSocialActivity,
											LrmCatCollaboration.hasCommentsInAlbum( wfCatalog, albumId ),
											LrmCatCollaboration.hasSpaceFavoritesInAlbum( wfCatalog, albumId ) )
	elseif context == 'catalog' then
	
		return LrmAlbumUtils.hasAnySharedAlbums( wfCatalog:indexStore() )
		
	end
	
end
	
--------------------------------------------------------------------------------

function LrmSocialActivityStatusModel.makeSocialActivityStatusModel( wfCatalog, context, albumId, assetId  )

			
	return {
		wfCatalog = wfCatalog,
		
		value = {
		
			hasNewActivity = hasNewActivity( wfCatalog, context, albumId, assetId )
								:filter( function( val ) return val ~= nil end ),
								
			supportsActivity = supportsActivity( wfCatalog, context, albumId, assetId )
								:filter( function( val ) return val ~= nil end ),
			
		},
		
		events = {
		
			updateLastReadTimestamp = updateLastReadTimestamp
			
		}
	}
	
end

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

return LrmSocialActivityStatusModel
