#ifndef BRIDGES_H
#define BRIDGES_H
#include <iostream>
#include <algorithm>
using namespace std;
#include "DataStructure.h"
#include "ServerComm.h"
namespace bridges {
namespace Bridges {
static bool jsonFlag = false;
static string user_name = string(), api_key = string();
static unsigned int assn_num = 0;
static int *dims = nullptr;
static DataStructure* ds_handle = nullptr;
static string server_url = "http://bridges-cs.herokuapp.com";
static string map_overlay_options[] = {"cartesian", "albersusa", "equirectangular"};
static bool map_overlay = false;
static string coord_system_type = "cartesian";
bool& getVisualizeJSONFlag() {
return jsonFlag;;
}
void setVisualizeJSONFlag(bool flag) {
jsonFlag = flag;
}
string& getUserName() {
return user_name;
}
void setUserName(string name) {
user_name = name;
}
string& getApiKey() {
return api_key;
}
void setApiKey(string k) {
api_key = k;
}
unsigned int& getAssignment() {
return assn_num;
}
void setAssignment(unsigned int num) {
assn_num = num;
}
static string title;
string& getTitle() {
return title;
}
void setTitle(string t) {
title = t;
}
static string description;
string& getDescription() {
return description;
}
void setDescription(string descr) {
description = descr;
}
void setDimensions(int *d) {
dims = d;
}
void setDataStructure(DataStructure *ds) {
ds_handle = ds;
}
DataStructure*& getDataStructure() {
return ds_handle;
}
void initialize(const unsigned int& num, const string& name,
const string& key) {
assn_num = num;
user_name = name;
api_key = key;
}
void setServer(string server_type) {
if (server_type == "live")
server_url = "http://bridges-cs.herokuapp.com";
else if (server_type == "clone")
server_url = "http://bridges-clone.herokuapp.com";
else if (server_type == "local")
server_url = "http://127.0.0.1:3000";
}
void setMapOverlay (bool overlay_flag) {
map_overlay = overlay_flag;
}
void setCoordSystemType (string coord) {
std::transform(coord.begin(), coord.end(), coord.begin(), ::tolower);
if (coord == "cartesian" ||coord == "albersusa" || coord == "equirectangular")
coord_system_type = coord;
else {
cout << "Unrecognized coordinate system \'" + coord + "\', defaulting to "
<< "cartesian. Options:";
for (auto proj : map_overlay_options)
cout << + "\t" ;
coord_system_type = "cartesian";
}
}
string getCoordSystemType () {
return coord_system_type;
}
void visualize() {
static unsigned int lastAssign = 0, part = 0;
static const string BASE_URL = server_url + "/assignments/";
if (assn_num != lastAssign) {
lastAssign = assn_num;
part = 0;
}
if (part == 99) {
cout << "#sub-assignments limit(99) exceeded, visualization not generated .."
<< endl;
return;
}
if (!ds_handle) {
cerr << "Error: Data Structure handle null! Visualization not generated.";
return;
}
string ds_type = ds_handle->getDStype();
string ds_json = OPEN_CURLY +
QUOTE + "visual" + QUOTE + COLON + QUOTE + ds_type + QUOTE + COMMA +
QUOTE + "title" + QUOTE + COLON + QUOTE + getTitle() + QUOTE + COMMA +
QUOTE + "description" + QUOTE + COLON + QUOTE + getDescription() + QUOTE + COMMA +
QUOTE + "map_overlay" + QUOTE + COLON + ((map_overlay) ? "true" : "false") + COMMA +
QUOTE + "coord_system_type" + QUOTE + COLON + QUOTE + getCoordSystemType() + QUOTE + COMMA;
if (ds_type == "Array") {
ds_json += QUOTE + "dims" + QUOTE + COLON +
OPEN_BOX +
to_string(dims[0]) + COMMA +
to_string(dims[1]) + COMMA +
to_string(dims[2]) +
CLOSE_BOX + COMMA;
}
else if (ds_type == "ColorGrid") {
ds_json += QUOTE + "dimensions" + QUOTE + COLON +
OPEN_BOX +
to_string(dims[0]) + COMMA + to_string(dims[1]) +
CLOSE_BOX + COMMA;
}
ds_json += QUOTE + "nodes" + QUOTE + COLON;
const pair<string, string> json_nodes_links =
ds_handle->getDataStructureRepresentation();
if (ds_type == "Tree" || ds_type == "BinaryTree" ||
ds_type == "BinarySearchTree" || ds_type == "AVLTree" ) {
ds_json += json_nodes_links.first + CLOSE_CURLY;
}
else if (ds_type == "ColorGrid") {
ds_json +=
OPEN_BOX + QUOTE + json_nodes_links.first + QUOTE + CLOSE_BOX
+ CLOSE_CURLY;
}
else {
ds_json +=
OPEN_BOX + json_nodes_links.first + CLOSE_BOX + COMMA +
QUOTE + "links" + QUOTE + COLON + OPEN_BOX +
json_nodes_links.second + CLOSE_BOX +
CLOSE_CURLY;
}
if (getVisualizeJSONFlag()) {
cout << "JSON String:\t" << ds_json << endl;
}
try {
ServerComm::makeRequest(BASE_URL + to_string(assn_num) + "." +
(part > 9 ? "" : "0") + to_string(part) + "?apikey=" + api_key +
"&username=" + user_name,
{"Content-Type: text/plain"}, ds_json);
cout << "Success: Assignment posted to the server. " << endl <<
"Check out your visualization at:" << endl << endl
<< BASE_URL + to_string(assn_num) + "/" + user_name << endl << endl;
part++;
}
catch (const string& error_str) {
cerr << "\nPosting assignment to the server failed!" << endl
<< error_str << endl << endl;
cerr << "Provided Bridges Credentials:" << endl <<
"\t User Name: " << user_name << endl <<
"\t API Key: " << api_key << endl <<
"\t Assignment Number: " << assn_num << endl;
}
}
}
}
#endif