/*
 * userContainer.cpp
 *
 *  Created on: 26.5.2011
 *      Author: petr
 */

#include "userContainer.h"

UserContainer::UserContainer() {
	isLoaded = 0;
}

int UserContainer::refreshList() {
	//nacteni dat ze serveru
	string status;
	int statusCode;
	ParamMap params;

	Json::Value response = Comunicator::getInstance()->sendJsonRequest("user", "get", "list", status, statusCode, params);

	if (statusCode != 200) {
		writeStatus(status, statusCode);
		return 0;
	}

	//vyprazdneni seznamu
	localList.clear();

	//zapis dat
	Json::Value::iterator iter = response["users"].begin();

	while (iter != response["users"].end()) {
		Json::Value buffer;

		buffer["user"] = (*iter);

		//zapsani noveho uzivatele
		User *user = new User(buffer);
		localList.push_back(user);

		UserContainer::globalList[string("user_") + buffer["id"].asString()] = user;

		iter++;
	}

	return 1;
}

int UserContainer::getList() {
	//kontorla jeslti jsou data nahrana
	if (!isLoaded) {
		refreshList();
		isLoaded = 1;
	}

	static int doCommand = 1;

	//vypis uzivatelu
	LocalContainer::iterator iter = localList.begin();
	int i = 1;

	while (iter != localList.end()) {
		cout << i << " : ";

		cout << (*iter)->objectData["user"]["username"].asString() << endl;

		iter++;
		i++;
	}

	//kontorla jestli se maji zobrazit prikazy
	if (!doCommand) {
		return 1;
	}

	//vypsani nabidky
	cout << "Pro zobrazeni uzivatele zadejte jeho cislo. Pro navrat zadejte 0 : ";
	int index;
	string command;

	cin >> index;

	while (index != 0) {
		//dekremnetace na pozici
		index--;

		//kontrola validity
		if (index >= 0 && index < localList.size()) {
			localList[index]->getInfo();
		}

		cout << endl;

		//vypsani nabidky
		cout << "Zadejte příkaz : ";
		cin >> command;

		while (command != "return") {
			if (command == "help") {
				printHelp();
			} else if (command == "delete") {
				if (localList[index]->deleteObject()) {
					//smazani probehlo uspesne, uvloni se pamet a znovu se nacte seznam uzivatelu
					free(localList[index]);
					localList.erase(localList.begin() + index);
				}
			} else if (command == "edit") {
				localList[index]->put();
			}

			if (command != "delete") {
				cout << "Zadejte příkaz : ";
				cin >> command;
			} else {
				command = "return";
			}
		}

		doCommand = 0;
		getList();

		//dotaz na dalsi akci
		cout << "Pro zobrazeni uzivatele zadejte jeho cislo. Pro navrat zadejte 0 : ";
		cin >> index;
	}

	return 1;
}

void UserContainer::printHelp() {
	cout << endl << "help - vypíše tuto náppovědu" << endl;
	cout << "edit - přepne na editaci uživatele" << endl;
	cout << "delete - smaže uživatele" << endl;
	cout << "return - vrátí se na předchozí nabídku" << endl << endl;
}

int UserContainer::createUser() {
	User *user = new User;

	if (!user->post()) {
		free(user);
		return 0;
	}

	//zapsani do lokalniho a globalniho indexu
	localList.push_back(user);
	globalList["user_" + user->getId()] = user;

	return 1;
}

int UserContainer::getUser(string id) {
	//kontorla nacteni uzivatelu
	if (!isLoaded) {
		refreshList();
		isLoaded = 1;
	}

	//kontorla jeslti uzivatel existuje
	GlobalContainer::iterator pos = globalList.find(string("user_") + id);

	if (pos == globalList.end()) {
		//uzivatel neexistuje
		cout << "Tento uživatel neexistuje";
		return 0;
	}

	pos->second->getInfo();

	//vypsani nabidky
	string command;
	cout << "Zadejte příkaz : ";
	cin >> command;

	while (command != "return") {
		if (command == "help") {
			printHelp();
		} else if (command == "delete") {
			if (pos->second->deleteObject()) {


			}
		} else if (command == "edit") {
			pos->second->put();
		}

		if (command != "delete") {
			cout << "Zadejte příkaz : ";
			cin >> command;
		} else {
			command = "return";
		}
	}

	return 1;
}
