/*
 * (c) 2005 Research Group Dr. Fuhrmann, University of Karlsruhe, Germany
 * All rights reserved.
 *
 * Author:
 *         Stefan Denk <denk@ira.uka.de>
 *
 * $Id: vdrrecording.h,v 1.6 2006/01/10 13:37:20 denk Exp $
 *
 */

#ifndef _VDRRECORDING_H_
#define _VDRRECORDING_H_

// stdlib

#include <string>
#include <list>
using namespace std;

// libc

#include <stdint.h>

// vdr

#include <channels.h>

// vidtrans

#include "fileandpos.h"
#include "task.h"


///\bug doc me
struct recinfo
{
	string channel;
	string title;
	string starttime;
	long unixtime;
};



/** Shadows the information for one recording on disk.
 * Additionally a list of hash values can
 *  - be memorized
 *  - be constructed from viddata-files
 */

class vdrrecording {
	private:
		/** Where is the recording stored
		 * Absolute path to the recording.
		 */
		string dir;
		/// how many files are in the directory?
		int videofilesindir;
		/** Which channel was recorded?
		 */
		cChannel * channel;			// there is a lot more information in cChannel than needed, but aggr_dest needs a cChannel object.
		/** When the recording started?
		 * In seconds since the unix epoch.
		 * May be rounded to a full minute.
		 */
		time_t starttime;
		/** How many seconds the recording took.
		 * In seconds.
		 * May be rounded to a full minute.
		 */
		time_t duration;
	public:
		/** constructor for vdrrecording
		 */
		vdrrecording(string _dir, int _videofilesindir, cChannel * _channel, time_t _starttime, time_t _duration);
		/** returns the directory entry from the vdrrecording-class
		 */
		inline const string& get_dir(void) const { return dir; }
		/** returns the number of videofiles in one recording (vdr.001 - vdr.xyz)
		 */
		inline const int& get_videofilesindir(void) const { return videofilesindir; }
};


/** all things to be done with local recordings.
 * that is, files that are sitting on the local hard disk.
 *
 * this means: 
 *
 * - locating these recordings (entering them in a fitting data structure).
 * - parsing these recordings (extract GOPs + their hash values, the number of pictures and the offsets between the GOPs).
 * - adding this information to the cvideosources data structure.
 * 
 * in regard to the client and server, it may be sensible to integrate a routing like "where to start sending" in this class, too.
 */

class vdrrecordings : public cThread
{
	private:
		list<vdrrecording*> recs;
		/// how many recfiles are sitting in one directory (one recording).
		int howmanyrecfiles(string recdir, bool * existingsnippets);
		/// add a recording to the list of recordings.
		void addrecording(string dir, int videofilesindir, cChannel *cChan, time_t starttime, time_t duration);
		/// retrieve information about a recording from the info.vdr file (Title, Channel fingerprint).
		int getrecinfo(string infofile, struct recinfo *result);
		/// build a channel object from the retrieved Channel fingerPrint (parsed from channels.conf).
		cChannel * buildchannel(string chanprint);
		/// parse a recording (all files in one directory) .
		void parse(string recdir, int recfilecount);
		/// from the parsed data, create one minute blocks to insert into cvideosources.
		void buildminuteblocks(string recdir, int recfilecount, time_t starttime, cChannel * cChan, bool * completedsnippets);
		/// to allow other functions to retrieve recordings added to the list of recordings by addrecording
                inline const list<vdrrecording*>& get_recordingdata(void) const { return recs; }
		/// cut all downloaded snippets of one directory so that they can be replayed by vdr without gaps or overlaps
		int cut(string dir, time_t starttime);
		/// reschedule a missing minute block
		void reschedule(string dir, int number, time_t starttime);
		/// rename all minute blocks after number by incrementing their extension by one.
		void changename(string dir, int number);
		/// process the state of the snippets stored in the respective download directory
		bool processsnippetlist(string dir, bool * completedsnippets);
		/// tell vdr that a task (i.e. a recording) is completely transferred, merged and parsed.
		int done(string recdir, int number, time_t starttime);
		/// pointer to ctasks needed for interactions
		ctasks * _ct;
		/// pointer to cvideosources for publishing content
		cvideosources * _vs;
	public:
		vdrrecordings(ctasks *ct, cvideosources *vs);
		/// iterate through all recordings ScanVideoDir has found at startup.
		void Action();
		/// retrieve the position where the server has to start sending (will start at beginning position of hash1).
		int startsending(uint32_t hash1, uint32_t hash2, uint32_t hash3, fileandpos * result);	
		/// build the name of the next recfile (that is, increment the extension of vdr.xxx by one)
		string getnextrecfile(string file);
		/// parse one single file (a service for clientthread)
		void parseonefile(string filename, int filenumber);
		/// increment the extension of the current recfile by one (some string magic)
		int getnextrecfile(string file, string * result);
};





#endif // _VDRRECORDING_H_
