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

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.

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

	-- RxLua
local Rx = require 'Rx'

	-- WF.core.utils
local WFCloseSupport = require 'WFCloseSupport'
local KSPathUtils = require 'KSPathUtils'

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

	-- WF.sqlite.doc_store
local WFRxDocStoreImp = require 'WFRxDocStoreImp'
local WFOldWFDocStoreMigratorImp = require 'WFOldWFDocStoreMigratorImp'

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

local WFOldWFDocStoreMigratorModel = {}

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

local function runMigration( userId )

	-- Construct path for old WFOzUser userInfoCachePath

	local oldUserInfoCachePath = KSPathUtils.child( KSPathUtils.getStandardFilePath 'caches',
											'Adobe' )
											
	oldUserInfoCachePath = KSPathUtils.child( oldUserInfoCachePath,
										   WFPlatformInfo.getAppIdentifier() )

	oldUserInfoCachePath = KSPathUtils.child( oldUserInfoCachePath,
										   'oz-user-' .. userId )
	
	
	-- Construct path for new WFCatUser userInfoCachePath
	
	local newUserInfoCachePath = KSPathUtils.child( KSPathUtils.getStandardFilePath 'caches',
											 'Adobe' )

	newUserInfoCachePath = KSPathUtils.child( newUserInfoCachePath,
						   'user' )
						   
	newUserInfoCachePath = KSPathUtils.child( newUserInfoCachePath,
										   'oz-user-' .. userId )
	
	if WFFileUtils.exists( newUserInfoCachePath ) then
		pcall( WFFileUtils.delete, newUserInfoCachePath )
	end
	
	if not WFFileUtils.exists( oldUserInfoCachePath ) then
		return nil
	end
	
	local rxdocStoreImp = WFRxDocStoreImp( newUserInfoCachePath )
	local ozDocStoreMigratorImp = WFOldWFDocStoreMigratorImp( rxdocStoreImp, oldUserInfoCachePath )
	
	return ozDocStoreMigratorImp
	
end

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

function WFOldWFDocStoreMigratorModel.modelForUserId( userId )

	local o
	local ozDocStoreMigrator = runMigration( userId )
	
	if ozDocStoreMigrator then
		o = ozDocStoreMigrator:runUntilDone()
	else
		o = Rx.Observable.just( true )
	end
	
	local modelResult = {
	
		value = o,
		
		_ozDocStoreMigrator = ozDocStoreMigrator,
		
		close = function( self )
		
			if not WFCloseSupport.hasBeenOrWillBeClosed( self._ozDocStoreMigrator ) then
				self._ozDocStoreMigrator:close()
			end
			
		end,
	}
	
	return modelResult
	
end

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

return WFOldWFDocStoreMigratorModel
