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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 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.

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

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

	-- WF.core.self_test
local WFRxTestCase = require "WFRxTestCase"

	-- WF.core.utils
local KSIdGenerator = require 'KSIdGenerator'
local KSTableUtils = require 'KSTableUtils'

	-- WF.lrmobile
local LrmConfig = require 'LrmConfig'
local LrmLibraryAlbumStatusModelImp = require 'LrmLibraryAlbumStatusModelImp'
local LrmCatalogTestHelper = require 'LrmCatalogTestHelper'

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

local LrmLibraryAlbumStatusModelImpTest = {}

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

local function makeLocalOnly( withProxy, withOriginal, isVideo, includeDevelopAux )
	
	local localOnly

	if WFPlatformInfo.isiOS then
		
		localOnly = {
			files = {
				thumbnail2x = {
					path = 'blobs/...',
					contentId = '16b97d9d9e3042d09633c3878622d48a',
				},
				preview = {
					path = 'blobs/...',
					contentId = '16b97d9d9e3042d09633c3878622d48a',
				}
			}
		}
		
		if withProxy then
			if isVideo then
				localOnly.files.proxies = {
					['1080p'] = {
						relativePath = '/path',
					}
				}
			else
				localOnly.files.proxy = {
					path = '/path',
				}
			end
		end
		
		if withOriginal then
			localOnly.files.master = {
				path = 'Originals/...',
			}
		end
		
	else
		
		localOnly = {
			thumbnail2x = {
				md5 = 'hash',
				revId = '16b97d9d9e3042d09633c3878622d48a',
			},
			preview = {
				md5 = 'hash',
				revId = '16b97d9d9e3042d09633c3878622d48a',
			}
		}
		
		if withProxy then
			if isVideo then
				KSTableUtils.setNested( localOnly, '/path', 'files', 'proxies', '1080p', 'relativePath' )
			else
				localOnly.proxy = {
					filePath = '/path'
				}
			end
		end
		
		if withOriginal then
			localOnly.originalFilePath = '/path'
		end
		
	end

	if includeDevelopAux then

		local auxLocalOnly = {
			[ 'aux1-digest' ] = {
				relativePath = '/path',
			},
			[ 'aux2-digest' ] = {
				relativePath = '/path',
			}
		}

		KSTableUtils.setNested( localOnly, auxLocalOnly, 'files', 'aux' )

		-- Also include xmp_develop if includeAux is true

		local sha256FieldName = WFPlatformInfo.isiOS and 'contentId' or 'sha256'
		local xmpDevelopLocalOnly = {
			path = '/path',
			[ sha256FieldName ] = 'xmpDevelop-sha256'
		}

		KSTableUtils.setNested( localOnly, xmpDevelopLocalOnly, 'files', 'xmp_develop' )

	end

	return localOnly

end

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

local function makeServerProvidedMetadata( withProxy, withOriginal, isVideo, includeDevelopAux )

	local links = {
		[ '/rels/rendition_type/thumbnail2x' ] = {
			href = '/v1.0/catalogs/f50509f546184519afa38d4e55e442fc/assets/496e4bec7eff1bee558b4760ae00efa3/revisions/16b97d9d9e3042d09633c3878622d48a/renditions/835c23790d534b28b8b7bb99740d13ba',
		},
		[ '/rels/rendition_type/2048' ] = {
			href = '/v1.0/catalogs/f50509f546184519afa38d4e55e442fc/assets/496e4bec7eff1bee558b4760ae00efa3/revisions/16b97d9d9e3042d09633c3878622d48a/renditions/835c23790d534b28b8b7bb99740d13ba',
		}
	}

	if withOriginal then

		links[ '/rels/master' ] = {
			href = '/v1.0/catalogs/f50509f546184519afa38d4e55e442fc/assets/496e4bec7eff1bee558b4760ae00efa3/revisions/16b97d9d9e3042d09633c3878622d48a/master'
		}

	end

	if withProxy then

		if isVideo then

			links[ '/rels/proxy_type/1080p' ] = {
				href = '/v1.0/catalogs/f50509f546184519afa38d4e55e442fc/assets/496e4bec7eff1bee558b4760ae00efa3/revisions/16b97d9d9e3042d09633c3878622d48a/proxies/a5429e4c4bc7402bb8fb9102dbde4317',
				fileSize = 1331695
			}

		else

			links[ '/rels/proxy_type/2560' ] = {
				href = '/v1.0/catalogs/f50509f546184519afa38d4e55e442fc/assets/496e4bec7eff1bee558b4760ae00efa3/revisions/16b97d9d9e3042d09633c3878622d48a/proxies/a5429e4c4bc7402bb8fb9102dbde4317'
			}

		end
	end

	if includeDevelopAux then

		links[ '/rels/xmp/develop' ] = {
			href = '/v1.0/catalogs/f50509f546184519afa38d4e55e442fc/assets/496e4bec7eff1bee558b4760ae00efa3/revisions/16b97d9d9e3042d09633c3878622d48a/xmp_develop'
		}

		links[ '/rels/aux/aux1Id' ] = {
			href = '/v1.0/catalogs/f50509f546184519afa38d4e55e442fc/assets/496e4bec7eff1bee558b4760ae00efa3/revisions/16b97d9d9e3042d09633c3878622d48a/auxes/aux1Id'
		}

		links[ '/rels/aux/aux2Id' ] = {
			href = '/v1.0/catalogs/f50509f546184519afa38d4e55e442fc/assets/496e4bec7eff1bee558b4760ae00efa3/revisions/16b97d9d9e3042d09633c3878622d48a/auxes/aux2Id'
		}

	end

	return { links = links }


