
#include <map>
#include <stdlib.h>
#include <gtools.h>
#include <nausparse.h>
#include "../../basic_impl.hpp"
#include "../../operations.hpp"
#include "../../invariants/girth.hpp"
#include "../../io/graph6.hpp"
#include "../../io/print_nice.hpp"

using namespace ba_graph;
std::vector <std::vector <int>> generatory;
void vypis_mi_permutacie(int count, permutation *perm, int *orbits, int numorbits, int stabvertex, int n)
{
    std::vector<int> permutacia;
    for (int i = 0; i < n; ++i) {
        permutacia.push_back(perm[i]);
    }
    generatory.push_back(permutacia);
}

std::vector <std::vector <int>> automorfizmus(const Graph &G) {
    generatory.clear();
    int n = G.order();
    static DEFAULTOPTIONS_SPARSEGRAPH(options);
    options.defaultptn = TRUE;
    //options.writeautoms = TRUE;
   // options.cartesian = TRUE;
    options.userautomproc = &vypis_mi_permutacie;

    std::string s;
    s = write_sparse6(G, false);

    char *Gs6 = new char[s.length() + 1];
    std::copy(s.begin(), s.end(), Gs6);
    Gs6[s.length()] = '\0';

    sparsegraph* g = (sparsegraph*)malloc(sizeof(sparsegraph));
    SG_INIT((*g));
    int num_loops;

    stringtosparsegraph(Gs6, g, &num_loops);
    delete[] Gs6;


    int m = (n + WORDSIZE - 1) / WORDSIZE;
    nauty_check(WORDSIZE, m, n, NAUTYVERSIONID);

    DYNALLSTAT(int, lab, lab_n);
    DYNALLSTAT(int, ptn, ptn_n);
    DYNALLSTAT(int, orbits, orbits_n);
    DYNALLOC1(int, lab, lab_n, n, "malloc");
    DYNALLOC1(int, ptn, ptn_n, n, "malloc");
    DYNALLOC1(int, orbits, orbits_n, n, "malloc");
    statsblk stats;



   // printf("Generators for Aut(C[%d]):\n",n);
    sparsenauty( g,lab,ptn,orbits,&options,&stats,NULL);
   // printf("\n");

  //  std::cout <<stats.numgenerators;

    DYNFREE(lab,lab_n);
    DYNFREE(ptn,ptn_n);
    DYNFREE(orbits,orbits_n);
    SG_FREE((*g));
    free(g);

    return generatory;
}