---------------------------------------------------------------------------------------------------
local SLC_STEWARD_TYPE = 0
local SLC_PHOTOGRAPHER_TYPE = 1
local SLC_CAMERAMAN_STANDING_TYPE = 2
local SLC_CAMERAMAN_PLATFORM_TYPE = 3
local SLC_MEDICAL_TYPE = 4
local SLC_BALLBOY_TYPE = 5
local SLC_GENERIC_TYPE = 6
local SLC_POLICE_TYPE = 7
local SLC_BENCH_PLAYER_HOME_TYPE = 8
local SLC_BENCH_PLAYER_AWAY_TYPE = 9
local SLC_MANAGER_HOME_TYPE = 10
local SLC_MANAGER_AWAY_TYPE = 11

---------------------------------------------------------------------------------------------------
local VARIATIONS = {
	  [SLC_STEWARD_TYPE]                =      { name="steward",          skintone = { 0, 1 }, bodytype = { 0, 1 }, suittype = { 0, 1 }, suitvariation = { 0, 1 } },
	  [SLC_PHOTOGRAPHER_TYPE]           =      { name="photographer",     skintone = { 0, 1 }, bodytype = { 0, 1 }, suittype = { 0, 1 }, suitvariation = { 0, 3 } },
	  [SLC_CAMERAMAN_STANDING_TYPE]     =      { name="cameraman",        skintone = { 0, 1 }, bodytype = { 0, 1 }, suittype = { 0, 1 }, suitvariation = { 0, 1 } },
	  [SLC_CAMERAMAN_PLATFORM_TYPE]     =      { name="cameraman",        skintone = { 0, 1 }, bodytype = { 0, 1 }, suittype = { 1, 1 }, suitvariation = { 0, 1 } },
	  [SLC_MEDICAL_TYPE]                =      { name="medic",            skintone = { 0, 1 }, bodytype = { 0, 1 }, suittype = { 0, 1 }, suitvariation = { 0, 1 } },
	  [SLC_BALLBOY_TYPE]                =      { name="ballboy",          skintone = { 0, 1 }, bodytype = { 0, 1 }, suittype = { 0, 1 }, suitvariation = { 0, 1 } },
	  [SLC_GENERIC_TYPE]                =      { name="generic",          skintone = { 0, 1 }, bodytype = { 0, 1 }, suittype = { 0, 1 }, suitvariation = { 0, 1 } },
	  [SLC_POLICE_TYPE]                 =      { name="policeofficer",    skintone = { 0, 1 }, bodytype = { 0, 1 }, suittype = { 1, 9 }, suitvariation = { 0, 1 } },
	  [SLC_BENCH_PLAYER_HOME_TYPE]      =      { name="benchplayer",      skintone = { 1, 10 }, bodytype = { 0, 1 }, suittype = { 0, 1 }, suitvariation = { 0, 1 } },
	  [SLC_BENCH_PLAYER_AWAY_TYPE]      =      { name="benchplayer",      skintone = { 1, 10 }, bodytype = { 0, 1 }, suittype = { 0, 1 }, suitvariation = { 0, 1 } },
	  [SLC_MANAGER_HOME_TYPE]           =      { name="manager",          skintone = { 1, 10 }, bodytype = { 0, 3 }, suittype = { 0, 3 }, suitvariation = { 0, 2 } },
	  [SLC_MANAGER_AWAY_TYPE]           =      { name="manager",          skintone = { 1, 10 }, bodytype = { 0, 3 }, suittype = { 0, 3 }, suitvariation = { 0, 2 } }
}

