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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 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 LrmLibrarySocialModelImp = {}

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

function LrmLibrarySocialModelImp:addComment( assetId, comment, albumId, opts )

	local addComment = LrmCatCollaboration.addComment
	if opts and opts.TEST_mock then
		addComment = opts.TEST_mock
	end

	local result = addComment( self._wfCatalog, albumId, assetId, comment )
		:map( function( data )
			return { state = true }
		end )
		:catch( function( data )
			return Rx.Observable.just( { state = false, error = data } )
		end )

	return {
		value = result,
	}

end

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

function LrmLibrarySocialModelImp:deleteComment( commentId, opts )

	local deleteComment = function()
		return self._wfCatalog:docStore():deleteDoc( commentId )
	end

	if opts and opts.TEST_mock then
		deleteComment = opts.TEST_mock
	end

	local result = deleteComment()
		:map( function( data )
			return { state = true }
		end )
		:catch( function( data )
			return Rx.Observable.just( { state = false, error = data } )
		end )

	return {
		value = result,
	}

end

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

function LrmLibrarySocialModelImp:deleteSpaceFavorite( spacefavoriteId, opts )

	local deleteSpaceFavorite = function()
		return self._wfCatalog:docStore():deleteDoc( spacefavoriteId )
	end

	if opts and opts.TEST_mock then
		deleteSpaceFavorite = opts.TEST_mock
	end

	local result = deleteSpaceFavorite()
		:map( function( data )
			return { state = true }
		end )
		:catch( function( data )
			return Rx.Observable.just( { state = false, error = data } )
		end )

	return {
		value = result,
	}

end

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

function LrmLibrarySocialModelImp:toggleMyFavorite( assetId, albumId, opts )

	local toggleMySpaceFavorite = LrmCatCollaboration.toggleMySpaceFavorite

	if opts and opts.TEST_mock then
		toggleMySpaceFavorite = opts.TEST_mock
	end

	local result = toggleMySpaceFavorite( self._wfCatalog, assetId, albumId )
		:map( function( data )
			return { state = true }
		end )
		:catch( function( data )
			return Rx.Observable.just( { state = false, error = data } )
		end )

	return {
		value = result
	}

end

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

function LrmLibrarySocialModelImp:shareAlbum( albumId, share, opts )

	local collaborationAction

	if share then
		collaborationAction = LrmCatCollaboration.shareAlbum
	else
		collaborationAction = LrmCatCollaboration.unshareAlbum
	end

	if opts and opts.TEST_mock then
		collaborationAction = opts.TEST_mock
	end

	local result = collaborationAction( self._wfCatalog, albumId )
		:map( function( data )
			return { state = true }
		end )
		:catch( function( data )
			return Rx.Observable.just( { state = false, error = data } )
		end )


	return {
		value = result,
	}
end

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

function LrmLibrarySocialModelImp:resolveAlbumFromSpace( spaceId, opts )

	local resolveAlbumFromSpace = LrmCatCollaboration.resolveAlbumFromSpace

	if opts and opts.TEST_mock then
		resolveAlbumFromSpace = opts.TEST_mock
	end

	local result = resolveAlbumFromSpace( self._wfCatalog, spaceId )
		:map( function( data )
			return { state = true }
		end )
		:catch( function( data )
			return Rx.Observable.just( { state = false, error = data } )
		end )

	return {
		value = result
	}

end

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

function LrmLibrarySocialModelImp:getSharedAlbumUrl( albumId, opts )

	local getSharedAlbumUrl = LrmCatCollaboration.sharedAlbumUrl

	if opts and opts.TEST_mock then
		getSharedAlbumUrl = opts.TEST_mock
	end

	local result = getSharedAlbumUrl( self._wfCatalog, albumId, true )
		:map( function( shareUrl )
			return { state = true, sharedAlbumUrl = shareUrl }
		end )
		:catch( function( data )
			return Rx.Observable.just( { state = false, error = data } )
		end )

	return {
		value = result,
	}
end

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

return LrmLibrarySocialModelImp
