#include <graph_displayer_canvas.h>
#include <math.h>

GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm, MapStorage & ms):g(gr),nodesmap(g),edgesmap(g),edgetextmap(g),displayed_graph(*(root()), 0, 0),mapstorage(ms),isbutton(false),active_item(NULL)
{
  //set_center_scroll_region(true);

  //first edges are drawn, to hide joining with nodes later

  for (EdgeIt i(g); i!=INVALID; ++i)
  {

    //drawing green lines, coordinates are from cm

    Gnome::Canvas::Points coos;
    coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
    coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
    
    edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
    *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
    edgesmap[i]->property_width_pixels().set_value(10);    
    
    //initializing edge-text as well, to empty string

    double x1, x2, y1, y2;
    edgesmap[i]->get_bounds(x1, y1, x2, y2);
    
    edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
    edgetextmap[i]->property_fill_color().set_value("black");
  }

  //afterwards nodes come to be drawn

  NodeIt i(g);
  int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;

  for (; i!=INVALID; ++i)
  {
    //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)

    if(cm[i].x>maxx)maxx=(int)cm[i].x;
    if(cm[i].y>maxy)maxy=(int)cm[i].y;
    if(cm[i].x<minx)minx=(int)cm[i].x;
    if(cm[i].y<miny)miny=(int)cm[i].y;

    //drawing bule nodes, with black line around them

    nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
    *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
    *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
    (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
  }

/*
  //setting zoom to be able to see the whole graph on the canvas

  double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
  double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);

  set_pixels_per_unit((biggest_x>biggest_y)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
  std::cout<<abs(maxx)<<" "<<abs(minx)<<" big x "<<biggest_x<<" "<<abs(maxy)<<" "<<abs(miny)<<" big y "<<biggest_y<<std::endl;
  std::cout<<maxx<<" "<<minx<<" big x "<<biggest_x<<" "<<maxy<<" "<<miny<<" big y "<<biggest_y<<std::endl;
  std::cout<<"dx "<<(maxx-minx)<<" dy "<<(maxy-miny)<<" xrate "<<((maxx-minx)/WIN_WIDTH)<<" yrate "<<((maxy-miny)/WIN_HEIGHT)<<std::endl;
*/
  updateScrollRegion();
}

GraphDisplayerCanvas::~GraphDisplayerCanvas()
{

  //writing out the end state of the graph
  //\todo all the maps has to be write out!

  Graph::NodeMap <int> id(g);
  Graph::NodeMap <double> xc(g);
  Graph::NodeMap <double> yc(g);
  
  int j=1;
  
  for (NodeIt i(g); i!=INVALID; ++i)
  {
    double x1,y1,x2,y2;
    nodesmap[i]->get_bounds(x1, y1, x2, y2);
    
    id[i]=j++;
    xc[i]=(x1+x2)/2;
    yc[i]=(y1+y2)/2;
  }

  GraphWriter<Graph> writer(std::cout,g);
  
  writer.writeNodeMap("id", id);
  writer.writeNodeMap("coordinates_x", xc);
  writer.writeNodeMap("coordinates_y", yc);
  writer.run();
}

int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
{
  for (EdgeIt i(g); i!=INVALID; ++i)
  {
    int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
    edgesmap[i]->property_width_pixels().set_value(w);
  }
  return 0;
};

int GraphDisplayerCanvas::changeColor (std::string mapname)
{  

  //function maps the range of the maximum and
  //the minimum of the nodemap to the range of
  //green in RGB

  for (EdgeIt i(g); i!=INVALID; ++i)
  {
    double w=(*(mapstorage.edgemap_storage)[mapname])[i];
    double max=mapstorage.maxOfEdgeMap(mapname);
    double min=mapstorage.minOfEdgeMap(mapname);
      
    //std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
    Gdk::Color color;
    if(max!=min)
    {
      color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
    }
    else
    {
      color.set_rgb_p (0, 100, 0);
    }

    edgesmap[i]->property_fill_color_gdk().set_value(color);
  }
  return 0;
};

int GraphDisplayerCanvas::changeText (std::string mapname)
{

  //the number in the map will be written on the edge
  //EXCEPT when the name of the map is Text, because
  //in that case empty string will be written, because
  //that is the deleter map
  //\todo isn't it a bit woodcutter?

  for (EdgeIt i(g); i!=INVALID; ++i)
  {
    if(mapname!="Text")
    {
      double number=(*(mapstorage.edgemap_storage)[mapname])[i];
      int length=(int)(floor(log(number)/log(10)))+1;
      int maxpos=(int)(pow(10,length-1));
      int strl=length+1+RANGE;
      char * str=new char[strl];
      str[length]='.';
      str[strl]='\0';
      
      for(int j=0;j<strl;j++)
      {
	if(j!=length)
        {
	  int digit=(int)(number/maxpos);
	  str[j]=(digit+'0');
	  number-=digit*maxpos;
	  number*=10;
        }
      }
      
      edgetextmap[i]->property_text().set_value(str);
    }
    else
    {
      edgetextmap[i]->property_text().set_value("");
    }
  }
  return 0;
};

bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
{
  switch(e->type)
  {
    case GDK_BUTTON_PRESS:
      //we mark the location of the event to be able to calculate parameters of dragging
      clicked_x=e->button.x;
      clicked_y=e->button.y;
      active_item=(get_item_at(e->button.x, e->button.y));
      isbutton=true;
      break;
    case GDK_BUTTON_RELEASE:
      isbutton=false;
      active_item=NULL;
      updateScrollRegion();
      break;
    case GDK_MOTION_NOTIFY:
      //we only have to do sg. if the mouse button is pressed
      if(isbutton)
      {
	//new coordinates will be the old values,
	//because the item will be moved to the
	//new coordinate therefore the new movement
	//has to be calculated from here

        double dx=e->motion.x-clicked_x;
        double dy=e->motion.y-clicked_y;
        active_item->move(dx, dy);
        clicked_x=e->motion.x;
        clicked_y=e->motion.y;

	//all the edges connected to the moved point has to be redrawn

        EdgeIt e;
        g.firstOut(e,n);
        for(;e!=INVALID;g.nextOut(e))
        {
            Gnome::Canvas::Points coos;
            double x1, x2, y1, y2;

            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));

            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));

            edgesmap[e]->property_points().set_value(coos);

	    edgesmap[e]->get_bounds(x1, y1, x2, y2);

	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
        }

        g.firstIn(e,n);
        for(;e!=INVALID;g.nextIn(e))
        {
            Gnome::Canvas::Points coos;
            double x1, x2, y1, y2;

            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));

            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));

            edgesmap[e]->property_points().set_value(coos);

	    edgesmap[e]->get_bounds(x1, y1, x2, y2);

	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
        }
      }
    default: break;
  }
  return true;
}

bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
{
  Gnome::Canvas::CanvasAA::on_expose_event(event);
  //usleep(10000);
  //rezoom();
  return true;
}

void GraphDisplayerCanvas::zoomIn()
{
  set_pixels_per_unit(
      (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
}

void GraphDisplayerCanvas::zoomOut()
{
  set_pixels_per_unit(
      (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
}

void GraphDisplayerCanvas::zoomFit()
{
  // get the height and width of the canvas
  Gtk::Allocation a = get_allocation();
  int aw = a.get_width();
  int ah = a.get_height();
  // add some space
  aw -= 5; if (aw < 0) aw = 0;
  ah -= 5; if (ah < 0) ah = 0;

  // get the bounding box of the graph
  double wx1, wy1, wx2, wy2;
  Gnome::Canvas::Item* pCanvasItem = root();
  pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);

  // fit the graph to the window
  double ppu1 = (double) aw / fabs(wx2 - wx1);
  double ppu2 = (double) ah / fabs(wy2 - wy1);
  set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2);
}

void GraphDisplayerCanvas::zoom100()
{
  set_pixels_per_unit(1.0);
}

void GraphDisplayerCanvas::updateScrollRegion()
{
  double wx1, wy1, wx2, wy2;
  Gnome::Canvas::Item* pCanvasItem = root();
  pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
  set_scroll_region(wx1, wy1, wx2, wy2);
}