---------------------------------------------------------------------------------------------------
function SleUpdate(idx)
	local as = gSportsRNA
	local sle = as:GetTable("wvSle", idx)
	local state = as:GetTable("wvState") -- to get the lighting and weather
	local stadium = as:GetTable("wvStadium", 1) -- to get the policeType only in BE stadiums
	
	-- need to bring closing color for bench player
	db.sle[idx].envLighting = as:GetInt(state, "wvAttribEnvLighting")
	db.sle[idx].stadiumLightID = as:GetInt(state, "wvAttribStadLightID")
	db.sle[idx].stadiumID = as:GetInt(state, "wvAttribStadID")
	local weather = as:GetInt(state, "wvAttribStadWeather" )
	if (weather == 1 or weather == 2) then
		db.sle[idx].wet = 1
	else
		db.sle[idx].wet = 0
	end

  if (weather == 1 or weather == 2) then
  
  if (name=="manager") then
  db.sle[idx].wet = 0
  end
  
  
  end
  
	-- asset naming rules
	--  mesh_suittype_bodytype_wet.rx3
	--  tex_suittype_skintone_suitvariation_wet_textures.rx3

	-- parameters
	--  skintone
	--  bodytype
	--  suittype
	--  suitvariation
	--  customcolor
		  
	local sletype = as:GetInt(sle, "sletype")
	if ( sletype == nil or sletype < SLC_STEWARD_TYPE or sletype > SLC_MANAGER_AWAY_TYPE ) then
		gSportsRNA:Assert( 0, "Invalid SLE["..idx.."] type ["..sletype.."]" )
		db.sle[idx].slename = "generic"
		db.sle[idx].skintone = 0
		db.sle[idx].bodytype = 0
		db.sle[idx].suittype = 0
		db.sle[idx].suitvariation = 0
		db.sle[idx].customcolor = 0
		return
	end
	local variation = VARIATIONS[ sletype ]
	
	local base_suittype = as:GetInt(sle, "suittype")
	if ( sletype == SLC_POLICE_TYPE ) then
		  base_suittype = as:GetInt(stadium, "policeType")
	end

	local customcolor = as:GetInt(sle, "customcolor")
	if ( customcolor == -1 ) then
		  if ( sletype == SLC_BENCH_PLAYER_HOME_TYPE or sletype == SLC_MANAGER_HOME_TYPE ) then
				 customcolor = as:GetInt(stadium, "homeKitColour")
		  elseif ( sletype == SLC_BENCH_PLAYER_AWAY_TYPE or sletype == SLC_MANAGER_AWAY_TYPE ) then
				 customcolor = as:GetInt(stadium, "awayKitColour")
		  end
	end

	db.sle[idx].slename = variation.name
	db.sle[idx].skintone = GetVariation( as:GetInt(sle, "skintone"), variation.skintone[1], variation.skintone[2] )
	db.sle[idx].bodytype = GetVariation( as:GetInt(sle, "bodytype"), variation.bodytype[1], variation.bodytype[2] )
	db.sle[idx].suittype = GetVariation( base_suittype, variation.suittype[1], variation.suittype[2] )
	db.sle[idx].suitvariation = GetVariation( as:GetInt(sle, "suitvariation"), variation.suitvariation[1], variation.suitvariation[2] )
	db.sle[idx].customcolor = customcolor

	if ( sletype == SLC_CAMERAMAN_PLATFORM_TYPE ) then
		db.sle[idx].attribmatname = "cameraman_static"
	else
		db.sle[idx].attribmatname = db.sle[idx].slename
	end
	
	if ( db.sle[idx].skintone == 1 or db.sle[idx].skintone == 3 or db.sle[idx].skintone == 7) then
		db.sle[idx].skintone = db.sle[idx].skintone + 1
	end
	
	
	
	local wipe3d = as:GetTable("wvWipe", 1)
	db.sle[idx].tournamentid = as:GetInt(wipe3d, "leagueID")
	db.sle[idx].tournamentid = getTournamentGraphics(db.sle[idx].tournamentid)

	
	db.sle[idx].teamid = as:GetInt(stadium, "homeKitTeamID" )
	
	if ((sletype == SLC_BENCH_PLAYER_AWAY_TYPE) or (sletype == SLC_MANAGER_AWAY_TYPE)) then
	db.sle[idx].teamid = as:GetInt(stadium, "awayKitTeamID" )
	end

	
	db.sle[idx].varient = 0
	
	if ((sletype == SLC_BENCH_PLAYER_HOME_TYPE) or (sletype == SLC_BENCH_PLAYER_AWAY_TYPE)) then
	db.sle[idx].varient = idx % 3
	end
	
	--gSportsRNA:Print( "SLC("..idx..", "..sletype..", "..as:GetInt(sle, "sleindex").."): '"..db.sle[idx].slename.."' ["..db.sle[idx].skintone..", "..db.sle[idx].bodytype..", "..db.sle[idx].suittype..", "..db.sle[idx].suitvariation..", "..db.sle[idx].customcolor.."]" )
