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

ADOBE CONFIDENTIAL
------------------
Copyright 2019 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 WFRxTestCase = require 'WFRxTestCase'
local LrmCatalogTestHelper = require 'LrmCatalogTestHelper'

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

-- WF.core.self_test
local WFAsyncTestCase = require 'WFAsyncTestCase'

local LrmLibraryExportImp = require 'LrmLibraryExportImp'

local WFLogger = require 'WFLogger'

-- WF.core.lua
local KSData = require 'KSData'
local KSIdGenerator = require 'KSIdGenerator'

local exportImpLog = WFLogger 'LrmLibraryExportImpTest'
--:setLevel( 'debug' )

local LrmLibraryExportImpTest = {}

local catalog, renditionServer, OzProfileModule


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

function LrmLibraryExportImpTest.setUp()

    catalog = LrmCatalogTestHelper.makeCatalogService()

end


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

function LrmLibraryExportImpTest.tearDown()

    if catalog then
        catalog:close()
    end

end

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

local function writeTestData( path, stringData )

    local data = KSData.make( stringData )

    path = KSData.saveToUniquelyNamedFile( data, path )
    data:close()
    return path
end

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

function LrmLibraryExportImpTest.testObtainMasterSuccess( test )

    local testAssetId = KSIdGenerator.uuidInLowercaseHex()

    local resultB

    local originalFilePath = LrmCatalogTestHelper.makeTestFilePath()

    originalFilePath = writeTestData( originalFilePath, 'original data' )

    WFAsyncTestCase( test ,1)

    :waitFor( function()

        return catalog:docStore():applyOverlayToDocumentP {

            docType = 'asset',
            docId = testAssetId,
            overlay = {
                subtype = 'image',
                importSource = {
                    originalWidth = 100,
                    originalHeight = 100,
                },
                _localOnly = {
                    originalFilePath = originalFilePath,
                }
            }
        }

    end )


    :waitFor( function()
        resultB =  LrmLibraryExportImp.requestSourceFilesDownload( {catalog = catalog },testAssetId, true)
        return resultB.valueB

    end )

    :continueWhenValueMatches( {
        assetId = testAssetId,
        state = true,
        path = originalFilePath
    } )

end

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


function LrmLibraryExportImpTest.testObtainMasterError( test )

    local testAssetId = KSIdGenerator.uuidInLowercaseHex()

    local resultB

    WFAsyncTestCase( test ,1)

    :waitFor( function()

        return catalog:docStore():applyOverlayToDocumentP {

            docType = 'asset',
            docId = testAssetId,
            overlay = {
                subtype = 'image',
                importSource = {
                    originalWidth = 100,
                    originalHeight = 100,
                },
                _localOnly = {

                }
            }
        }

    end )


    :waitFor( function()
        resultB =  LrmLibraryExportImp.requestSourceFilesDownload( {catalog = catalog },testAssetId, true)
        return resultB.valueB

    end )

    :continueWhenValueMatches( {
        assetId = testAssetId,
        state = false,
        error = 'Original can not be found'
    } )

end

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

return LrmLibraryExportImpTest