end

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

local function makeAuxAndDevelop()
	
	local aux = {
		[ 'aux1Id' ] = {
			type = 'mask',
			digest = 'aux1-digest',
			fileSize = 5000,
			sha256 = 'aux1-sha256'
		},
		[ 'aux2Id' ] = {
			type = 'filter',
			digest = 'aux2-digest',
			fileSize = 5000,
			sha256 = 'aux2-sha256'
		}
	}

	local develop = {
		xmpCameraRaw = {
			sha256 = 'xmpDevelop-sha256'
		},
		masks = {
			[ 'aux1Id' ] = {
				digest = 'aux1-digest'
			}
		},
		filters = {
			[ 'groupDigest' ] = {
				[ 'aux2Id' ] = {
					digest = 'aux2-digest',
					sourceLongEdge = 6000,
					crsSizeX = 1024,
					crsSizeY = 768
				}
			}
		}
	}

	return aux, develop

end

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

local function createCatalogService( rxTest )

	local catalog = LrmCatalogTestHelper.makeCatalogService( rxTest )
	
	rxTest:finally( function()
		catalog:close()
	end )
	
	return catalog
	
end

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

-- @mark unsynchronizedAssetsForAlbum tests

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

function LrmLibraryAlbumStatusModelImpTest.rxTestBinaryDownloadProgressPreferredProxy( rxTest )

	rxTest:setTimeLimit( 5 )
	
	local catalog = createCatalogService( rxTest )

	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local albumId = KSIdGenerator.uuidInLowercaseHex()
	local albumAssetId = KSIdGenerator.uuidInLowercaseHex()

	local parentModel = {
		catalog = catalog,
	}

	local obs = rxTest:observe( LrmLibraryAlbumStatusModelImp.unsynchronizedAssetsForAlbum( parentModel, albumId, LrmConfig.StrategyPreferProxy ).value )

	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'album',
				_id = albumId,
				subtype = 'collection',
				name = 'test album',
				_localOnly = {
					useOffline = true,
				}
			} )

	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'album_asset',
				_id = albumAssetId,
				album = {
					id = albumId,
				},
				asset = {
					id = assetId,
				}
			} )

	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'asset',
				_id = assetId,
				subtype = 'image',
				_serverProvidedMetadata = makeServerProvidedMetadata( true, true )
			} )

		obs:expectData( 1 )

		rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'asset',
				_id = assetId,
				subtype = 'image',
				_localOnly = makeLocalOnly( true, false ),
				_serverProvidedMetadata = makeServerProvidedMetadata( true, true )
			} )

		obs:expectData( 0 )

end

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