end

---------------------------------------------------------------------------------------------------
-- Get Variation based on initial value, base offset and num variations
function GetVariation( value, offset, numVariants )
	local var = value
	if ( var < 0 ) then
		var = math.random( offset, offset + numVariants - 1 )
		var = math.max( offset, math.min( offset + numVariants - 1, var ) )
	end
	return var
end

---------------------------------------------------------------------------------------------------
function SleAssetBind(sle)
	local gr = gRenderables
	local lod = 0

	gr:AddCallback(sle, lod, "SleUpdate(?)")
	gr:AddAsset(sle, lod, "shader", "data/fifarna/shader.big")
	
	-- SLEs sit in different directories for each type. cameraman, photographer, etc
	-- Need a table of directory names matched up with model types to pick right directory
	-- Also need model number to get the right model for each type
	
	gr:AddAsset(sle, lod, "slemesh", "${GetRMSle(?,0)}data/sceneassets/${db.sle[?].slename}/${db.sle[?].slename}_${db.sle[?].suittype}_${db.sle[?].bodytype}_${db.sle[?].wet}.rx3")
	gr:AddAsset(sle, lod, "sletex", "${GetRMSle(?,1)}data/sceneassets/${db.sle[?].slename}/${db.sle[?].slename}_${db.sle[?].suittype}_${db.sle[?].skintone}_${db.sle[?].suitvariation}_${db.sle[?].wet}_textures.rx3")
	gr:AddAsset(sle, lod, "charcmn", "data/sceneassets/charactercmn/charactercmn_${db.sle[?].envLighting}.rx3")	
	
	local part = "slemesh"
	gr:CreateMaterialFromAttribulator(sle, lod, part, "${db.sle[?].slename}_material", "${db.sle[?].attribmatname}" )
	gr:SetTexture(sle, lod, part, "textures", "diffuseTexture", "sletex", "${db.sle[?].slename}_")
	--gr:SetTexture(sle, lod, part, "textures", "diffuseTexture", "sletex", "")
	gr:SetTexture(sle, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(sle, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	--gr:SetConstantARGB(sle, lod, part, "global", "clothingColour", "${db.sle[?].customcolor}")
	gr:SetTextureFromRuntime(sle, lod, part, "textures", "coverageMap", "covmap_${db.sle[?].stadiumID}_${db.sle[?].stadiumLightID}")

	-- shadow asset
	gr:AddCallback(sle, 1, "SleUpdate(?)")
	gr:AddAsset(sle, 1, "shader", "data/fifarna/shader.big")
	gr:AddAsset(sle, 1, "shadow", "data/sceneassets/crowd/crowd_shadow_0.rx3")
	gr:CreateMaterial(sle, 1, "shadow", "missingShader.fx")
	
	return sle
end

---------------------------------------------------------------------------------------------------
local DEFAULTSLETYPEMAP = {
	[0] = 	{ stype = SLC_STEWARD_TYPE, 		snum = 25 },
	[1] = 	{ stype = SLC_PHOTOGRAPHER_TYPE, 	snum = 26 },
	[2] = 	{ stype = SLC_CAMERAMAN_STANDING_TYPE, 	snum = 5 },
	[3] = 	{ stype = SLC_CAMERAMAN_PLATFORM_TYPE, 	snum = 5 },
	[4] = 	{ stype = SLC_MEDICAL_TYPE, 		snum = 4 },
	[5] = 	{ stype = SLC_BALLBOY_TYPE, 		snum = 8 },
	[6] = 	{ stype = SLC_GENERIC_TYPE, 		snum = 5 },
	[7] = 	{ stype = SLC_POLICE_TYPE, 		snum = 17 },
	[8] = 	{ stype = SLC_BENCH_PLAYER_HOME_TYPE, 	snum = 3 },
	[9] = 	{ stype = SLC_BENCH_PLAYER_AWAY_TYPE, 	snum = 3 },
	[10] = 	{ stype = SLC_MANAGER_HOME_TYPE, 	snum = 1 },
	[11] = 	{ stype = SLC_MANAGER_AWAY_TYPE, 	snum = 1 }
}

local SUPERLOWSLETYPEMAP = {
	[0] = 	{ stype = SLC_STEWARD_TYPE, 		snum = 0 },
	[1] = 	{ stype = SLC_PHOTOGRAPHER_TYPE, 	snum = 0 },
	[2] = 	{ stype = SLC_CAMERAMAN_STANDING_TYPE, 	snum = 0 },
	[3] = 	{ stype = SLC_CAMERAMAN_PLATFORM_TYPE, 	snum = 0 },
	[4] = 	{ stype = SLC_MEDICAL_TYPE, 		snum = 0 },
	[5] = 	{ stype = SLC_BALLBOY_TYPE, 		snum = 0 },
	[6] = 	{ stype = SLC_GENERIC_TYPE, 		snum = 0 },
	[7] = 	{ stype = SLC_POLICE_TYPE, 		snum = 0 },
	[8] = 	{ stype = SLC_BENCH_PLAYER_HOME_TYPE, 	snum = 0 },
	[9] = 	{ stype = SLC_BENCH_PLAYER_AWAY_TYPE, 	snum = 0 },
	[10] = 	{ stype = SLC_MANAGER_HOME_TYPE, 	snum = 1 },
	[11] = 	{ stype = SLC_MANAGER_AWAY_TYPE, 	snum = 1 }
}

---------------------------------------------------------------------------------------------------
-- Create sle renderables with type assignment
function AssignSleTypes( gr, batch, sles, numSles )

	local idx = 0
	
	local sleTypeMap = DEFAULTSLETYPEMAP
	local as = gSportsRNA
	local settingTable = as:GetTable("Settings")
	local levelOfDetail = as:GetString(settingTable, "LevelOfDetail") 
	if (levelOfDetail == "superlow") then
		sleTypeMap = SUPERLOWSLETYPEMAP
	end
	
	for id, map in pairs(sleTypeMap) do
	
		for i = 0, map.snum - 1 do
			gr:AddSubObject(batch, sles[idx], map.stype)

			idx = idx + 1
			if idx == numSles then
				break
			end
		end

		if idx == numSles then
			break
		end

	end

	for i = idx, numSles - 1 do
		gr:AddSubObject(batch, sles[i], SLC_GENERIC_TYPE)
	end
	
end


function GetRMSle(idx,isModel)
	local sleorder = ""
	local fileType = ""
	
	if (isModel == 1) then
	fileType = "_textures"
	end
	
	sleorder = sleorder.."data/sceneassets/"..db.sle[idx].slename.."/specific"..db.sle[idx].slename.."_"..db.sle[idx].teamid.."_"..db.sle[idx].tournamentid.."_"..db.sle[idx].varient..""..fileType..".rx3;"
	sleorder = sleorder.."data/sceneassets/"..db.sle[idx].slename.."/specific"..db.sle[idx].slename.."_"..db.sle[idx].teamid.."_"..db.sle[idx].tournamentid.."_0"..fileType..".rx3;"
	sleorder = sleorder.."data/sceneassets/"..db.sle[idx].slename.."/specific"..db.sle[idx].slename.."_"..db.sle[idx].teamid.."_0_"..db.sle[idx].varient..""..fileType..".rx3;"
	sleorder = sleorder.."data/sceneassets/"..db.sle[idx].slename.."/specific"..db.sle[idx].slename.."_"..db.sle[idx].teamid.."_0_0"..fileType..".rx3;"
	sleorder = sleorder.."data/sceneassets/"..db.sle[idx].slename.."/specific"..db.sle[idx].slename.."_0_"..db.sle[idx].tournamentid.."_"..db.sle[idx].varient..""..fileType..".rx3;"
	sleorder = sleorder.."data/sceneassets/"..db.sle[idx].slename.."/specific"..db.sle[idx].slename.."_0_"..db.sle[idx].tournamentid.."_0"..fileType..".rx3;"
	sleorder = sleorder.."data/sceneassets/"..db.sle[idx].slename.."/specific"..db.sle[idx].slename.."_0_0_"..db.sle[idx].varient..""..fileType..".rx3;"
	sleorder = sleorder.."data/sceneassets/"..db.sle[idx].slename.."/specific"..db.sle[idx].slename.."_0_0_0"..fileType..".rx3;"
	return sleorder
end

--Revolution Mod 14 1.1
--Edited by scouser09