#include <string>
#include <iostream>
#include <fstream>
#include <sstream>

#include "gdb/Sparksee.h"
#include "gdb/Database.h"
#include "gdb/Session.h"
#include "gdb/Graph.h"
#include "gdb/Objects.h"
#include "gdb/ObjectsIterator.h"
#include "algorithms/TraversalDFS.h"

using std::string;
using std::cout;
using namespace sparksee::gdb;
using namespace sparksee::algorithms;

int main(int argc, char *argv[]) {
  // http://sparsity-technologies.com/downloads/doc-cpp/html/
  // Create the database
  SparkseeConfig cfg;
  //cfg.SetRecoveryEnabled(true);
  Sparksee *sparksee = new Sparksee(cfg);
  //Database *db = sparksee->Create(L"CGraph.gdb", L"CGraph");
  Database *db = sparksee->Open(L"CGraph.gdb", L"CGraph");
  //db->DisableRollback();  // Rollback is now disabled
  Session *sess = db->NewSession();
  Graph *graph = sess->GetGraph();

  type_t nodeTypeId = graph->FindType(L"CNODE");
  type_t CNode;
  type_t relType;
  if (nodeTypeId==Type::InvalidType) {
    std::wcout << "CNODE has to be created from scratch" << std::endl;
    CNode = graph->NewNodeType(L"CNODE");
    attr_t CId = graph->NewAttribute(CNode, L"ID", Long, Unique);
    attr_t CName = graph->NewAttribute(CNode, L"NAME", String, Indexed);
  }
  else {
    std::wcout << "CNODE already present" << std::endl;
  }

  std::array<std::string, 3> FrankArray = {"FatherOf", "Spouse", "BrotherOf"};
  std::wstring RelationshipType( FrankArray[1].begin(), FrankArray[1].end() );
  std::wcout << "RelationshipType= " << RelationshipType << std::endl;
  type_t relTypeId = graph->FindType(RelationshipType);
  std::wcout << "relTypeId= " << relTypeId << std::endl;
  if (relTypeId==Type::InvalidType) {
    std::wcout << RelationshipType << " HAS TO BE INSERTED in DB" << std::endl;
    // Edges:
    // Add a directed edge type from CNodeLeft to CNodeRight
    relType = graph->NewEdgeType(RelationshipType, CNode, CNode);
  }
  else {
    std::wcout << RelationshipType << " already present" << std::endl;
  }


  // Close the database:
  delete sess;
  delete db;
  delete sparksee;
  return 0;
}