function LrmLibraryAlbumStatusModelImpTest.rxTestBinaryDownloadProgressPreferredMaster( rxTest )

	rxTest:setTimeLimit( 5 )
	
	local catalog = createCatalogService( rxTest )

	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local albumId = KSIdGenerator.uuidInLowercaseHex()
	local albumAssetId = KSIdGenerator.uuidInLowercaseHex()

	local parentModel = {
		catalog = catalog,
	}

	local obs = rxTest:observe( LrmLibraryAlbumStatusModelImp.unsynchronizedAssetsForAlbum( parentModel, albumId, LrmConfig.StrategyPreferMaster ).value )

	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'album',
				_id = albumId,
				subtype = 'collection',
				name = 'test album',
				_localOnly = {
					useOffline = true,
				}
			} )


	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'album_asset',
				_id = albumAssetId,
				album = {
					id = albumId,
				},
				asset = {
					id = assetId,
				}
			} )

	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'asset',
				_id = assetId,
				subtype = 'image',
				_serverProvidedMetadata = makeServerProvidedMetadata( true, true )
			} )

	obs:expectData( 1 )

	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'asset',
			_id = assetId,
			subtype = 'image',
			_localOnly = makeLocalOnly( true, true ),
			_serverProvidedMetadata = makeServerProvidedMetadata( true, true )
		} )

	obs:expectData( 0 )

end

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

function LrmLibraryAlbumStatusModelImpTest.rxTestBinaryDownloadProgressPreferredMasterWithProxyOnly( rxTest )

	local catalog = createCatalogService( rxTest )
	
	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local albumId = KSIdGenerator.uuidInLowercaseHex()
	local albumAssetId = KSIdGenerator.uuidInLowercaseHex()

	local parentModel = {
		catalog = catalog,
	}

	local obs = rxTest:observe( LrmLibraryAlbumStatusModelImp.unsynchronizedAssetsForAlbum( parentModel, albumId, LrmConfig.StrategyPreferMaster ).value )

	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'album',
			_id = albumId,
			subtype = 'collection',
			name = 'test album',
			_localOnly = {
				useOffline = true,
			}
		} )

	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'album_asset',
			_id = albumAssetId,
			album = {
				id = albumId,
			},
			asset = {
				id = assetId,
			},
		} )

	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'asset',
			_id = assetId,
			subtype = 'image',
			_serverProvidedMetadata = makeServerProvidedMetadata( true, false )
		} )

	obs:expectData( 1 )

	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'asset',
			_id = assetId,
			_localOnly  = makeLocalOnly( true, false ),
			_serverProvidedMetadata = makeServerProvidedMetadata( true, false )
		} )

	obs:expectData( 0 )

end

----------------------------------------------------------------------------------
--
---- @mark missingCacheSizeForAlbum tests
--
----------------------------------------------------------------------------------

function LrmLibraryAlbumStatusModelImpTest.rxTestMissingCacheSizeOnlyProxy( rxTest )

	rxTest:setTimeLimit( 5 )
	
	local catalog = createCatalogService( rxTest )

	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local albumId = KSIdGenerator.uuidInLowercaseHex()
	local albumAssetId = KSIdGenerator.uuidInLowercaseHex()

	local parentModel = {
		catalog = catalog,
	}

	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'album',
				_id = albumId,
				subtype = 'collection',
				name = 'test album',
				_localOnly = {
					useOffline = true,
				}
			} )


	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'album_asset',
				_id = albumAssetId,
				album = {
					id = albumId,
				},
				asset = {
					id = assetId,
				}
			} )

	local aux, develop = makeAuxAndDevelop()

	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'asset',
				_id = assetId,
				subtype = 'image',
				aux = aux,
				develop = develop,
				_serverProvidedMetadata = makeServerProvidedMetadata( true, true, nil, true )
			} )


	local obs = rxTest:observe( LrmLibraryAlbumStatusModelImp.missingCacheSizeForAlbum( parentModel, albumId, LrmConfig.StrategyPreferProxy ).value )

	obs:expectData( 1479926 )

	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'asset',
				_id = assetId,
				subtype = 'image',
				aux = aux,
				develop = develop,
				_localOnly = makeLocalOnly( true, false, nil, true ),
				_serverProvidedMetadata = makeServerProvidedMetadata( true, true, nil, true )
			} )

	obs:expectData( 0 )

end

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

