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

GraphDisplayerCanvas::GraphDisplayerCanvas(MapStorage & ms, MapWin & mw) :
  nodesmap(ms.graph), edgesmap(ms.graph), edgetextmap(ms.graph),
  nodetextmap(ms.graph), displayed_graph(*(root()), 0, 0),
  canvasentrywidget(NULL), mapstorage(ms), isbutton(0), active_item(NULL),
  target_item(NULL), nodemap_to_edit(""), edgemap_to_edit(""), mapwin(mw)
{
  //base event handler is move tool
  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
  actual_tool=MOVE;

  //setting event handler for the editor widget
  entrywidget.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::entryWidgetChangeHandler), false);

  active_node=INVALID;
  active_edge=INVALID;
  forming_edge=INVALID;
}

GraphDisplayerCanvas::~GraphDisplayerCanvas()
{
  for (NodeIt n(mapstorage.graph); n != INVALID; ++n)
  {
    delete nodesmap[n];
    delete nodetextmap[n];
  }

  for (EdgeIt e(mapstorage.graph); e != INVALID; ++e)
  {
    delete edgesmap[e];
    delete edgetextmap[e];
  }

  if(canvasentrywidget)
  {
    delete(canvasentrywidget);
  }
}

void GraphDisplayerCanvas::drawGraph()
{
  //first edges are drawn, to hide joining with nodes later

  for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
  {

    //drawing green lines, coordinates are from mapstorage.coords

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

    xy<double> text_pos=edgesmap[i]->getArrowPos();
    text_pos+=(xy<double>(10,10));

    edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
    edgetextmap[i]->property_fill_color().set_value("darkgreen");
    edgetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false);
  }

  //afterwards nodes come to be drawn

  for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
  {
    //drawing bule nodes, with black line around them

    nodesmap[i]=new Gnome::Canvas::Ellipse(
        displayed_graph,
        mapstorage.coords[i].x-20,
        mapstorage.coords[i].y-20,
        mapstorage.coords[i].x+20,
        mapstorage.coords[i].y+20);
    *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
    *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");

    //initializing edge-text as well, to empty string

    xy<double> text_pos(
        (mapstorage.coords[i].x+node_property_defaults[N_RADIUS]+5),
        (mapstorage.coords[i].y+node_property_defaults[N_RADIUS]+5));

    nodetextmap[i]=new Gnome::Canvas::Text(displayed_graph,
        text_pos.x, text_pos.y, "");
    nodetextmap[i]->property_fill_color().set_value("darkblue");
    nodetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false);
  }

  updateScrollRegion();
}

void GraphDisplayerCanvas::clear()
{
  active_node=INVALID;
  active_edge=INVALID;
  forming_edge=INVALID;

  for (NodeIt n(mapstorage.graph); n != INVALID; ++n)
  {
    delete nodesmap[n];
    delete nodetextmap[n];
  }

  for (EdgeIt e(mapstorage.graph); e != INVALID; ++e)
  {
    delete edgesmap[e];
    delete edgetextmap[e];
  }
}
