#include "testlib.h"
//#include <sstream>
#include <list>
#include <string>
#include <map>

using namespace std;

#include "polynom.h"
#include "link.h"

const int MINn = 1, MAXn = 7;
const int MINk = 1, MAXk = 3;
const int MINm = 2, MAXm = 25;
const int MINc = 1, MAXc = MAXm;
const int MINlen = 4, MAXlen = MAXm * MAXm;

map<pair<int, int>, string> intersection_points;

//Проверить соответствие плюса минусу в решении участника и их количество. И то, что символы плюсы или минусы.
void check_answer(vector<link_component> components)
{
  int i, j, num, r;
//  char* str;
  for(i = 0; i < int(components.size()); i++)
  {
     if(int(components[i].intersections.size()) > 2 * MAXn)
       quitf(_wa, "Size of string %d is too large", i);
     const char* str = components[i].intersections.c_str();
     for(j = 0; j < int(components[i].intersections.size()); j++)
       if(str[j] != '+' && str[j] != '-')
         quitf(_wa, "Symbol %d of string %d is %c", j, i, str[j]);
     r = 0;
     num = 0;
     for(list<point>::iterator pit = components[i].points.begin(); pit != components[i].points.end(); pit++)
       if(intersection_points.find(*pit) != intersection_points.end())
       {
         intersection_points[*pit] += str[num];
         num++;
         r++;
       }
     if(r != int(components[i].intersections.size()))
       quitf(_wa, "Length of string %d not equals to number of intersections for this snake: %d != %d", i, 
       int(components[i].intersections.size()), r);
  }
  for(map<pair<int, int>, string>::iterator iit = intersection_points.begin(); iit != intersection_points.end(); iit++)
    if(((iit->second) != "+-") && ((iit->second) != "-+"))
       quitf(_wa, "Point (%d, %d) have string %s", iit->first.first, iit->first.second, (iit->second).c_str());
}

int main(int argc, char * argv[])
{
  setName("Compare Jones polynom of link with 1");

  registerTestlibCmd(argc, argv);

  making_polynoms();

  int i, j, component_number, point_number, x, y;
  list<point> point_list;
  string intersections;
  vector<link_component> components;
  map<pair<int, int>, int> points;

  component_number = inf.readInt(MINk, MAXk);
  inf.readEoln ();

  for(i = 0; i < component_number; i++)
  {
    point_number = inf.readInt(MINlen, MAXlen);
    for(j = 0; j < point_number; j++)
    {
      x = inf.readInt(MINc, MAXc);
      y = inf.readInt(MINc, MAXc);
      point_list.push_back(make_pair(x, y));
      if(points.find(make_pair(x, y)) == points.end())
        points[make_pair(x, y)] = 1;
      else
        points[make_pair(x, y)]++;
    }
    inf.readEoln ();
    intersections = ouf.readString ("[+-]{1,100}");
    components.push_back(link_component(point_list, intersections));
    point_list.clear();
  }

  for(map<pair<int, int>, int>::iterator pit = points.begin(); pit != points.end(); pit++)
    if(pit->second == 2)
      intersection_points[pit->first] = "";

  check_answer(components);

  link L(components);
  polynom<int> p = L.Graph.getJonesPolynom();
  polynom<int> p_of_trivial_link = L.Graph.getJonesPolynomOfTrivialLink();
  stringstream s;
  s << p;
  string p_string = s.str();
  if(p == p_of_trivial_link)
    quitf(_ok, "Jones polynom = %s. Thus link is trivial", p_string.c_str());
  else
    quitf(_wa, "Jones polynom = %s", p_string.c_str());


//  return 0;
}








