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

WFCatGPSFormatUtilsTest.lua

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

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.sefl_test
local WFRxTestCase = require 'WFRxTestCase'

	-- WF.managed_catalog
local WFCatClientMetadataUtils = require 'WFCatClientMetadataUtils'
local WFCatGPSFormatUtils = require 'WFCatGPSFormatUtils'


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

local function spack( ... )
	return { ... }
end

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

local WFCatGPSFormatUtilsTest = {}

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

function WFCatGPSFormatUtilsTest.rxTestFloatLatLonCoord( rxTest )

	rxTest:expectEqual( 20.54, WFCatGPSFormatUtils.floatLatLonCoord( '20.54E' ) )
	rxTest:expectEqual( -20.1147, WFCatGPSFormatUtils.floatLatLonCoord( '20.1147W' ) )
	rxTest:expectEqual( nil, WFCatGPSFormatUtils.floatLatLonCoord() )
end

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

function WFCatGPSFormatUtilsTest.rxTestNormalizeGPSCoord( rxTest )

	rxTest:expectEqual( -10, WFCatGPSFormatUtils.normalizeGPSCoord( 50, 15 ) )
	rxTest:expectEqual( 10, WFCatGPSFormatUtils.normalizeGPSCoord( -20, 15 ) )
	rxTest:expectEqual( -10, WFCatGPSFormatUtils.normalizeGPSCoord( 20, 15 ) )
end

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

function WFCatGPSFormatUtilsTest.rxTestLatLonCoordFromFloat( rxTest )

	-- Seattle

	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitude=^1^D^2\'^3\" ^4",
			47,
			36,
			"34.92",
			"N",
		},
		WFCatGPSFormatUtils.latLonCoordFromFloat( 47.6097, "lat" ) )
		
	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitude=^1^D^2\'^3\" ^4",
			122,
			19,
			"51.00",
			"W",
		},
		WFCatGPSFormatUtils.latLonCoordFromFloat( -122.3308333, "lon" ) )
	
	-- Amsterdam
		
	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitude=^1^D^2\'^3\" ^4",
			52,
			22,
			"0.12",
			"N",
		},
		WFCatGPSFormatUtils.latLonCoordFromFloat( 52.3667, "lat" ) )
		
	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitude=^1^D^2\'^3\" ^4",
			4,
			54,
			"0.00",
			"E",
		},
		WFCatGPSFormatUtils.latLonCoordFromFloat( 4.9000, "lon" ) )
	
end

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

function WFCatGPSFormatUtilsTest.rxTestXmpLatLonCoordFromFloat( rxTest )

	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitudeXMP=^1,^2^3",
			47,
			"36.582",
			"N",
		},
		WFCatGPSFormatUtils.xmpLatLonCoordFromFloat( 47.6097, "lat" ) )
		
	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitudeXMP=^1,^2^3",
			122,
			"19.849998",
			"W",
		},
		WFCatGPSFormatUtils.xmpLatLonCoordFromFloat( -122.3308333, "lon" ) )
	
	-- Amsterdam
		
	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitudeXMP=^1,^2^3",
			52,
			"22.002",
			"N",
		},
		WFCatGPSFormatUtils.xmpLatLonCoordFromFloat( 52.3667, "lat" ) )
		
	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitudeXMP=^1,^2^3",
			4,
			"54",
			"E",
		},
		WFCatGPSFormatUtils.xmpLatLonCoordFromFloat( 4.9000, "lon" ) )
	
end

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

function WFCatGPSFormatUtilsTest.rxTestNormalizeXmpLatLonCoord( rxTest )

	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitudeXMP=^1,^2^3",
			47,
			"36.582",
			"N",
		},
		WFCatGPSFormatUtils.normalizeXmpLatLonCoord( '47.6097N', "lat" ) )
		
	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitudeXMP=^1,^2^3",
			122,
			"19.849998",
			"W",
		},
		WFCatGPSFormatUtils.normalizeXmpLatLonCoord( '-122.3308333W', "lon" ) )
		
	-- Amsterdam
		
	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitudeXMP=^1,^2^3",
			52,
			"22.002",
			"N",
		},
		WFCatGPSFormatUtils.normalizeXmpLatLonCoord( 52.3667, "lat" ) )
		
	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitudeXMP=^1,^2^3",
			4,
			"54",
			"E",
		},
		WFCatGPSFormatUtils.normalizeXmpLatLonCoord( 4.9000, "lon" ) )
		
