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

WFCatSearchByAssetType.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2019 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.

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

local KSTableUtils = require 'KSTableUtils'
--------------------------------------------------------------------------------

local derefOpt = KSTableUtils.derefOpt

local isValidSearchableAssetDocument = assert( require 'WFCatIndexUtils'.isValidSearchableAssetDocument )

local _HDRStr = "hdr"
local _PanoramaStr = "panorama"
local _RAWStr = "raw"

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

local WFCatSearchByAssetType = {}

--[[
	Index definitions for finding assets by type.  The SQLite index only;
--]]

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

function WFCatSearchByAssetType.generateIndexSchema( indexStoreSchema )

	indexStoreSchema:addTable {

		sql = [[CREATE TABLE mediaTypes( type )]],

		useLocalId = true,
		
		relevantTypeSubtypeTuples = {
			{ 'asset', '*' },
		},
		
		map = function( docValue, emit )

			local isSearchable, searchableDoc = isValidSearchableAssetDocument( docValue )
			if isSearchable and searchableDoc then
				
				-- add image/video
				local subType = derefOpt( searchableDoc, 'subtype' )
				emit( subType )
				
				-- add HDR
				local isHDR = derefOpt( searchableDoc, 'xmp', 'aux', 'IsMergedHDR' )
				if isHDR then
					emit( _HDRStr )
				end
				
				-- add Panorama
				local isMergedPano = derefOpt( searchableDoc, 'xmp', 'aux', 'IsMergedPanorama' )
				local isPanoShot = false
				local originalWidth = derefOpt( searchableDoc, 'importSource', 'originalWidth' )
				local originalHeight = derefOpt( searchableDoc, 'importSource', 'originalHeight' )
				
				if type( originalWidth ) == 'number' and originalWidth > 0 
					and type( originalHeight ) == 'number' and originalHeight > 0 then
						
						local longEdge, shortEdge = math.max( originalHeight, originalWidth ), math.min( originalHeight, originalWidth )
						local aspectRatio = longEdge / shortEdge
						isPanoShot = aspectRatio >= 2 and longEdge >= 1400
					
				end
				
				if isMergedPano or isPanoShot then
					emit( _PanoramaStr )
				end
				
				-- add RAW
				local contentType = derefOpt( searchableDoc, 'importSource', 'contentType' ) or ''
				local isRAW = string.find( contentType, "application/octet%-stream" ) 
					or string.find( contentType, "image/dng" )
				if isRAW then
					emit( _RAWStr )
				end
				
			end

		end,

	}

	indexStoreSchema:addIndex
		[[CREATE INDEX mediaTypes_type ON mediaTypes( type, docId )]]

end

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

function WFCatSearchByAssetType.addSearchToSearchSchema( searchSchema )

	searchSchema:addSearchableData {
		term = 'asset_types',
		dataType = 'asset_typesTerm',
		sql = [[SELECT docId FROM mediaTypes WHERE type = ?]],
		query_params = {
			'assetType',
		},
	}

end

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

function WFCatSearchByAssetType.collectAssetTypeDetailsOfRequiredAssets( wfCatalog, assetIds )
	
	return wfCatalog:indexStore():query {
		sql = [[SELECT DISTINCT type
                    FROM  TEMP_ids  
                    LEFT JOIN mediaTypes 
                    ON TEMP_ids.docId = mediaTypes.docId]],
		tempIds = assetIds,
		calm = 5,
	}
	
end

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

function WFCatSearchByAssetType.collectAssetTypeDetailsOfAllAssets( wfCatalog )
	
	return wfCatalog:indexStore():query {
		sql = [[ SELECT DISTINCT type FROM mediaTypes]],
		calm = 5,
	}

end

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

return WFCatSearchByAssetType