function LrmLibraryAlbumStatusModelImpTest.rxTestMissingCacheSizeOnlyProxy_video( rxTest )

	rxTest:setTimeLimit( 5 )

	local catalog = createCatalogService( rxTest )
	
	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local albumId = KSIdGenerator.uuidInLowercaseHex()
	local albumAssetId = KSIdGenerator.uuidInLowercaseHex()

	local isVideo = true

	local parentModel = {
		catalog = catalog,
	}

	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'album',
			_id = albumId,
			subtype = 'collection',
			name = 'test album',
			_localOnly = {
				useOffline = true,
			}
		})


	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'album_asset',
			_id = albumAssetId,
			album = {
				id = albumId,
			},
			asset = {
				id = assetId,
			}
		})

	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'asset',
			_id = assetId,
			subtype = 'video',
			_serverProvidedMetadata = makeServerProvidedMetadata( false, true, true ),
			video = {
				duration = { 2, 1 }
			}
		})


	local obs = rxTest:observe( LrmLibraryAlbumStatusModelImp.missingCacheSizeForAlbum( parentModel, albumId, LrmConfig.StrategyPreferProxy ).value )

	obs:expectData( 17549184 )
	
	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'asset',
			_id = assetId,
			subtype = 'video',
			_serverProvidedMetadata = makeServerProvidedMetadata( true, false, true ),
			importSource = {
				fileSize = 20971525
			}
		})
	
	obs:expectData( 1841519 )

	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'asset',
			_id = assetId,
			subtype = 'video',
			_localOnly = makeLocalOnly( true, false, isVideo ),
			_serverProvidedMetadata = makeServerProvidedMetadata( true, false, true ),
			importSource = {
				fileSize = 20971525
			}
		})

	obs:expectData( 0 )

end

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

function LrmLibraryAlbumStatusModelImpTest.rxTestMissingCacheSizeMasterAndProxy( rxTest )

	rxTest:setTimeLimit( 5 )

	local catalog = createCatalogService( rxTest )
	
	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local albumId = KSIdGenerator.uuidInLowercaseHex()
	local albumAssetId = KSIdGenerator.uuidInLowercaseHex()

	local parentModel = {
		catalog = catalog,
	}

	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'album',
				_id = albumId,
				subtype = 'collection',
				name = 'test album',
				_localOnly = {
					useOffline = true,
				}
			} )

	local aux, develop = makeAuxAndDevelop()
	
	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'asset',
				_id = assetId,
				subtype = 'image',
				aux = aux,
				develop = develop,
				_serverProvidedMetadata = makeServerProvidedMetadata( true, true, nil, true )
			} )

	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'album_asset',
				_id = albumAssetId,
				album = {
					id = albumId,
				},
				asset = {
					id = assetId,
				}
			} )

	local obsMaster = rxTest:observe( LrmLibraryAlbumStatusModelImp.missingCacheSizeForAlbum( parentModel, albumId, LrmConfig.StrategyPreferMaster ).value )

	obsMaster:expectData( 21507728 )

	local obsProxy = rxTest:observe( LrmLibraryAlbumStatusModelImp.missingCacheSizeForAlbum( parentModel, albumId, LrmConfig.StrategyPreferProxy ).value )

	obsProxy:expectData( 1479926 )

	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'asset',
			_id = assetId,
			subtype = 'image',
			aux = aux,
			develop = develop,
			_localOnly  = makeLocalOnly( true, false, nil, true ),
			_serverProvidedMetadata = makeServerProvidedMetadata( true, true, nil, true )
		} )

	obsMaster:expectData( 20971520 )

	obsProxy:expectData( 0 )

end

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

