-- TODO: nastavovani barvy
-- TODO: nastavovani barvy nemeni zapis do Props, kdyz je sec or ocl
-- TODO: Acquire nefunguje dobre - nemaze stary zapis

rollout SectorsRollOut "Sectors" rolledUp:true (
	-- init GUI
	include "$scripts\\pteroTools\\_propGroupUI.ms"
	dropdownList	drpType		"Type:"													Width:144		height:6		align:#center
	group "Portal properties" (
		spinner		spnMaxDist	"Max Active D.:"	range:[0, 65535, 0]	type:#float		FieldWidth:40	offset:[50,0]	align:#right	across:2
		checkbutton	btnTpeMax	"Tape"				images:(fnReturnFilename #tpeIcon)	width:18		height:16		align:#right	offset:[1,0]	toolTip:"Measure Max Active Distance ..."	enabled:false
		spinner		spnHalfDist	"Half Open D.:"		range:[0, 65535, 0]	type:#float		FieldWidth:40	offset:[50,0]	align:#right	across:2
		checkbutton	btnTpeHalf	"Tape"				images:(fnReturnFilename #tpeIcon)	width:18		height:16		align:#right	offset:[1,0]	toolTip:"Measure Half Open Distance ..."	enabled:false
		checkbox	cbxNoFade	"Start Fade:"															offset:[-1,-1]	align:#left		across:3
		spinner		spnFadeDist						range:[0, 65535, 0]	type:#float		FieldWidth:35	offset:[26,0]	align:#right
		checkbutton	btnTpeFade	"Tape"				images:(fnReturnFilename #tpeIcon)	width:18		height:16		align:#right	offset:[0,0]	toolTip:"Measure Start Fade Distance ..."	enabled:false
		colorPicker clrLight	""														width:60		height:60		align:#left
		spinner		spnClrR		"R:"				range:[0, 254, 0]	type:#integer	FieldWidth:35	offset:[0,-64]	align:#right
		spinner		spnClrG		"G:"				range:[0, 254, 0]	type:#integer	FieldWidth:35					align:#right
		spinner		spnClrB		"B:"				range:[0, 254, 0]	type:#integer	FieldWidth:35					align:#right
	)

	-- fnInitRollOut()
	fn fnName		= SectorsRollOut
	fn fnLabel		= "Sectors"
 	fn fnKeyNames = #( "Sector", "Ocluder", "Portal" )
 	fn fnKeyUI = #( #( drpType ), #( drpType ), #( spnMaxDist, spnHalfDist, spnFadeDist, spnClrR, spnClrG, spnClrB ) )
 	fn fnKeyDef = #( #( 0 ), #( 0 ), # ( 0, 0, 0, 0, 0, 0 ) )
 	fn fnDel keyIDX = clrLight.color = color 0 0 0
-- 	fn fnRead = fnColor (fnName()) type:false
	fn fnInitRollOut = (
		fnAppendRollOut (fnName()) (fnLabel())
	)

	-- fnPropsKeyIDX
	fn fnPropsKeyIDX obj =
		if obj != undefined then (
			local pteroBuffer = getUserPropBuffer obj
			local keys	= _keyTableDef (fnKeyNames()) (fnKeyUI()) (fnKeyDef())
			local ret	= 0

			for n = 1 to keys.names.count do
				if (findString pteroBuffer keys.names[n]) != undefined do (
					ret = n
					exit
				)
			return ret
		) else
			return 0

	-- fnApply()
	fn fnApply obj keyIDX type acqObj = (
		local keys		= _keyTableDef (fnKeyNames()) (fnKeyUI()) (fnKeyDef())
		local keyArray	= _keyDef "" "" #("undefined")
		local tmp

		-- FIND KEYIDX
		keyIDX = case type of (
			#read:		fnPropsKeyIDX obj
			#write:		drpType.selection - 1
			#delete:	fnPropsKeyIDX obj
			#acquire:	fnPropsKeyIDX acqObj
			#load:		(fnGetDefs (fnLabel()))[1]
			#save:		drpType.selection - 1
			#none:		return true
		)
		drpType.selection = if type == #delete then 1 else (keyIDX + 1)
		if type != #acquire do (
			if keyIDX <= 0 do (
				keyIDX = 1
				type = #delete
			)
			keyArray = _keyDef keys.names[keyIDX] (keys.names[keyIDX] + "=undefined") #("undefined")
		)

		-- SAVE PRESSET
		if type == #save do (
			----
			if keyIDX <= 2 then
				keys.values[keyIDX][1] = 1
			else
				for n = 1 to keys.controls[keyIDX].count do (
					tmp = fnGetControlValue (fnName()) keys.controls[keyIDX][n]
					if n ==  3 then
						keys.values[keyIDX][n] = if (fnGetControlValue (fnName()) cbxNoFade) == 0 then -1 else (if tmp >= 0 then tmp else 0)
					else
						keys.values[keyIDX][n] = tmp
				)
			insertItem keyIDX keys.values[keyIDX] 1
			----
			fnSetDefs (fnLabel()) (keys.values[keyIDX] as string)
			return true
		)

		if obj != undefined do (
			-- DELETE WRONG ENTRY
			if type == #write or btnAuto.checked and type == #load or btnAuto.checked and type == #acquire do (
				for n = 1 to keys.names.count do (
					if n != keyIDX do fnDeleteProp obj keys.names[n]
				)
				if keyIDX <= 0 do (
					keyIDX = 1
					type = #delete
				)
			)
			-- DELETE
			if type == #delete do (
				fnDeleteProp obj keys.names[keyIDX]
				type = #read
			)
			-- ACQUIRE or READ
			keyArray = fnReadProp (if type == #acquire then acqObj else obj) keys.names[keyIDX]
		)

		-- UPDATE UI
		if type == #read or type == #load or type == #acquire do (
			-- LOAD PRESSET
			if type == #load do keyArray.values = fnGetDefs (fnLabel())
			----
			if keyArray.values.count > keys.controls[keyIDX].count or keyIDX <= 2 do deleteItem keyArray.values 1
			for n = 1 to keys.controls[3].count do (
				if (keyArray.values[n] == "") or (keyArray.values[n] as string == "undefined") or (keyArray.values[n] == undefined) do
					keyArray.values[n] = keys.values[3][n]
				if n == 3 do case of (
					(keyArray.values[n] >= 0): fnSetControlValue (fnName()) cbxNoFade 1
					(keyArray.values[n] < 0 or keyIDX != 3): fnSetControlValue (fnName()) cbxNoFade 0
				)
				fnSetControlValue (fnName()) keys.controls[3][n] keyArray.values[n]
			)
			----
			if type == #read do return true
			if btnAuto.checked do type = #write
		)

		-- WRITE
		if type == #write do (
			keyArray.keyName = keys.names[keyIDX]
			----
			if keyIDX <= 2 then
				keyArray.values = #("1")
			else (
				type = #delete
				for n = 1 to keys.controls[keyIDX].count do (
					tmp = fnGetControlValue (fnName()) keys.controls[keyIDX][n]
					if n == 3 then
						keyArray.values[n] = if (fnGetControlValue (fnName()) cbxNoFade) == 0 then "-1" else (if tmp >= 0 then tmp as string else "0")
					else
						keyArray.values[n] = tmp as string
					if keys.controls[3][n].enabled and keys.controls[3][n].value > 1 do type = #write
				)
			)
			----
			if obj != undefined do
				if type == #delete then
					fnDeleteProp obj keys.names[keyIDX]
				else
					fnWriteprop obj keyArray
			return true
		)
	)

	-- fnUpdate()
	fn fnUpdate keyIDX:1 type:#none acqObj:undefined inner:true = (
		if btnAuto.checked and type == #none do type = #write

		if not inner do
			case keyIDX of (
				1: ()
				default: return true
			)

		case selection.count of (
			0:		 fnApply undefined keyIDX type acqObj
			default: for obj in selection do fnApply obj keyIDX type acqObj
		)

		if inner or type == #acquire do _mainRollout.name.fnUtilize (fnName()) info:true

		for n = 3 to (fnKeyUI())[3].count do
			(fnKeyUI())[3][n].enabled = cbxNoFade.checked
		clrLight.enabled = cbxNoFade.checked

		fnColor (fnName()) type:false keyIDX:keyIDX
	)

	-- fnCheckDistances()
	fn fnCheckDistances = (
		if spnHalfDist.value > spnMaxDist.value do spnMaxDist.value = spnHalfDist.value
		if cbxNoFade.checked and spnFadeDist.value > spnMaxDist.value do spnMaxDist.value = spnFadeDist.value
		drpType.selection = 3 + 1
		
		fnUpdate()
	)
	
	-- Event Handler
	on drpType			selected state	do if btnAuto.checked do fnUpdate()
	on cbxNoFade		changed value	do fnCheckDistances()
	on spnMaxDist		entered			do fnCheckDistances()
	on spnHalfDist		entered			do fnCheckDistances()
	on spnFadeDist		entered			do fnCheckDistances()
	on spnClrR			entered			do (fnColor (fnName()) type:false keyIDX:3; drpType.selection = 3 + 1; fnUpdate())
	on spnClrG			entered			do (fnColor (fnName()) type:false keyIDX:3; drpType.selection = 3 + 1; fnUpdate())
	on spnClrB			entered			do (fnColor (fnName()) type:false keyIDX:3; drpType.selection = 3 + 1; fnUpdate())
	on clrLight			changed value	do (fnColor (fnName()) val:value keyIDX:3; drpType.selection = 3 + 1; fnUpdate())
	on btnAcq			changed state	do fnAcquire (fnName()) keys:((fnKeyNames()).count)
	on btnSet			pressed			do fnUpdate type:#write
	on btnDef			pressed			do fnUpdate type:#load
	on btnSav			pressed			do fnUpdate type:#save
	on btnDel			pressed			do fnUpdate type:#delete
	on SectorsRollOut	open			do (
		drpType.items	= #("- None -", (fnKeyNames())[1], (fnKeyNames())[2], (fnKeyNames())[3])
		fnOpenRollOut (fnName())
	)
	on SectorsRollOut	close			do fnCloseRollOut (fnName())
)
