-- soda sweets 2 theme options (self-contained)
-- based on scripts by aj kelly; ad-hoc userprefs by asg

envTable = GAMESTATE:Env();
-- env
function setenv(name,value) envTable[name] = value; end;
function getenv(name) return envTable[name]; end;

-- file (assume stepmania 4 alpha 5)
function WriteFile(path,buf)
	local f = RageFileUtil.CreateRageFile()
	if f:Open(path, 2) then
		f:Write( tostring(buf) )
		f:destroy()
		return true
	else
		Trace( "[FileUtils] Error writing to ".. path ..": ".. f:GetError() );
		f:ClearError();
		f:destroy()
		return false
	end
end;

function ReadFile(path)
	local f = RageFileUtil.CreateRageFile()
	local ret = ""
	if f:Open(path, 1) then
		ret = tostring( f:Read() )
		f:destroy()
		return ret
	else
		Trace( "[FileUtils] Error reading from ".. path ..": ".. f:GetError() );
		f:ClearError();
		f:destroy()
		return nil
	end
end;

local prefBasePath = "Data/UserPrefs/sodasweets2/";

-- ad-hoc userprefs implementation
function SetAdhocPref(pref,val)
	WriteFile(prefBasePath..pref,val);
	setenv(pref,val);
end;

function GetAdhocPref(pref)
	if getenv(pref) ~= nil then
		return getenv(pref);
	else
		local res = ReadFile(prefBasePath..pref);
		return res;
	end;
end;

function InitPrefs()
	local prefs = {
		Enable3DModels = true;
	}
	for k,v in pairs(prefs) do
		if not GetAdhocPref(k) then
			SetAdhocPref(k,v);
		end;
	end;
end;

-- begin option rows here!
function OptionRow3DModels()
	local t = {
		Name = "Enable3DModels";
		LayoutType = "ShowAllInRow";
		SelectType = "SelectOne";
		OneChoiceForAllPlayers = true;
		ExportOnChange = false;
		Choices = { 'Off','On' };
		LoadSelections = function(self, list, pn)
			local pname = ToEnumShortString(pn);
			local curSet = GetAdhocPref("Enable3DModels");
			if curSet then
				list[2] = true;
			else
				list[1] = true;
			end;
		end;
		SaveSelections = function(self, list, pn)
			local val = list[2] and true or false;
			SetAdhocPref("Enable3DModels",val);
		end;
	};
	setmetatable( t, t );
	return t;
end;