
--  ParamBuilder  -  https://www.transportfever.net/filebase/index.php?entry/5626-parambuilder/
--  Version 1.1
--  Date: 2020-07-13  /  2020-08-12  /  2020-10-02
--  (c) VacuumTube  -  https://www.transportfever.net/wsc/index.php?user/29264-vacuumtube/

-- Do not edit this file! Otherwise there are mod compatibility issues with different versions
-- Usually located under "res/scripts/parambuilder_v1_1.lua"
-- Support/Feedback/Bug-Thread:  https://www.transportfever.net/index.php?thread/15688-parambuilder-mod-tool-f%C3%BCr-ui-parameter/


local pb = {}


function pb.Slider(key,name,values,defaultIdx,tooltipORvandef,valuesTxt)
	return pb.Builder("SLIDER",key,name,values,defaultIdx,tooltipORvandef,valuesTxt)
end

function pb.Combobox(key,name,values,defaultIdx,tooltip,valuesTxt)
	return pb.Builder("COMBOBOX",key,name,values,defaultIdx,tooltip,valuesTxt)
end

function pb.Button(key,name,values,defaultIdx,tooltip,valuesTxt)
	return pb.Builder("BUTTON",key,name,values,defaultIdx,tooltip,valuesTxt)
end

function pb.IconButton(key,name,icons,values,defaultIdx,tooltip)
	return pb.Builder("ICON_BUTTON",key,name,values or icons,defaultIdx,tooltip,icons)  -- icons are values(txt) here, the icon names given to params.values
end

function pb.Checkbox(key,name,defaultCheck)  -- tooltip not working for CB
	if defaultCheck==nil then
		defaultCheck = true
	end
	assert(type(defaultCheck)=="boolean", "defaultCheck should be boolean")
	return pb.Builder("CHECKBOX",key,name,{"false","true","dummy"},defaultCheck and 1 or 0)
end


function pb.Builder(uiType,key,name,values,defaultIdx,tooltipORmodvandef,valuesTxt)
	local defaultIndex = defaultIdx or 0
	assert(defaultIndex >= 0, "invalid defaultIndex: ".. defaultIndex)
	local defaultBool = defaultIndex > 0
	local defaultValue = values[defaultIndex+1]
	assert(defaultValue, "No valid defaultValue! defaultIndex out of bound?")
	
	local defaultValueTxt = defaultValue
	if valuesTxt then
		assert(#valuesTxt==#values, "values and valuesTxt must have same length!")
		defaultValueTxt = valuesTxt[defaultIndex+1]
	end
	local moddefstr = string.format("%s %s: %s" , "Mod" , _("Default"), defaultValueTxt )
	
	if type(tooltipORmodvandef)=='number' then  -- use for mod settings that change vanilla config values
		local vandefstr = string.format("%s %s: %s\n", "Vanilla", _("Default"), tooltipORmodvandef )
		tooltipORmodvandef = vandefstr..moddefstr
	elseif tooltipORmodvandef==true then  -- show mod default as tooltip
		tooltipORmodvandef = moddefstr
	end
	
	local printmsg = function(...)
		if not pb.dontprint then  -- currently not included. problem: one mod affects all mods
			pb.printkey(key, ...)
		end
	end
	
	return {
		params = {
			uiType = uiType,
			key = key,
			name = name,
			tooltip = tooltipORmodvandef,
			values = valuesTxt or pb.strValues(values),
			defaultIndex = defaultIdx,
		},
		values = values,
		defaultIndex = defaultIndex,
		defaultBool = defaultBool,
		defaultValue = defaultValue,
		defaultValueTxt = defaultValueTxt,
		getIndex = function(params)
			if params then
				local index = params[key]
				if type(index)~="number" then
					printmsg("WARNING: index not type number", "Index: ", index )
				end
				return index
			else
				printmsg("No params", "-> default index:", defaultIndex )
				return defaultIndex
			end
		end,
		getBool = function(params)
			if params then
				local index = params[key]
				if index then
					return index > 0
				else
					printmsg("No key", "-> default bool:", defaultBool )
					return defaultBool
				end
			else
				printmsg("No params", "-> default bool:", defaultBool )
				return defaultBool
			end
		end,
		getValue = function(params)
			if params then
				local index = params[key]
				if index then
					local val = values[index+1]
					if val==nil then
						printmsg("WARNING: No value for given index:", index, " Index out of bound? Duplicate key?")
					end
					return val
				else  -- key not exist. May happen if mod params has other keys, but this key not set yet
					printmsg("No key", "-> default value:", defaultValue )
					return defaultValue
				end
			else  -- should only happen if mod params not set yet
				printmsg("No params", "-> default value:", defaultValue )
				return defaultValue
			end
		end,
	}
end



function pb.range(start,enD,step)
	step = step or 1
	local r = {}
	for i = start,enD+step*0.001,step do  -- floating precision problem: make sure to include end
		table.insert(r, i)
	end
	return r
end

function pb.rangeSymm(enD,step)  -- better than range because of numeric precision around 0
	local r1 = pb.range(0,enD,step)
	local rs = {0}
	for k = 2,#r1 do 
		table.insert(rs, r1[k])
		table.insert(rs, 1, -r1[k])
	end
	return rs
end

function pb.strValues(values)
	local s = {}
	for i,val in pairs(values) do
		s[i] = tostring(val)
	end
	return s
end


function pb.printmod(...)
	print(getCurrentModId(), ...)
end

function pb.printkey(key, ...)
	pb.printmod("key: "..key, ...)
end


return pb