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

#include "user.h"

User::User() {
	objectType = "user";
}

User::User(Json::Value data) {
	objectData = data;

	objectType = "user";

	//nastaveni dat
	id = objectData["user"]["id"].asString();
	username = objectData["user"]["username"].asString();
	email = objectData["user"]["email"].asString();
	root_directory_id = objectData["user"]["root_directory_id"].asString();
}

int User::get(string id) {
	//nacteni objektu
	int success = Item::get(id);

	if (!success) {
		return 0;
	}

	//nastaveni dat
	username = objectData["user"]["username"].asString();
	email = objectData["user"]["email"].asString();
	root_directory_id = objectData["user"]["root_directory_id"].asString();

	return 1;
}

int User::put() {
	//priprava objektu
	ParamMap params;
	string buffer;

	//dotaz na zmenu emailove adresy
	cout << "Zadejte novou emailovou adresu : ";
	cin >> buffer;

	params["user[email]"] = buffer;

	//dotaz na zmenu hesla
	cout << "Chcete změnit heslo? (a/n) : ";
	cin >> buffer;

	if (buffer == "a") {
		cout << "Zadejte staré heslo : ";
		cin >> buffer;

		params["user[password_old]"] = buffer;

		cout << "Zadejte nové heslo : ";
		cin >> buffer;

		params["user[password]"] = buffer;

		cout << "Zadejte nové heslo (kontrola) : ";
		cin >> buffer;

		params["user[password_check]"] = buffer;
	}

	//odeslani pozadavku
	string status;
	int statusCode;

	Json::Value response = Comunicator::getInstance()->sendJsonRequest(objectType, "put", id, status, statusCode, params);

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

	//update dat
	objectData["user"]["email"] = params["user[email]"];
	email = params["user[email]"];

	return 1;
}

int User::post() {
	//nacitani dat
	ParamMap params;
	string buffer, username;

	cout << endl << "VYTVOŘENÍ NOVÉHO UŽIVATELE : " << endl;
	cout << "Přihlašovací jméno : ";
	cin >> username;

	cout << "E-Mail : ";
	cin >> buffer;
	params["user[email]"] = buffer;

	cout << "Heslo : ";
	cin >> buffer;
	params["user[password]"] = buffer;

	//odeslani requestu
	string status;
	int statusCode;
	Json::Value response = Comunicator::getInstance()->sendJsonRequest(objectType, "post", username, status, statusCode, params);

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

	cout << endl << "Uživatel " << username << " byl vytvořen" << endl;
	cout << "Identifikační číslo uživatele : " << response["user"]["id"].asString() << endl;
	cout << "Identifikační číslo kořenového adresáře : " << response["user"]["root_directory_id"].asString() << endl << endl;

	//nastaveni dat
	objectData = response;
	this->username = response["user"]["username"].asString();
	email = response["user"]["email"].asString();
	root_directory_id = response["user"]["root_directory_id"].asString();
	id = response["user"]["id"].asString();

	return 1;
}

int User::deleteObject() {
	//pokud o smazani objektu
	string status;
	int statusCode;
	ParamMap params;

	Json::Value response = Comunicator::getInstance()->sendJsonRequest(objectType, "delete", id, status, statusCode, params);

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

	cout << "Uživatel byl smazán" << endl;

	return 1;
}

int User::getInfo() {
	cout << endl;
	cout << "Uživatelské jméno : " << username << endl;
	cout << "Identifikační číslo : " << id << endl;
	cout << "E-Mailová adresa : " << email << endl;
	cout << "# domovského adresáře : " << root_directory_id << endl;

	return 0;
}

int User::getShortInfo() {
	cout << endl;
	cout << "Uživatelské jméno : " << username << endl;
	cout << "Identifikační číslo : " << id << endl;

	return 0;
}
