#include<stk.h>
#include<strstream.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/layout/Layouts.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/graph/Graph.h>

static PRIMITIVE ComponentLayout2Cmd(SCM g, SCM chunks, SCM chunk_size)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("grid-layout!: bad arg 1, graph expected", g);
        if (!INTEGERP(chunks))
                Err("grid-layout!: bad arg 2, #chunks expected", chunks);
        if (!INTEGERP(chunk_size))
                Err("grid-layout!: bad arg 2, chunk_size expected", chunk_size);
        ComponentLayout2(gr, INTEGER(chunks), INTEGER(chunk_size));

        return UNDEFINED;
}

static PRIMITIVE ComponentLayout2AnimCmd(SCM gparms, int len)
{
        SCM g, gv, chunks, chunk_size;

        if (len < 4)
                return UNDEFINED;
        g = CAR(gparms);
        gv = CAR(CDR(gparms));
        chunks = CAR(CDR(CDR(gparms)));
        chunk_size = CAR(CDR(CDR(CDR(gparms))));

	Graph *gr;
        if (!(gr = getGraph(g)))
                Err("grid-layout!: bad arg 1, graph expected", g);
	char *vname = getViewName(gv);
        if (!INTEGERP(chunks))
                Err("grid-layout!: bad arg 2, #chunks expected", chunks);
        if (!INTEGERP(chunk_size))
                Err("grid-layout!: bad arg 2, chunk_size expected", chunk_size);
        ComponentLayout2(gr, INTEGER(chunks), INTEGER(chunk_size));
	path_draw(vname);
	layoutVertices(vname);
        return UNDEFINED;
}

void gen_ComponentLayout2_Wrapper()
{
        STk_add_new_cpp_primitive("component-layout2", tc_subr_3,
                               (PRIMITIVE (*)(...))ComponentLayout2Cmd);
        STk_add_new_cpp_primitive("grid-layout2*", tc_lsubr, 
                               (PRIMITIVE (*)(...))ComponentLayout2AnimCmd);
}
