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

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.

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

-- WF.core.rxlua
local _ = require 'WFRxPromiseBridge'

-- WF.core.reactive
local KSReactive = require 'KSReactive'
local KSPromises = require 'KSPromises'

-- WF.lrmobile_models
local LrmAssetUtils = require 'LrmAssetUtils'
local OzCatalogUploadStatus = require 'OzCatalogUploadStatus'
local LrmCatXmpDevelop = require "LrmCatXmpDevelop"
local WFCatSharedWithYouUtils = require 'WFCatSharedWithYouUtils'

-- wichitafoundation.core.logging
local WFLogger = require 'WFLogger'

local KSTableUtils = require 'KSTableUtils'
local derefOpt = assert(KSTableUtils.derefOpt)
--------------------------------------------------------------------------------

local exportModelLog = WFLogger 'LrmExportModel'
:setLevel( 'debug' )

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

local LrmExportModel = {}

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

local Model = {}

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

function LrmExportModel.makeModel(catalog, assetIds)

	assert(catalog)
	assert(assetIds)

	local result = {}
	local resultB = KSReactive.variableB(nil)

	local function getDocValueP(assetId)
		catalog:photoInfoB(assetId)
	end

	local function getExportInfoForAssetP(assetId)

		return catalog:docStore():docValueOnceP('asset', assetId)
		:mapP(function(_, docValue)
			local settings = docValue.develop or {}

			local exportInfo = {}

			exportInfo.localUrl = docValue and docValue._localOnly and docValue._localOnly.localUrl
			exportInfo.masterPath = OzCatalogUploadStatus.checkAndCorrectLocalPath(LrmAssetUtils.getMasterPath(docValue))
			exportInfo.hasMaster = LrmAssetUtils.hasMaster(docValue)
			exportInfo.hasMasterLocally = LrmAssetUtils.hasMasterLocally(docValue)
			exportInfo.hasPreviewLocally = LrmAssetUtils.hasPreviewLocally(docValue)
			exportInfo.importSHA = docValue and docValue.importSource and docValue.importSource.sha256
			exportInfo.hasProxyLocally = LrmAssetUtils.hasProxyLocally(docValue)
			exportInfo.proxyPath = OzCatalogUploadStatus.checkAndCorrectLocalPath(LrmAssetUtils.getProxyPath(docValue))
			exportInfo.fileSize = docValue and docValue.importSource and docValue.importSource.fileSize
			exportInfo.fileName = docValue and docValue.importSource and docValue.importSource.fileName
			exportInfo.settings = settings
			exportInfo.localXmpDevelopPath = ""
			exportInfo.valid = true
			exportInfo.isSpaceAsset = (docValue._spaceId ~= nil)
			exportInfo.isAssetEditable = WFCatSharedWithYouUtils.canEditAsset( docValue )

			if not LrmAssetUtils.hasExternalXmpDevelop(docValue) then
				return KSPromises.resolvedPromiseP('data', exportInfo)
			end

			return LrmCatXmpDevelop.getExternalXmpDevelopP(catalog, docValue)
			:mapP(function(status, xmpCameraRaw, xmpRelPath)
				if status == 'data' then
					exportInfo.localXmpDevelopPath = xmpRelPath or ""
					exportInfo.valid = true
				else
					exportInfo.valid = false
				end

				return KSPromises.resolvedPromiseP('data', exportInfo)
			end)
		end)
		:mapDataP_new(function(exportInfo)
			result[assetId] = exportInfo
		end)
	end

	KSPromises.allP(getExportInfoForAssetP, assetIds)
	:whenResolvedDo(function()
		resultB:set(result)
		end

	)

	:attachTo(resultB)

	return {
		assetIds = assetIds,
		catalog = catalog,
		valueB = resultB,
		models = Model,
	}

end

return LrmExportModel
