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

WFCatOzErrors.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2018 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'
local KSTableUtils = require 'KSTableUtils'
local WFTableMerge = require 'WFTableMerge'

	-- WF.core.logger
local WFLogger = require 'WFLogger'

local logger = WFLogger 'WFCatOzErrors'

local derefOpt = KSTableUtils.derefOpt

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

local WFCatOzErrors = {}

WFCatOzErrors.Errors = {
	
	kInlineXmpDevelopSettingTooBigError = 'InlineXmpDevelopSettingTooBigError',
	kInputValidationError = 'InputValidationError',
	kMultipartCompleteError = 'MultipartCompleteError',
	kPayloadValidationError = 'PayloadValidationError',
	kResourceExistsError = 'ResourceExistsError',
	kResourceNotFoundError = 'ResourceNotFoundError',
	kStorageExceededError = 'StorageExceededError',
	kTrialOrSubscriptionExpiredError = 'TrialOrSubscriptionExpiredError',
	kPreconditionFailedError = 'PreconditionFailed',
	kAssetGoneError = 'AssetGoneError',
	kInvalidAccessRequestError = 'InvalidAccessRequestError',
	kSpaceUserExistsError = 'SpaceUserExistsError',
	kRestoreAssetError = 'RestoreAssetError',
	kInsufficientPermissionError = 'InsufficientPermissionError',
	kEntitlementExpiredError = 'EntitlementExpiredError',
	
}

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

local function maybeGetOzErrorHash( docValue )
	
	return derefOpt( docValue, '_localOnly', 'ozError' )
	
end

WFCatOzErrors.maybeGetOzErrorHash = maybeGetOzErrorHash

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

local function maybeGetErrorSubtype( docValue )
	return derefOpt( docValue, '_localOnly', 'ozError', 'subtype' )
end

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

local function hasStorageExceededError( docValue ) 
	local maybeErrorSubtype = maybeGetErrorSubtype( docValue )
	
	return maybeErrorSubtype == 'StorageExceededError'
end

WFCatOzErrors.hasStorageExceededError = hasStorageExceededError

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

local function hasTrialOrSubscriptionExpiredError( docValue )
	local maybeErrorSubtype = maybeGetErrorSubtype( docValue )
	
	return maybeErrorSubtype == 'TrialOrSubscriptionExpiredError'
end

WFCatOzErrors.hasTrialOrSubscriptionExpiredError = hasTrialOrSubscriptionExpiredError

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

local function hasEntitlementExpiredError( docValue )
	local maybeErrorSubtype = maybeGetErrorSubtype( docValue )
	
	return maybeErrorSubtype == 'EntitlementExpiredError'
end

WFCatOzErrors.hasEntitlementExpiredError = hasEntitlementExpiredError

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

local function hasMultipartCompleteError( docValue ) 
	local maybeErrorSubtype = maybeGetErrorSubtype( docValue )
	
	return maybeErrorSubtype == 'MultipartCompleteError'
end

WFCatOzErrors.hasMultipartCompleteError = hasMultipartCompleteError

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

local function hasResourceNotFoundError( docValue ) 
	local maybeErrorSubtype = maybeGetErrorSubtype( docValue )
	
	return maybeErrorSubtype == 'ResourceNotFoundError'
end

WFCatOzErrors.hasResourceNotFoundError = hasResourceNotFoundError

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

local function hasInsufficientPermissionError( docValue )
	local maybeErrorSubtype = maybeGetErrorSubtype( docValue )
	
	return maybeErrorSubtype == 'InsufficientPermissionError'
end

WFCatOzErrors.hasInsufficientPermissionError = hasInsufficientPermissionError

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

local function createOverlayForOzErrorSubtype( docId, errorSubtype, maybeReason )
	
	-- [ylo 2017-11-28]: Different errors may overwrite each other, which leads to wrong behavior
 	-- e.g. MultipartCompleteError and StorageExceededError, one would re-upload asset while the other stop uploading.
	return {
		_id = docId,
		_localOnly = { 
			ozError = { 
				subtype = errorSubtype,
				reason = maybeReason
			}
		}
	}
	
end

WFCatOzErrors.createOverlayForOzErrorSubtype = createOverlayForOzErrorSubtype

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

local function addOverlayForOzErrorSubtype( docValue, errorSubtype, maybeReason )
	
	local overlay = createOverlayForOzErrorSubtype( docValue._id, errorSubtype, maybeReason )
	
	return WFTableMerge.applyOverlay( docValue, overlay )

end

WFCatOzErrors.addOverlayForOzErrorSubtype = addOverlayForOzErrorSubtype

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

local function createOverlayToRemoveOzError( docId )
	
	return {
		_id = docId,
		_localOnly = {
			ozError = { _delete = true }
		}
	}
	
end

WFCatOzErrors.createOverlayToRemoveOzError = createOverlayToRemoveOzError

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

local function shouldSkipUploadDueToError( docValue )
	
	local shouldSkipUpload = false
	local reason

	if WFCatOzErrors.hasStorageExceededError( docValue ) then
		shouldSkipUpload = true
		reason = 'StorageExceededError'
	end
	
	if WFCatOzErrors.hasTrialOrSubscriptionExpiredError( docValue ) then
		shouldSkipUpload = true
		reason = 'TrialOrSubscriptionExpiredError'
	end
	
	if WFCatOzErrors.hasResourceNotFoundError( docValue ) then
		shouldSkipUpload = true
		reason = 'ResourceNotFoundError'
	end
	
	if WFCatOzErrors.hasInsufficientPermissionError( docValue ) then
		shouldSkipUpload = true
		reason = 'InsufficientPermissionError'
	end
	
	-- more upload skipping errors should be added here
	
	return shouldSkipUpload, reason
	
end

WFCatOzErrors.shouldSkipUploadDueToError = shouldSkipUploadDueToError

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

local function markDocsWithOzError( docStore, errorsToDocIdArrays, maybeReason )
	
	local errorOverlays = {}
	
	for ozErrorSubtype, docIdsForError in pairs( errorsToDocIdArrays ) do

		logger:warnf( "Marking %d docs with error %s reason %s", #docIdsForError, ozErrorSubtype, maybeReason )
		
		for _, docId in ipairs( docIdsForError ) do
			local overlay = WFCatOzErrors.createOverlayForOzErrorSubtype( docId, ozErrorSubtype, maybeReason )
			errorOverlays[ #errorOverlays + 1 ] = overlay
		end
	end
	
	
	if not next( errorOverlays ) then
		return Rx.Observable.just()
	end
	
	return docStore:bulkApplyOverlay( errorOverlays )
	
end

WFCatOzErrors.markDocsWithOzError = markDocsWithOzError

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

return WFCatOzErrors
