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

WFOzSharing.lua

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

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

NOTICE: All information contained herein is, and remains 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 are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

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

	-- WF.core.reactive
local KSReactive = require 'KSReactive'
	
	-- WF.core.utils
local WFArguments = require 'WFArguments'
local WFUnknown = require 'WFUnknown'

	-- WF.oz_client.catalog
local OzCatSharingHelper = require 'OzCatSharingHelper'


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

-- @mark WFOzSharing class

local WFOzSharing = {}

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

local kSpaceIdForAlbumIdQuery = [[SELECT spaceId FROM space_album WHERE albumId == ?]]

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

function WFOzSharing.publicShareUrlB( wfOzCatalog, ozAlbumId )
	
	WFArguments.check( 'WFOzCatalog', 'string' )
	
	return OzCatSharingHelper.sharedAssetOrAlbumUrlB( wfOzCatalog:indexStore(),
													wfOzCatalog:docStore(),
													'album',
													ozAlbumId )
		:closingMapB( function( maybeUrl )
		
			if WFUnknown.isUnknown( maybeUrl ) then
				return nil
			else
				return maybeUrl or false
			end
		
		end )

end

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

function WFOzSharing.isLoginRequiredForShareB( wfOzCatalog, ozAlbumId )
	
	WFArguments.check( 'WFOzCatalog', 'string' )
	
	local indexStore = wfOzCatalog:indexStore()
	local docStore = wfOzCatalog:docStore()
	
	local resultB = indexStore:queryB( kSpaceIdForAlbumIdQuery, ozAlbumId )
	
		:closingMapSwitchB( function ( rows )
		
			local spaceId = rows and rows[ 1 ] and rows[ 1 ][ 1 ]
			
			if spaceId then
			
				return docStore:fieldValueB( spaceId,
							'private' )
				:mapB( function ( data )
					return data
				end )
			else
				return KSReactive.constantB( nil )
			end
			
		end )
		
	return resultB

end

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

function WFOzSharing.spaceIdB( wfOzCatalog, ozAlbumId )

	WFArguments.check( 'WFOzCatalog', 'string' )
	
	local indexStore = wfOzCatalog:indexStore()
	
	local resultB = indexStore:queryB( kSpaceIdForAlbumIdQuery, ozAlbumId )
	
		:mapB( function ( rows )
			
			if WFUnknown.isUnknown( rows ) then
				return nil
			else
				
				local spaceId = rows and rows[ 1 ] and rows[ 1 ][ 1 ]
			
				return spaceId
			end
				
		end )
		
	return resultB

end

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

function WFOzSharing.makePublicShareP( wfOzCatalog, ozAlbumId )
	
	WFArguments.check( 'WFOzCatalog', 'string' )
	
	local indexStore = wfOzCatalog:indexStore()
	local ozDocStore = require 'OzDocStoreViaWFDocStore'( wfOzCatalog:docStore(), wfOzCatalog:ozCatalogId() )
	
	return OzCatSharingHelper.shareAssetOrAlbum( indexStore,
													ozDocStore,
													'album',
													ozAlbumId,
													'space_album' )
				:mapDataP( function( status, spaceId )
					
					if status == 'data' and spaceId then
						return wfOzCatalog:docStore():applyOverlayP(
							{
								_id = spaceId,
								_slot = 'main',
								private = false,
							} )
							:mapDataP( function()
								return status, spaceId
							end )
					else
						return status, spaceId
					end

				end )

end

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

function WFOzSharing.makePrivateShareP( wfOzCatalog, ozAlbumId )
	
	WFArguments.check( 'WFOzCatalog', 'string' )
	
	local indexStore = wfOzCatalog:indexStore()
	local ozDocStore = require 'OzDocStoreViaWFDocStore'( wfOzCatalog:docStore(), wfOzCatalog:ozCatalogId() )
		
	return OzCatSharingHelper.shareAssetOrAlbum( indexStore,
													ozDocStore,
													'album',
													ozAlbumId,
													'space_album' )
				:mapDataP( function( status, spaceId )
					
					if status == 'data' and spaceId then
						return wfOzCatalog:docStore():applyOverlayP(
							{
								_id = spaceId,
								_slot = 'main',
								private = true,
							} )
							:mapDataP( function()
								return status, spaceId
							end )
					else
						return status, spaceId
					end

				end )

end

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

function WFOzSharing.removeShareP( wfOzCatalog, ozAlbumId )
	
	WFArguments.check( 'WFOzCatalog', 'string' )
	
	local indexStore = wfOzCatalog:indexStore()
	local docStore = wfOzCatalog:docStore()
	
	return OzCatSharingHelper.unshareAssetOrAlbumP( indexStore,
													require 'OzDocStoreViaWFDocStore'( docStore, wfOzCatalog:ozCatalogId() ),
													'album',
													ozAlbumId,
													'space_album' )

end

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

return WFOzSharing
