//------------------------------------------------------------------------------
// <copyright file="LqrSMACCSVSampleFileSet.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
//   Declaration of CSVSampleFileSet class, which encapsulates operations of a
//   sensor data logger.
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#ifndef __LqrSMACCSVSampleFileSet_h__
#define __LqrSMACCSVSampleFileSet_h__

#include "LqrSMACCSVSample.h"
#include "LqrSMACFileSet.h"
#include "LqrSMACLockableConcept.h"
#include "LqrSMACSharedLib.h"
#include "LqrSMACTimeRange.h"
#include <map>
#include <ostream>
#include <set>
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/utility.hpp>

namespace LiquidRobotics {
namespace SMACFramework {

class SHARED_LIB_PUBLIC CSVSampleFileSet : public LockableConceptMutex, boost::noncopyable
{
public:

	typedef std::string string;
	typedef boost::shared_ptr<CSVSampleFileSet> Ptr;
	typedef std::set<string> ColumnHeaderSet;
	typedef std::map<string,size_t> ColumnHeaderMapToColNum;
	typedef boost::shared_ptr<ColumnHeaderMapToColNum> ColumnHeaderMapToColNumPtr;
	typedef std::set<string> RecordTypeSet;
	typedef std::map<string,ColumnHeaderMapToColNumPtr> RecordSetToColumnHeaders;
	typedef std::vector<string> StringVector;

	struct FileRecordTypeInfo
	{
		typedef boost::shared_ptr<FileRecordTypeInfo> Ptr;

		string recordType;
		StringVector columnHeadersOrdered;
		ColumnHeaderMapToColNum headerToColNum;
	};

	struct FileRecordTypeToInfoMap : public std::map<string,FileRecordTypeInfo::Ptr>
	{
		void GetFromStream(std::istream& stm);
	};

	typedef boost::shared_ptr<FileRecordTypeToInfoMap> FileRecordTypeToInfoMapPtr;

	CSVSampleFileSet(const FileSetSettings& _settings);
	virtual ~CSVSampleFileSet();

	void Initialize();
	void Shutdown();
	void FilterSamples(const string& recordType, const TimeRange& timeRange, std::ostream& stm);
	void FilterSamples(const ColumnHeaderSet& columnHeaders, const RecordTypeSet& recordTypes, const TimeRange& timeRange, std::ostream& stm);
	bool FilterSamplesPlainTextFile(const ColumnHeaderSet& columnHeaders, const RecordTypeSet& recordTypes, const TimeRange& timeRange, const string& fileName, std::ostream& stm, bool withHeaders = true) const ;
	static timeval GetTimeFromSample(const CSVSample& sample, FileRecordTypeToInfoMap& recordInfo);
	TimeRange GetTimeRangeFromFileContent(const string& fileName) const;
	TimeRange GetTimeRangeFromFileName(const string& fileName) const;
	//static void GetRecordSetColumnHeaders(RecordSetToColumnHeaders& result, std::istream& stm);
	void RegisterSampleHeaders(const string& recordType, CSVSample& data, bool withStandardColumns = true);
	void WriteSample(const string& recordType, const timeval& sampleTime, double latitude, double longitude, CSVSample& data);
	void WriteSample(CSVSample& data);

private:

	typedef LiquidRobotics::SMACFramework::ByteArray::Ptr ByteArrayPtr;
	typedef boost::shared_ptr<StringVector> StringVectorPtr;
	typedef std::map<string,StringVectorPtr> RecordTypeToHeaders;

	void OnFileVolumeComplete(FileSet& sender, const string& oldFileName, string& proposedFileName);
	void OnNewFile(FileSet& sender, const string& newFileName, ByteArray& fileInitData);

	LockableConceptMutex dataMutex;
	FileSetSettings fileSetSettings;
	FileSet::Ptr fileSet;
	RecordTypeToHeaders recordTypeRegistry;
	FileSet::ScopedConnectionPtr fileVolumeCompleteSubscription;
	FileSet::ScopedConnectionPtr newFileSubscription;
};

}
}

#endif
