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


int GraphDisplayerCanvas::changeEdgeWidth (std::string mapname, Graph::Edge edge)
{
  Graph::EdgeMap<double> * actual_map;
  double min, max;

  if(mapname=="Default")
    {
      min=edge_property_defaults[E_WIDTH];
      max=edge_property_defaults[E_WIDTH];
      actual_map=new Graph::EdgeMap<double>(mapstorage.graph,edge_property_defaults[E_WIDTH]);
    }
  else
    {
      min=mapstorage.minOfEdgeMap(mapname);
      max=mapstorage.maxOfEdgeMap(mapname);
      actual_map=(mapstorage.edgemap_storage)[mapname];
    }

  if(edge==INVALID)
    {
      for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
	{
	  double v=abs((*actual_map)[i]);
	  int w;
	  if(min==max)
	    {
	      w=(int)(edge_property_defaults[E_WIDTH]);
	    }
	  else
	    {
	      w=(int)(MIN_EDGE_WIDTH+(v-min)/(max-min)*(MAX_EDGE_WIDTH-MIN_EDGE_WIDTH));
	    }
	  edgesmap[i]->property_width_units().set_value(w);
	}
    }
  else
    {
      int w=(int)(*actual_map)[edge];
      if(w>=0)
	{
	  edgesmap[edge]->property_width_units().set_value(w);
	}
    }
  return 0;
};

int GraphDisplayerCanvas::changeEdgeColor (std::string mapname, Graph::Edge edge)
{  

  //function maps the range of the maximum and
  //the minimum of the nodemap to the range of
  //green in RGB
  Graph::EdgeMap<double> * actual_map;
  if(mapname=="Default")
    {
      actual_map=new Graph::EdgeMap<double>(mapstorage.graph,edge_property_defaults[E_COLOR]);
    }
  else
    {
      actual_map=(mapstorage.edgemap_storage)[mapname];
    }

  double max, min;

  if(mapname!="Default")
    {
      max=mapstorage.maxOfEdgeMap(mapname);
      min=mapstorage.minOfEdgeMap(mapname);
    }
  else
    {
      max=edge_property_defaults[E_COLOR];
      min=edge_property_defaults[E_COLOR];
    }

  if(edge==INVALID)
    {
      for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
	{
	  double w=(*actual_map)[i];

	  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);
	}
    }
  else
    {
      Gdk::Color color;

      double w=(*actual_map)[edge];

      if(max!=min)
	{
	  color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
	}
      else
	{
	  color.set_rgb_p (0, 100, 0);
	}

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

int GraphDisplayerCanvas::changeEdgeText (std::string mapname, Graph::Edge edge)
{
  //the number in the map will be written on the edge
  //EXCEPT when the name of the map is Default, because
  //in that case empty string will be written, because
  //that is the deleter map

  if(edge==INVALID)
    {
      for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
	{
	  if(mapname!="Default")
	    {
	      edgemap_to_edit=mapname;
	      double number=(*(mapstorage.edgemap_storage)[mapname])[i];

	      std::ostringstream ostr;
	      ostr << number;
	      
      	      edgetextmap[i]->property_text().set_value(ostr.str());
	    }
	  else
	    {
	      edgemap_to_edit="";
	      edgetextmap[i]->property_text().set_value("");
	    }
	}

    }
  else
    {
      if(mapname!="Default")
	{
	  double number=(*(mapstorage.edgemap_storage)[mapname])[edge];

	  std::ostringstream ostr;
	  ostr << number;
	  
	  edgetextmap[edge]->property_text().set_value(ostr.str());
	}
      else
	{
	  edgetextmap[edge]->property_text().set_value("");
	}
	  
    }

  return 0;

};
