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

WFCatSearchSortOnlineHandler.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains 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 are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
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'
local Observable = Rx.Observable
local Subject = Rx.Subject
local WFRxRunUntilDone = require 'WFRxRunUntilDone'

local WFCatSearchOnlineRequest = require "WFCatSearchOnlineRequest"
local WFCatSearchAndSortConstants = require "WFCatSearchAndSortConstants"
--------------------------------------------------------------------------------

local WFCatSearchSortOnlineHandler = {}
------------------------------------------------------------
local _currentOnlineSearchRequest = false

local _wfCatUser = false

------------------------------------------------------------
local function getCatalogInfoDataObservable( wfCatalogModel )
	
	local catalogInfoObservable = wfCatalogModel
			:take( 1 )
			
	local catalogInfoDataObservable = catalogInfoObservable
			:flatMap( function( wfCatatalog ) 
				-- may be test catalog ?
				if wfCatatalog.ozUser then
					_wfCatUser = wfCatatalog:ozUser()
				end
				return Observable.just( wfCatatalog )
			end )
	
	return catalogInfoDataObservable
end

------------------------------------------------------------
local function placeSearchRequest( wfCatalogModel )
	
	local searchSubject = Subject()
	local catalogInfoDataObservable = getCatalogInfoDataObservable( wfCatalogModel )
	
	local searchRequestObservable = catalogInfoDataObservable:
		flatMap( function ( wfCatatalog )
			local url = WFCatSearchAndSortConstants.getSearchUrl()
			return _currentOnlineSearchRequest:executeOnlineSearch( 
							wfCatatalog, _wfCatUser, url, WFCatSearchAndSortConstants.getSearchRequestArgs() )
		end )
		
	local finalObservable = searchRequestObservable:
				tap (
					-- onNext
					function ( )
						local statusData = _currentOnlineSearchRequest:getCurrentStatus()
						searchSubject:onNext( statusData )
					end,
					-- onError
					function ( errorData )
						local statusData = _currentOnlineSearchRequest:getCurrentStatus()
						statusData.errorData = errorData
						searchSubject:onError( statusData )
					end,
					function () 
						_currentOnlineSearchRequest:closeRequest()
						_currentOnlineSearchRequest:dispose()
						
						local statusData = _currentOnlineSearchRequest:getCurrentStatus()
						searchSubject:onNext( statusData )
						
						searchSubject:onCompleted( )
					end
				)
				:takeLast( 1 )
				
		finalObservable	
				:call( WFRxRunUntilDone ) 
				
	return searchSubject
	
end

--------------------------------------------------------------------------------
function WFCatSearchSortOnlineHandler.initializeSearchDetails( catalogModel, userProvidedTerms )
	_currentOnlineSearchRequest = WFCatSearchOnlineRequest( userProvidedTerms )
	return placeSearchRequest(  catalogModel )
end

--------------------------------------------------------------------------------
function WFCatSearchSortOnlineHandler.searchFacetValues( catalogModel, userProvidedTerms )
	
	local catalogInfoDataObservable = getCatalogInfoDataObservable( catalogModel )
	
	local searchRequestObservable = catalogInfoDataObservable
		
		:flatMap( function ( wfCatalog )
			local url = WFCatSearchAndSortConstants.getSearchUrl()
			
			local searchOnlineRequest = WFCatSearchOnlineRequest( userProvidedTerms )
			
			return searchOnlineRequest:executeFacetValuesSearch( 
						wfCatalog, _wfCatUser, url, WFCatSearchAndSortConstants.getSearchRequestArgs() )
		end )
		
		:call( WFRxRunUntilDone ) 
	
	return searchRequestObservable

end

--------------------------------------------------------------------------------
function WFCatSearchSortOnlineHandler.categoryKeys( catalogModel, userProvidedTerms )
	
	local catalogInfoDataObservable = getCatalogInfoDataObservable( catalogModel )
	
	local searchRequestObservable = catalogInfoDataObservable
		
		:flatMap( function ( wfCatalog )
		
			local isStage = userProvidedTerms.isStage
			userProvidedTerms.isStage = nil
			
			local url = WFCatSearchAndSortConstants.getSearchUrl( isStage )
			
			local searchOnlineRequest = WFCatSearchOnlineRequest( userProvidedTerms )
			
			return searchOnlineRequest:executeCategoryKeysSearch( 
						wfCatalog, _wfCatUser, url, WFCatSearchAndSortConstants.getSearchRequestArgs() )
		end )
		
		:call( WFRxRunUntilDone ) 
	
	return { value = searchRequestObservable }

end

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

function WFCatSearchSortOnlineHandler.getAutoCompleteValues( catalogModel, searchParams )
	
	local catalogInfoDataObservable = getCatalogInfoDataObservable( catalogModel )
	
	local searchRequestObservable = catalogInfoDataObservable
		
		:flatMap( function ( wfCatalog )
			local url = WFCatSearchAndSortConstants.getAutoCompleteURL( searchParams )
			
			local searchOnlineRequest = WFCatSearchOnlineRequest( {} )	-- empty body since it is a GET request
			
			return searchOnlineRequest:executeAutoComplete( 
						wfCatalog, _wfCatUser, url, WFCatSearchAndSortConstants.getAutoCompleteRequestArgs() )
		end )
		
		:call( WFRxRunUntilDone ) 
	
	return { value = searchRequestObservable }

end

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

return WFCatSearchSortOnlineHandler
