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

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.

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

	-- Rx
local Rx = require "Rx"
local Observable = Rx.Observable

local WFRxRunUntilDone = require "WFRxRunUntilDone"

	-- WF.core.utils
local KSStringUtils = require 'KSStringUtils'

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

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

local LrmLibraryAlbumModelImp = {}

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

function LrmLibraryAlbumModelImp:createAlbum( albumTitle, parentId, isFolder, hideInorganize )

	local trimmedAlbumTitle = KSStringUtils.trim( albumTitle )

	local result = LrmCatAlbum.createAlbum( self._wfCatalog, trimmedAlbumTitle, parentId, isFolder, hideInorganize )
		:map( function( albumId )
			return { state = true, albumId = albumId }
		end )
		:catch( function( data )
			return Rx.Observable.just( { state = false, error = data } )
		end )

	return {
		value = result
	}

end

function LrmLibraryAlbumModelImp:convertAlbumToAdhoc(albumId)
	return LrmCatAlbum.convertAlbumToAdhoc(self._wfCatalog, albumId)
end

function LrmLibraryAlbumModelImp:convertAdhocToAlbum(albumId)
	return LrmCatAlbum.convertAdhocToAlbum(self._wfCatalog, albumId)
end

function LrmLibraryAlbumModelImp:shareGroupAlbum( albumId , attributes)

	return {
		value = LrmCatAlbum.shareGroupAlbum( self._wfCatalog, albumId , attributes)
	}


end

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