end

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

function WFCatGPSFormatUtilsTest.rxTestNormalizeLatLonCoord( rxTest )

	-- Seattle
	
	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitude=^1^D^2\'^3\" ^4",
			47,
			36,
			"34.92",
			"N",
		},
		WFCatGPSFormatUtils.normalizeLatLonCoord( '47.6097N', "lat" ) )
		
	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitude=^1^D^2\'^3\" ^4",
			122,
			19,
			"51.00",
			"W",
		},
		WFCatGPSFormatUtils.normalizeLatLonCoord( '-122.3308333W', "lon" ) )
		
	-- Amsterdam
		
	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitude=^1^D^2\'^3\" ^4",
			52,
			22,
			"0.12",
			"N",
		},
		WFCatGPSFormatUtils.normalizeLatLonCoord( '52.3667N', "lat" ) )
		
	rxTest:expectEqual( 
		{
			"ZSTR",
			"$$$/AgLibrary/Exif/LatitudeLongitude=^1^D^2\'^3\" ^4",
			4,
			54,
			"0.00",
			"E",
		},
		WFCatGPSFormatUtils.normalizeLatLonCoord( '4.9000E', "lon" ) )
		
end

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

function WFCatGPSFormatUtilsTest.rxTestGetElevationOverlay( rxTest )

	rxTest:expectEqual(
		{ location = { altitude = 10 } },
		WFCatGPSFormatUtils.getElevationOverlay( 10 ) )
			
	rxTest:expectEqual(
		{ location = { altitude = { _delete = true } } },
		WFCatGPSFormatUtils.getElevationOverlay() )
			
end

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

function WFCatGPSFormatUtilsTest.rxTestParseElevation( rxTest )

	rxTest:expectEqual( 10, WFCatGPSFormatUtils.parseElevation( 10 ) )
	rxTest:expectEqual( 152.4, WFCatGPSFormatUtils.parseElevation( '500' ) )
	
end

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

function WFCatGPSFormatUtilsTest.rxTestParseLatLong( rxTest )
	
	local deg = string.char( 194 ) .. string.char( 176 )
	
	rxTest:expectEqual( { 23.50002, 12.00007 }, spack( WFCatGPSFormatUtils.parseLatLong( "23.50002, 12.00007" ) ) )
	rxTest:expectEqual( { 23.50002, 12.00007 }, spack( WFCatGPSFormatUtils.parseLatLong( "12.00007E, 23.50002N" ) ) )
	rxTest:expectEqual( { 23.5, 12 }, spack( WFCatGPSFormatUtils.parseLatLong( "23" .. deg .. "30'0\" N 12" .. deg .. "0'0\" E" ) ) )
	rxTest:expectEqual( { 23.5, 12 }, spack( WFCatGPSFormatUtils.parseLatLong( "23" .. deg .. "30'0\"n, 12" .. deg .. "0'0\"e" ) ) )
	rxTest:expectEqual( { 23, 12 }, spack( WFCatGPSFormatUtils.parseLatLong( "23N, 12E" ) ) )
	rxTest:expectEqual( {}, spack( WFCatGPSFormatUtils.parseLatLong( "Invalid" ) ) )
	
end

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

function WFCatGPSFormatUtilsTest.rxTestGetLocationOverlay( rxTest )

	local kDeleted = { _delete = true }
	rxTest:expectEqual(
		{ 
			location = { 
				longitude = 20,
				latitude = 10,
				sublocation = kDeleted,
				city = kDeleted,
				state = kDeleted,
				country = kDeleted,
				isoCountryCode = kDeleted,
			}
		},
		WFCatGPSFormatUtils.getLocationOverlay( 10, 20 ) )
			
	rxTest:expectEqual(
		{ 
			location = { 
				longitude = kDeleted,
				latitude = kDeleted,
			}
		},
		WFCatGPSFormatUtils.getLocationOverlay() )
			
end

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

