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

ADOBE CONFIDENTIAL
------------------
Copyright 2013 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 KSPrefs = require "KSPrefs"
local WFImsAppIdentity = require 'WFImsAppIdentity'
local WFCapabilities = require 'WFCapabilities'
local WFPlatformInfo = require 'WFPlatformInfo'
--------------------------------------------------------------------------------

local LrmOzSetup = {}

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

local function setupCredentials( clientId, clientSecret )
	
	WFImsAppIdentity.setClientIdAndSecret( clientId, clientSecret )

	local imsDeviceId = WFCapabilities.getCapability( 'LrmConfig.imsDeviceId' )
	
	if imsDeviceId and imsDeviceId ~= '' then
	
		WFImsAppIdentity.setImsDeviceId( imsDeviceId )
		
	else
	
        if WFPlatformInfo.isAndroid then
            local deviceId
            local deviceIdForNewAlgo

            local userId = KSPrefs ['currentUserId']
            local flag = KSPrefs ['IsAndroidIDGeneratedFromNewAlgo']
            if flag or (not userId) then
                deviceId = WFImsAppIdentity.generateDeviceIdWithNewAlgo()
                deviceIdForNewAlgo = deviceId
            else
                deviceId = WFImsAppIdentity.generateDeviceId()
                deviceIdForNewAlgo = WFImsAppIdentity.generateDeviceIdWithNewAlgo()
            end

            WFImsAppIdentity.setImsDeviceId( deviceId )
            WFImsAppIdentity.setImsDeviceIdWithNewAlgo( deviceIdForNewAlgo )
        else
		local deviceId = WFImsAppIdentity.generateDeviceId()
		WFImsAppIdentity.setImsDeviceId( deviceId )
        end
		
	end
	
end

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

function LrmOzSetup.setupCredentials()
	local clientId = KSPrefs["thio_ims_client_id"]
	local clientSecret = KSPrefs["thio_ims_client_secret"]

    if not WFPlatformInfo.isAndroid or (clientId and clientSecret) then
	    setupCredentials( clientId, clientSecret )
    end
	
end

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

return LrmOzSetup

