/*
 * ZHStudentSoluetion.cpp
 *
 *  Created on: 2015. dec. 13.
 *      Author: Tamás
 */

#include "ZHStudentSolution.h"
#include <vector>
#include <tr1/unordered_set>
#include <exception>
#include <iostream>
#include <map>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int stoi(string data) {
	int value;
	stringstream ss(data);
	ss >> value;
	return value;
}

string itos(int data) {
	string value;
	stringstream ss;
	ss << data;
	ss >> value;
	return value;
}

int readInt(ifstream &f) {
	string line;
	if (!getline(f, line, '\t')) {cerr << "HIBA: nem megfelelo bemeneti fajl!\n"; throw exception(); }
	return stoi(line);
}

string readStr(ifstream &f) {
	string line;
	if (!getline(f, line, '\t')) {cerr << "HIBA: nem megfelelo bemeneti fajl!\n"; throw exception(); }
	return line;
}

void ZHStudentSolution::readPlanet(std::string fileName, std::vector<MinorPlanet> &data) {
	ifstream f(fileName);
	if (f.fail()) { cerr << "HIBA: nem sikerult megnyitni a fajlt!\n"; throw exception(); }

	while (f.peek() != ifstream::traits_type::eof()) {
		MinorPlanet mp;
		mp.number = readInt(f);
		mp.name = readStr(f);
		mp.temporaryName = readStr(f);
		mp.YearOfDiscovery = readInt(f);
		mp.MonthOfDiscovery = readInt(f);
		mp.DayOfDiscovery = readInt(f);
		mp.PlaceOfDiscovery = readStr(f);
		mp.reference = readStr(f);
		getline(f, mp.person);
		data.push_back(mp);
	}
	cout << "Beolvasas kesz!\n";
}

void ZHStudentSolution::readCity(std::string fileName, std::vector<std::string>& cityData) {
	ifstream f(fileName);
	if (f.fail()) { cerr << "HIBA: nem sikerult megnyitni a fajlt!\n"; throw exception(); }

	string line;
	while (getline(f, line))
		cityData.push_back(line);
}

DayOfYear ZHStudentSolution::task1(const std::vector<MinorPlanet>& data) {
	const short int MONTHS = 12, DAYS_IN_A_MONTH = 31;
	vector<int> days(MONTHS * DAYS_IN_A_MONTH, 0);

	int max = 0, max_place;

	for (auto r : data)
		days[(r.MonthOfDiscovery-1)*DAYS_IN_A_MONTH + (r.DayOfDiscovery-1)]++;
	for (size_t i = 0; i < MONTHS * DAYS_IN_A_MONTH; i++)
		if (max < days[i]) {
			max = days[i];
			max_place = i;
		}

	if (max == 0) cerr << "HIBA: nincs maximum!\n";
	return DayOfYear({(short int)(max_place/DAYS_IN_A_MONTH), (short int)(max_place%DAYS_IN_A_MONTH)});
}

void ZHStudentSolution::prepare2_3_4(const vector<MinorPlanet>& data) {
	for (auto r : data)
		if (r.PlaceOfDiscovery != "") {
			places[r.PlaceOfDiscovery].first++;
			if (r.name != "")
				places[r.PlaceOfDiscovery].second.push_back(r.name);
		}
}

string ZHStudentSolution::task2() {
	int max = 0;
	string name = "";
	for (auto r : places)
		if (max < r.second.first) {
			max = r.second.first;
			name = r.first;
		}
	return name;
}

vector<string> ZHStudentSolution::task3() {
	vector <string> result;
	for (auto r : places)
		if (r.second.first == 1)
			result.push_back(r.first);
	return result;
}

vector<string> ZHStudentSolution::task4(string place) {
	vector<string> result = places[place].second;
	if (result.empty()) cerr << "A megadott varosban nem fedeztek fel kisbolygot.\n";
	return result;
}

vector<string> ZHStudentSolution::task5(const vector<MinorPlanet>& data, const vector<string>& cityData) {
	tr1::unordered_set<string> places;
	for (auto r : cityData)
		places.insert(r);
	vector<string> list;
	for (auto r : data)
		if (places.count(r.name))
			list.push_back(itos(r.number) + " " + r.name);
	return list;
}

std::vector<int> task6(const std::vector<MinorPlanet>& data) {

}







