recordings = {}

recordings.defaults = {
			trec_Id = 0,
			trec_Status = 0,
			trec_Channel = 0,
			trec_Frequency = 0,
	
			vrec_format = 0,
			vrec_vcodec = 0,
			vrec_acodec = 1,
			vrec_resolution = 4,
			vrec_samplerate = 0,
			vrec_abitrate = 2,
			vrec_vbitrate = 3,
			vrec_framerate = 13,
			vrec_quality = 3,
			vrec_targetdevice = 0,
			trec_Enabled = 1,
			}



function recordings.loadRecordings(filename)
	local recordings = {};
	local rec_info = {};
	local current_recording;

	for line in io.lines(filename or "/mnt/OSD/.recordings") do
		key, value = line:match("^([^%=]+)%=(.*)$");
		if key and value and current_recording then
			current_recording[key] = value;
		else
			recording = line:match("^%[([^%]]+)%]$");
			if recording then
				if recording == "RecordingInfo" then
					current_recording = rec_info;
				elseif recording == "file_info" then
					-- Absolutely nothing
				else
					recordings[recording] = setmetatable({}, { __index = recordingMethods });
					current_recording = recordings[recording];
				end
			end
		end
	end
	return setmetatable(recordings, { __index = recordinglistMethods, info = rec_info }), rec_info;
end
recordinglistMethods = {};

function recordinglistMethods:saveRecordings(file)
	if type(file) == "string" then
		file = io.open(file, "w+");
	end
	if not file then error("Unable to write recording list to file"); end

	file:write("[file_info]\n");
	file:write("version=1\n\n");
	
	file:write("[RecordingInfo]\n");
	for k, v in pairs(getmetatable(self).info) do
		file:write(tostring(k).."="..tostring(v).."\n");
	end
	file:write("\n");
	
	for name, recording in pairs(self) do
		file:write("["..name.."]\n");
		for k, v in pairs(recording) do
			file:write(tostring(k).."="..tostring(v).."\n");
		end
		file:write("\n");
	end

	file:flush();
	file:close();
--[[	print("<table>\n");
	for name, recording in pairs(self) do
		print("<tr><td>",name, "</td><td></td></tr>\n");
		for k, v in pairs(recording) do
			print("<tr><td></td><td>", k, " = ", v, "</td></tr>\n");
		end
	end ]]
	
	-- print("</table>\n"); ]]
end

function recordinglistMethods:newRecording(info)
	local num_recordings = getmetatable(self).info.Count;
	num_recordings = num_recordings + 1;
	local recording_name = string.format("recording%d", num_recordings);
	self[recording_name] = setmetatable({}, { __index = recordingMethods });
	
	for k, v in pairs(recordings.defaults) do
		self[recording_name][k] = v;
	end
	
	if info then
		for k, v in pairs(info) do self[recording_name][k] = v; end
	end
	getmetatable(self).info.Count = num_recordings;
	return self[recording_name], recording_name;
end

function recordinglistMethods:deleteRecording(name)
	if name and self[name] then
		self[name] = nil;
		getmetatable(self).info.Count = getmetatable(self).info.Count - 1;
	else
		error("Attempt to remove a recording that doesn't exist: "..tostring(name));
	end
end

recordingMethods = {};

function recordingMethods:setChannel(channel)
	self["trec_Channel"] = channel;
end

function recordingMethods:setStartTime(time)
	if type(time) == "number" then
		time = os.date("*t", time);
	end
	if type(time) == "table" then
		local now = os.date("*t");
		self["trec_Start-Year"] = time.year or now.year;
		self["trec_Start-Mon"] = time.month or now.month;
		self["trec_Start-Day"] = time.day or now.day;
		self["trec_Start-Hour"] = time.hour or now.hour;
		self["trec_Start-Min"] = time.min or now.min;
	end
end

function recordingMethods:setDuration(hours, minutes)
	self["trec_Length-Hour"] = hours;
	self["trec_Length-Min"] = minutes;
end

function recordingMethods:setEndTime(time)
	error("Unimplemented function, setEndTime()");
end

function recordingMethods:setPath(path)
	self["vrec_recpath"] = path;
end

function recordingMethods:getPath()
	return self["vrec_recpath"];
end

function recordingMethods:getStartTime()
	local t = { year = self["trec_Start-Year"],
			 month = self["trec_Start-Mon"],
			 day = self["trec_Start-Day"],
			 hour = self["trec_Start-Hour"],
			 min = self["trec_Start-Min"] };
	if (t.year and t.month and t.day and t.hour and t.min) then
		return os.time(t);
	end
end

function recordingMethods:getDuration()
	return tonumber(self["trec_Length-Hour"]) or 0, tonumber(self["trec_Length-Min"]) or 0;
end

module "recordings"