function WFCatGPSFormatUtilsTest.rxTestGetDirectionOverlay( rxTest )

	rxTest:expectEqual(
		{ location = { direction = 10 } },
		WFCatGPSFormatUtils.getDirectionOverlay( 10 ) )
			
	rxTest:expectEqual(
		{ location = { direction = { _delete = true } } },
		WFCatGPSFormatUtils.getDirectionOverlay() )
			
end

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

function WFCatGPSFormatUtilsTest.rxTestFormatDirections( rxTest )

	rxTest:expectEqual(
		{ 'ZSTR', "$$$/AgLibrary/Exif/Direction/DisplayName=^1", 
			{
				'ZSTR', 
				"$$$/AgLocation/CardinalDirection/North=North", 
			},
		},
		{ WFCatGPSFormatUtils.formatDirection( 10, "cardinal" ) } )
	
	rxTest:expectEqual(
		{ 'ZSTR', "$$$/AgLibrary/Exif/Direction/DisplayDegrees=^1^D", math.floor( 10 + 0.5 ) },
		{ WFCatGPSFormatUtils.formatDirection( 10, "NOT cardinal" ) } )
	
	rxTest:expectEqual( nil, WFCatGPSFormatUtils.formatDirection( "test" ) )
	
end

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

function WFCatGPSFormatUtilsTest.rxTestGPSDirectionRefToFromOzRef( rxTest )
	
	rxTest:expectEqual( 'M', WFCatGPSFormatUtils.convertGPSDirectionRefToOzRef( 'C' ) )
	rxTest:expectEqual( 'T', WFCatGPSFormatUtils.convertGPSDirectionRefToOzRef( 'T' ) )
	rxTest:expectEqual( nil, WFCatGPSFormatUtils.convertGPSDirectionRefToOzRef( 'invalid' ) )
	
	rxTest:expectEqual( 'C', WFCatGPSFormatUtils.convertOzRefToGPSDirectionRef( 'M' ) )
	rxTest:expectEqual( 'T', WFCatGPSFormatUtils.convertOzRefToGPSDirectionRef( 'T' ) )
	rxTest:expectEqual( nil, WFCatGPSFormatUtils.convertOzRefToGPSDirectionRef( 'invalid' ) )

end

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

function WFCatGPSFormatUtilsTest.rxTestFormatGPSCoordinates( rxTest )

	local function convert( lat, lng )
		local formattedValue = { WFCatGPSFormatUtils.formatGPSCoordinates( lat, lng ) }
		return WFCatClientMetadataUtils.localizeValue( formattedValue )
	end
	
	local function val( s1, s2 )
		
		local d1 = s1 > 0 and 'N' or 'S'
		local d2 = s2 > 0 and 'E' or 'W'
	
		local v1 = WFCatClientMetadataUtils.localizeValue { "ZSTR", "$$$/AgLibrary/Exif/LatitudeLongitude=^1^D^2\'^3\" ^4", 30, 30, 4, d1 }
		local v2 = WFCatClientMetadataUtils.localizeValue { "ZSTR", "$$$/AgLibrary/Exif/LatitudeLongitude=^1^D^2\'^3\" ^4", 30, 30, 4, d2 }
		return v1 .. ' ' .. v2
	end

	rxTest:expectEqual( val( 1, 1 ), convert( '30.501', '30.501' ) )
	rxTest:expectEqual( val( 1, -1 ), convert( '30.501', '-30.501' ) )
	rxTest:expectEqual( val( -1, 1 ), convert( '-30.501', '30.501' ) )
	rxTest:expectEqual( val( -1, -1 ), convert( '-30.501', '-30.501' ) )
end

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

function WFCatGPSFormatUtilsTest.rxTestGPSCoordStringToNumber( rxTest )

	rxTest:expectEqual( 30.5, WFCatGPSFormatUtils.GPSCoordStringToNumber( '30,30N', 180 ) )
	rxTest:expectEqual( 149.5, WFCatGPSFormatUtils.GPSCoordStringToNumber( '210,30S', 180 ) )
	rxTest:expectEqual( 30.5, WFCatGPSFormatUtils.GPSCoordStringToNumber( '30,30E', 90 ) )
	rxTest:expectEqual( -30.5, WFCatGPSFormatUtils.GPSCoordStringToNumber( '210,30W', 90 ) )
end

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

return WFRxTestCase.wrapSuite( 'WFCatGPSFormatUtilsTest', WFCatGPSFormatUtilsTest )
