-- ui for customisation of selection bbox

r2.CustomBBox =
{
	UIPath = "ui:interface:r2ed_bbox_edit",
	FirstDisplay = true,
	-- tmp, hardcoded stuff here
	SavePath = "data_common\\r2\\r2_ui_custom_boxes_data.lua",
	Boxes = {}, -- boxes, sorted by sheet ids
	SliderMaxRange = 7;	
}



-- convert coordinate to slider unit
function r2.CustomBBox:coordToSliderValue(coord)
	return (coord / self.SliderMaxRange) * 5000 + 5000
end

-- convert slider value to coordinates
function r2.CustomBBox:sliderValueToCoord(sliderValue)
	return self.SliderMaxRange * ((sliderValue - 5000) / 5000)
end


function r2.CustomBBox:getWindow()
	return getUI(self.UIPath)
end

-- update custom bbox dialog from the current selection
function r2.CustomBBox:updateUI()		
	local ui = self:getWindow()
	if ui.active == false then return end
	local selectedInstance = r2:getSelectedInstance()
	-- show one of the 3 states of the window (no selection, no custom bbox for the selection, or the bbox itself)
	local function setActive(noSelection, noBBox, bbox)
		ui:find("no_selected_bbox").active = noSelection
		ui:find("no_custom_bbox").active = noBBox
		ui:find("custom_bbox").active = bbox
	end
	--
	if selectedInstance == nil then
		setActive(true, false, false)		
		return
	end
	local sheet = selectedInstance.SheetClient
	debugInfo("sheet = " .. tostring(sheet))
	if sheet == nil then
		setActive(false, true, false)
		return
	end
	setActive(false, false, true)	
	--
	local function setCustomBBoxEnabledInUI(active)
		debugInfo("setCustomBBoxEnabledInUI : " .. tostring(active))
		ui:find("x_min").slider.active = active
		ui:find("x_max").slider.active = active
		ui:find("y_min").slider.active = active
		ui:find("y_max").slider.active = active
		ui:find("z_min").slider.active = active
		ui:find("z_max").slider.active = active
		ui:find("custom_bbox_enabled").pushed = active
		local textColor = select(active, CRGBA(255, 255, 255), CRGBA(127, 127, 127, 127))
		ui:find("x_min_text").color_rgba = textColor
		ui:find("x_max_text").color_rgba = textColor
		ui:find("y_min_text").color_rgba = textColor
		ui:find("y_max_text").color_rgba = textColor
		ui:find("z_min_text").color_rgba = textColor
		ui:find("z_max_text").color_rgba = textColor		
	end
	--	
	local currBBox = self.Boxes[sheet]
	if currBBox == nil then
		-- no custom bbox defined
		setCustomBBoxEnabledInUI(false)
		return
	end
	if not currBBox.Enabled then
		setCustomBBoxEnabledInUI(false)
		return
	end
	setCustomBBoxEnabledInUI(true)
	debugInfo('*' .. tostring(sheet))	
	ui:find("x_min").slider.value = self:coordToSliderValue(currBBox.XMin)
	ui:find("x_max").slider.value = self:coordToSliderValue(currBBox.XMax)
	ui:find("y_min").slider.value = self:coordToSliderValue(currBBox.YMin)
	ui:find("y_max").slider.value = self:coordToSliderValue(currBBox.YMax)
	ui:find("z_min").slider.value = self:coordToSliderValue(currBBox.ZMin)
	ui:find("z_max").slider.value = self:coordToSliderValue(currBBox.ZMax)
end

-- update current bbox parameters for the selection from the ui
function r2.CustomBBox:updateFromUI()	
	local selectedInstance = r2:getSelectedInstance()
	if selectedInstance == nil then		
		return
	end
	local sheet = selectedInstance.SheetClient
	if sheet == nil then		
		return
	end
	local ui = self:getWindow()
	local box = {}	
	box.XMin = self:sliderValueToCoord(ui:find("x_min").slider.value)
	box.XMax = self:sliderValueToCoord(ui:find("x_max").slider.value)
	box.XMin = math.min(box.XMin, box.XMax)
	box.YMin = self:sliderValueToCoord(ui:find("y_min").slider.value)
	box.YMax = self:sliderValueToCoord(ui:find("y_max").slider.value)
	box.YMin = math.min(box.YMin, box.YMax)
	box.ZMin = self:sliderValueToCoord(ui:find("z_min").slider.value)
	box.ZMax = self:sliderValueToCoord(ui:find("z_max").slider.value)
	box.ZMin = math.min(box.ZMin, box.ZMax)
	box.Enabled = ui:find("custom_bbox_enabled").pushed
	if not box.Enabled and self.Boxes[sheet] == nil then
		return -- no custom bbox set yet
	end	
	if box.Enabled then
		if self.Boxes[sheet] == nil then
			debugInfo('assigning new custom bbox')
			box.XMin = -0.5
			box.XMax = 0.5
			box.YMin = -0.5
			box.YMax = 0.5
			box.ZMin = 0
			box.ZMax = 2			
		end	
	end
	self.Boxes[sheet] = box
	r2:setEntityCustomSelectBox(sheet, box)
	self:updateUI()
end

-- save all bbox
function r2.CustomBBox:save()
-- tmp : hardcoded path here for now ...
	local stream = io.open(self.SavePath, "w")
	assert(stream)
	stream:write("--***************************************************************--\r\n")
	stream:write("-- this file was generated by the r2 editor, do not edit !!!!!!  --\r\n")
	stream:write("--***************************************************************--\r\n")
	for k, box in pairs(self.Boxes) do
		if box.Enabled then
			stream:write(string.format("\r\nr2.CustomBBox.Boxes['%s'] = { Enabled = true, XMin = %f, XMax = %f, YMin = %f, YMax = %f, ZMin = %f, ZMax = %f }",
				                        k, box.XMin, box.XMax, box.YMin, box.YMax, box.ZMin, box.ZMax))
		end
	end
	stream:close()
	debugInfo("Custom bbox save successful")
end

function r2.CustomBBox:load()
	r2.doFile(self.SavePath)
	for sheet, box in pairs(self.Boxes) do
		r2:setEntityCustomSelectBox(sheet, box)
	end
end
	