#include"Functions.hpp"
#include"Instructions.hpp"
#include"Relativity.hpp"

struct Indexed{
  Index negatives[hDegree];
  Index zeropoint;
  Index positives[hDegree];

  Heights heights[nHeights];
  Potentials potentials[nHeights];
  Vertex vertices[nVertices];

  ZeroColor zerocolor;

  Movement movements[nColors][nVertices];

  inline Indexed(){
    for(Vertices v=0;v<nVertices;++v){
      auto const D=vEdges[v];
      auto const E=aEdges[v]+v;
      auto*const H=heights+E;
      auto*const P=potentials+E;
      auto*const V=vertices+v;
      (*V)(&zerocolor);
      for(Edges d=0;d<=D;++d)
        P[d](&zeropoint+d,V);
      for(auto&c:movements){
        c[v](H,P);
//TODO
        c[v].vertex=v;
      }
    }
  }
  //TODO NP!=P
  struct LinkableVertex;
  struct LinkableVertices:List<LinkableVertex>{};
  struct LinkableVertex:Element<LinkableVertex,LinkableVertices,void,0>{
    Vertices v;
  };

  struct Branch;
  struct LinkableBranches:List<Branch>{};

  struct Branch:Element<Branch,LinkableBranches,void,0>{
    LinkableVertices p;
    LinkableVertices e;
    LinkableBranches b;

    //destructor muere todas

    unsigned long long inline result(Indexed*const in){
      unsigned long long sum=0;
      loopL((&e)){
        auto*const a=new Branch();
        (*a)(&b),~(*a);
        auto*const v=new LinkableVertex();
        auto const e=i->v;
        (*v)(&a->p),~(*v),v->v=e;
        loopL((&p)){
          auto*const v=new LinkableVertex();
          auto const p=i->v;
          (*v)(&a->p),~(*v),v->v=p;
          loopE{
            auto const e=edges[i];
            if(e!=p){
              auto*const n=new LinkableVertex();
              (*n)(&a->e),~(*n),n->v=e;
              in->movements[0][e].height.l-(in->heights+aEdges[e]+e);
            }
          }
        }
      }
    }
  };

  Branch branches[nVertices];
  Vertices nBranches;
  //TODO NP!=P END
  void inline solve(){
    for(unsigned i=0;i<=hDegree*2+1;++i){
      auto const*const n=negatives+i;

      nBranches=0;
      for(auto j=n->z;*n!=j;j=j->z){
        //checks[checks_n++]=j;
        //cout<<(*(*(j))->z)->vertex<<endl;
      }

      if(*n!=n->z){
        Movement*m=(*(*(n->z))->z);
        /*
        signed num=i-hDegree;
        cout<<num<<','<<m->vertex<<','<<(m-m->vertex-movements[0])/nVertices<<','<<zerocolor.score<<endl;
        /**/
        change(m);
        break;
      }
    }
  }

  Colors inline count(){
    auto const*m=movements[0];
    for(Vertices e=0;e<nVertices;++e)
      if(vertices[e].selected-e>m)
        m=vertices[e].selected;
    return(m-movements[0])/nVertices;
  }

  inline Movement*algo(){
    auto*m=movements[0];
    for(Vertices e=0;e<nVertices;++e){
      auto*const a=vertices[e].selected-e;
      if(a>m)
        m=a;
    }
    return m;
  }

  void inline shrink(){
    auto*c=algo();
    auto*X=c+nVertices;
    for(auto*C=X;C<movements[zerocolor.colors];++C)
      !*C;
    for(Vertices e=0;e<nVertices;++e){
      auto const*const v=vertices+e;
      if(v->selected==c+e){
        auto const*p=potentials+aEdges[e]+e;
        while(*p==p->z)++p;
        change(*p->z);
      }
    }
    for(;c<X;++c)
      !*c;
    zerocolor.colors=count();
  }

  void inline change(Movement*const m1){
    auto const e=m1->vertex;
    auto*const v=vertices+e;
    auto*const m0=v->selected;
    setC(c0,m0)
    setC(c1,m1)
    setH(h0,m0)
    setH(h1,m1)

    ~*m0,zerocolor(m0),(*v)(&zerocolor);
    //TODO Guardar potential.l, luego si no hay dZ revisar si está activo y haceR?
    //*(m0->potential.l->position);
    *(m1->potential.l->position);

    Change(*m0,*m1)

    Delta const dH=h1-h0;
    zerocolor.score+=dH;

    !*m1,v->selected=m1;

    if(dH)movZ(-dH)
    //else{
      //*(m0->potential.l->position);
      //*(m1->potential.l->position);
    //}
  }

  void inline start(){
    for(Vertices e=0;e<nVertices;++e){
#ifdef Solution
      auto*const c1=movements[Solution[e]];
#else
      auto*const c1=movements[0];
#endif
      setM(m1,c1)
      setH(h1,m1)

      Start(*m1)

      auto const*const h=heights+aEdges[e]+e;
      Delta const dH=h1-h;
      zerocolor.score+=dH;

      auto*const v=vertices+e;
      !*m1,v->selected=m1;

      if(dH)movZ(-dH)
    }
  }
};
