/*****************************************************************************
 *
 * Copyright (c) 2018-19, Zeeshan Islam, Paula Herber, Josef Treus
 * Westfälische Wilhelms-Universität Münster, AG Embedded Systems 
 * All rights reserved.
 *
 * This file is part of SysCSec
 *
 * SysCSec is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * SysCSec is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SysCSec.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *  Please report any problems or bugs to: sysc.sec@uni-muenster.de
 *
 ****************************************************************************/


#ifndef UTILS_H
#define UTILS_H

#include <iostream>
#include <stdint.h>
#include <list>

#include <iostream>
#include <fstream>
#include <cassert>
#include <random>
#include <chrono>

using namespace std;

class Utils {
private:
public:

	/**
	 * Return random string with requested 'length'.
	 */
	static string randomString(const int length) {
		const char chars[] =
				"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		string s = "";
		for (int i = 0; i < length; ++i) {
			s += chars[rand() % (sizeof(chars) - 1)];
		}
		return s;
	}

	/**
	 * Creates a .csv file from given 'header' and 'fieldsList'.
	 */
	/**
	static void writeToCSVFile(list<string> header,
			map<list<int>> fieldsList) {
		assert(header.size() > 0);
		assert(fieldsList.size() > 0);

		string fieldSeperator = ",";
		string columnSeperator = "\n";

		ofstream myfile;
		myfile.open("data.csv");

		for (auto it = header.begin(); it != header.end(); it++) {
			string columnHeader = *it;
			myfile << columnHeader << fieldSeperator;
		}
		myfile << columnSeperator;

		for (auto it = fieldsList.begin(); it != fieldsList.end(); it++) {
			list<int> fields = *it;
			for (auto it2 = fields.begin(); it2 != fields.end(); it2++) {
				int field = *it2;
				myfile << field << fieldSeperator;
			}
			myfile << columnSeperator;
		}

		myfile.close();
	}
	**/
	/**
	 * Creates a .js file from given values.
	 */
	static void writeToJSObject(map<int,string> specialValues, map<int,int> clientConsumption, map<int,int> serverConsumption) {
		assert(clientConsumption.size() > 0);
		assert(serverConsumption.size() > 0);
		ofstream myfile;
		/* Create file and write input data as Javascript variables */
		myfile.open("data.js");
		/* Write private keys (just for validation). 
		*/
		if(specialValues.empty() == false){
			myfile<< "var specialValues = [";
			for(auto it = specialValues.begin(); it != specialValues.end(); it++){
				if(it != specialValues.begin()){
					myfile<< ",";
				}
				string value = it->second;
				myfile<<value;
			}
			myfile << "];" << endl;	
		}
		/* Write client data. */
		myfile << "var dataClient = [" << endl;
		for(auto it = clientConsumption.begin(); it != clientConsumption.end(); it++) {
				myfile << "["<< it->first << ", " << it->second << "]," << endl;
			}
		myfile << "];" << endl;
		/* Write server data. */
		myfile << "var dataServer = [" << endl;
		for(auto it = serverConsumption.begin(); it != serverConsumption.end(); it++) {
				myfile << "["<< it->first << ", " << it->second << "]," << endl;
			}
		myfile << "];" << endl;
		
		/* close file. */
		myfile.close();
	}
	static double normalDistributedValue(double muh, double sig){
		std:: default_random_engine randomGenerator;
		randomGenerator.seed(std::chrono::system_clock::now().time_since_epoch().count());
		std::normal_distribution<double> distrN(muh,sig);
		return distrN(randomGenerator);
	}
};

#endif