function LrmLibraryAlbumModelImp:reparentAlbum( albumId, newParentId )

	local result = LrmCatAlbum.isValidAlbum( self._wfCatalog, albumId )
		:flatMap( function( valid )

			if valid then
				return LrmCatAlbum.reparentAlbum( self._wfCatalog, albumId, newParentId )
			else
				return Rx.Observable.throw( "Album doesn't exist" )
			end

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

	return {
		value = result,
	}

end

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

function LrmLibraryAlbumModelImp:renameAlbum( albumId, albumTitle, oldName )
	
	local result = LrmCatAlbum.isValidAlbum( self._wfCatalog, albumId )
		:flatMap( function( valid )
			if valid then
				return LrmCatAlbum.renameAlbum( self._wfCatalog, albumId, albumTitle, false )
			else
				return Rx.Observable.throw( "Album doesn't exist" )
			end
		end )
		:map( function( data )
			return { state = true }
		end )
		:catch( function( data )
			return Rx.Observable.just( { state = false, error = data } )
		end )
		:call( WFRxRunUntilDone )

	return {
		value = result,
	}

end

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

function LrmLibraryAlbumModelImp:deleteAlbum( albumId )

	local catalog = self._wfCatalog
	
	local result = LrmCatAlbum.isValidAlbum( self._wfCatalog, albumId )
		:flatMap( function( valid )
		
			if valid then
				return LrmCatAlbum.deleteAlbum( catalog, albumId )
			else
				return Rx.Observable.throw( "Album doesn't exist" )
			end
			
		end )
		:map( function( data )
			return { state = true }
		end )
		:catch( function( data )
			return Rx.Observable.just( { state = false, error = data } )
		end )
		:call( WFRxRunUntilDone )

	return {
		value = result,
	}

end

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

function LrmLibraryAlbumModelImp:setAlbumForOfflineUse( albumId, useOffline )

	local result = self._wfCatalog:docStore():applyOverlay {
			type = LrmAlbumUtils.getDocTypeForAlbumId (albumId),
			_id = albumId,
			_localOnly = {
				useOffline = useOffline,
			},
		}
		:map( function( data )
			return { state = true }
		end )
		:catch( function( data )
			return Rx.Observable.just( { state = false, error = data } )
		end )
		:call( WFRxRunUntilDone )
		
	return {
		value = result,
	}
end

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

function LrmLibraryAlbumModelImp:setAlbumCover( albumId, assetId )

	local result = self._wfCatalog:docStore():applyOverlay {
			type = LrmAlbumUtils.getDocTypeForAlbumId( albumId ),
			_id = albumId,
			cover = {
				id = assetId
			}
		}
		:map( function( data )
			return { state = true, albumId = albumId  }
		end )
		:catch( function( data )
			return Rx.Observable.just( { state = false, albumId = albumId, error = data } )
		end )
		:call( WFRxRunUntilDone )

	return {
		value = result
	}
end


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

function LrmLibraryAlbumModelImp:getSharedAlbumAttributes( albumId )

	return {
        value = LrmCatAlbum.getSharedAlbumAttributes( self._wfCatalog, albumId)
    }

end


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

function LrmLibraryAlbumModelImp:setSharedAlbumAttributes( albumId , attributes)

	return {
		value = LrmCatAlbum.setSharedAlbumAttributes( self._wfCatalog, albumId , attributes )
	}

end

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


function LrmLibraryAlbumModelImp:stopSharing( albumId )

	return {
		value = LrmCatAlbum.stopSharing( self._wfCatalog, albumId )
	}

end


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


function LrmLibraryAlbumModelImp:getAccessRequests( albumId )

	return {
		value = LrmCatAlbum.getAccessRequests( self._wfCatalog, albumId )
	}

end

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


function LrmLibraryAlbumModelImp:getCollaborators( albumId )

	return {
		value = LrmCatAlbum.getCollaborators( self._wfCatalog, albumId )
	}

end

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


function LrmLibraryAlbumModelImp:getInvites( albumId )

	return {
		value = LrmCatAlbum.getInvites( self._wfCatalog, albumId )
	}

end

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

local function applyUpdate(self,update)

	return {
		value = LrmCatAlbum.updateCollaborator( self._wfCatalog, update )
	}

end


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


function LrmLibraryAlbumModelImp:updateCollaborator( update )

	return applyUpdate(self,update)

end

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


function LrmLibraryAlbumModelImp:acceptAccessRequest( update )

	return applyUpdate(self,update)

end


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

function LrmLibraryAlbumModelImp:rejectAccessRequest( update )

	return applyUpdate(self,update)

end

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

function LrmLibraryAlbumModelImp:inviteByEmail( albumId, email,optAccessLevel )

	return {
		value = LrmCatAlbum.inviteByEmail( self._wfCatalog, albumId , email ,optAccessLevel)
	}

end

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

function LrmLibraryAlbumModelImp:getAllCollaborators()

	return {
		value = LrmCatAlbum.getAllCollaborators( self._wfCatalog )
	}

end

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

function LrmLibraryAlbumModelImp:getAlbumsList( includeAssetCount )

	return {
		value = LrmCatAlbum.getAlbumsList( self._wfCatalog, includeAssetCount )
	}

end

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

function LrmLibraryAlbumModelImp:canEditAsset( docValue )

	return {
		value = Observable.just( LrmCatAlbum.canEditAsset( docValue ) )
	}

end

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

function LrmLibraryAlbumModelImp:canDeleteAsset( docValue )

	return {
		value = Observable.just( LrmCatAlbum.canDeleteAsset( docValue ) )
	}

end

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

function LrmLibraryAlbumModelImp:canDeleteAssetIds( arrayOfDocIds )

	return {
		value = LrmCatAlbum.canDeleteAssetIds( self._wfCatalog, arrayOfDocIds )
	}

end

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

function LrmLibraryAlbumModelImp:hasSharedAlbumsWithDisplayFilters()

	return {
		value = LrmCatAlbum.hasSharedAlbumsWithDisplayFilters( self._wfCatalog )
	}

end

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

function LrmLibraryAlbumModelImp:getMigratedToUnfilteredAlbumId( albumId )

	return {
		value = LrmCatAlbum.getMigratedToUnfilteredAlbumId( self._wfCatalog, albumId )
	}

end

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

function LrmLibraryAlbumModelImp:deleteDisplayFilters( albumId )

	return {
		value = LrmCatAlbum.deleteDisplayFilters( self._wfCatalog, albumId )
	}

end

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

function LrmLibraryAlbumModelImp:leaveSpace( spaceId )

	return {
		value = LrmCatAlbum.leaveSpace( self._wfCatalog, spaceId )
	}

end

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

function LrmLibraryAlbumModelImp:reportAbuse( spaceId, albumId )

	return {
		value = LrmCatAlbum.reportAbuse( self._wfCatalog, spaceId, albumId )
	}

end

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

function LrmLibraryAlbumModelImp:deleteAssets( spaceId, assetIds )

	return {
		value = LrmCatAlbum.deleteAssets( self._wfCatalog, spaceId, assetIds )
	}

end

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

return LrmLibraryAlbumModelImp