function LrmLibraryAlbumStatusModelImpTest.rxTestMissingCacheSizeMasterAndProxy_video( rxTest )

	rxTest:setTimeLimit( 5 )

	local catalog = createCatalogService( rxTest )
	
	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local albumId = KSIdGenerator.uuidInLowercaseHex()
	local albumAssetId = KSIdGenerator.uuidInLowercaseHex()

	local isVideo = true

	local parentModel = {
		catalog = catalog,
	}

	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'album',
			_id = albumId,
			subtype = 'collection',
			name = 'test album',
			_localOnly = {
				useOffline = true,
			}
		})


	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'album_asset',
			_id = albumAssetId,
			album = {
				id = albumId,
			},
			asset = {
				id = assetId,
			}
		})

	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'asset',
			_id = assetId,
			subtype = 'video',
			_serverProvidedMetadata = makeServerProvidedMetadata( true, true, isVideo ),
			importSource = {
				fileSize = 20971525
			}
		})


	local obsMaster = rxTest:observe( LrmLibraryAlbumStatusModelImp.missingCacheSizeForAlbum( parentModel, albumId, LrmConfig.StrategyPreferMaster ).value )

	obsMaster:expectData( 21481349 )

	local obsProxy = rxTest:observe( LrmLibraryAlbumStatusModelImp.missingCacheSizeForAlbum( parentModel, albumId, LrmConfig.StrategyPreferProxy ).value )

	obsProxy:expectData( 1841519 )
	
	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'asset',
			_id = assetId,
			subtype = 'video',
			_localOnly = makeLocalOnly( true, false, isVideo ),
			_serverProvidedMetadata = makeServerProvidedMetadata( true, true, isVideo ),
			importSource = {
				fileSize = 20971525
			}
		})

	obsMaster:expectData( 20971525 )
	obsProxy:expectData( 0 )
	
	rxTest:waitFor( catalog:docStore():applyOverlay {
			type = 'asset',
			_id = assetId,
			subtype = 'video',
			_localOnly = makeLocalOnly( true, true, isVideo ),
			_serverProvidedMetadata = makeServerProvidedMetadata( true, true, isVideo ),
			importSource = {
				fileSize = 20971525
			}
		})
		
	obsMaster:expectData( 0 )

end

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

function LrmLibraryAlbumStatusModelImpTest.rxTestBinaryDownloadProgressRedacted( rxTest )

	rxTest:setTimeLimit( 5 )
	
	local catalog = createCatalogService( rxTest )

	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local albumId = KSIdGenerator.uuidInLowercaseHex()
	local albumAssetId = KSIdGenerator.uuidInLowercaseHex()

	local parentModel = {
		catalog = catalog,
	}

	local obs = rxTest:observe( LrmLibraryAlbumStatusModelImp.unsynchronizedAssetsForAlbum( parentModel, albumId, LrmConfig.StrategyPreferProxy ).value )

	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'album',
				_id = albumId,
				subtype = 'collection',
				name = 'test album',
				_localOnly = {
					useOffline = true,
				}
			} )

	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'album_asset',
				_id = albumAssetId,
				album = {
					id = albumId,
				},
				asset = {
					id = assetId,
				}
			} )

	rxTest:waitFor( catalog:docStore():applyOverlay {
				type = 'asset',
				_id = assetId,
				_spaceId = "cc5b58f1c79544f6abcb2ee50e3297b9",
				subtype = 'image',
				_serverProvidedMetadata = {
					base = "https://photos.adobe.io/v2/spaces/cc5b58f1c79544f6abcb2ee50e3297b9/",
					links = {
						[ "/rels/master" ] = {
							content_type = "image/jpeg",
							exists = true
						},
						[ "/rels/proxy_type/2560" ] = {
							exists = true
						},
						[ "/rels/rendition_type/2048" ] = {
							href = "/v2/spaces/cc5b58f1c79544f6abcb2ee50e3297b9/assets/f63baa7eda254f668b876d000118b00e/revisions/666f265f27c44fc18ad1773319168ffa/renditions/758559a07063312d398fa4e86db148a8"
						},
						[ "/rels/rendition_type/thumbnail2x" ] = {
							href = "/v2/spaces/cc5b58f1c79544f6abcb2ee50e3297b9/assets/f63baa7eda254f668b876d000118b00e/revisions/666f265f27c44fc18ad1773319168ffa/renditions/97c370e7e21bda1b4f4ee7f700ef9159"
						},
						self = {
							href = "/v2/spaces/cc5b58f1c79544f6abcb2ee50e3297b9/updates/14beacbc0f0e92bae0ddfe90f0bdc548"
						}
					},
				},
			} )

		obs:expectData( 1 )

		rxTest:waitFor( catalog:docStore():applyOverlay {
				_id = assetId,
				_localOnly = makeLocalOnly( false, false ),
			} )

		obs:expectData( 0 )

end

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

return WFRxTestCase.wrapSuite( 'LrmLibraryAlbumStatusModelImpTest', LrmLibraryAlbumStatusModelImpTest )

