#include <LEDA/graphics/graphwin.h>
#include <LEDA/graph/graph_alg.h>

using namespace leda;

using std::cout;
using std::endl;


void highlight(GraphWin& gw, list<node> V,
                             list<edge> E,
                             node_array<int>& kind)
{
  const graph& G = gw.get_graph();
  bool flush0 = gw.set_flush(false);

  node v;
  forall_nodes(v,G) {
     switch (kind[v]) {
   
        case 0: gw.set_color(v,grey1);
        gw.set_border_color(v,grey1);
        gw.set_label_color(v,grey2);
        break;
      
        case 2: gw.set_color(v,grey1);
        gw.set_label_type(v,no_label);
        gw.set_width(v,8);
        gw.set_height(v,8);
        break;
      
        case 3:
        case 4: gw.set_shape(v,rectangle_node);
        gw.set_color(v,red);
        break;
      
        case -3: gw.set_shape(v,rectangle_node);
        gw.set_color(v,blue2);
        break;
      }
  }

  edge e;
  forall_edges(e,G) gw.set_color(e,grey1);

  forall(e,E)
  { gw.set_color(e,black);
    gw.set_width(e,2);
   }

  gw.redraw();
  gw.set_flush(flush0);
}


int main()
{
  GraphWin gw("Leda Graph Editor");

  gw.set_edge_direction(undirected_edge);

  gw.display(window::center,window::center);

  graph& G = gw.get_graph();

  while (gw.edit())
  { 
    if (PLANAR(G))
    { if (G.number_of_nodes() < 3) continue;

      gw.save_all_attributes();

      node_array<double> xcoord(G);
      node_array<double> ycoord(G);
      STRAIGHT_LINE_EMBEDDING(G,xcoord,ycoord);

      node v;
      forall_nodes(v,G) {
        gw.set_width(v,16);
        gw.set_height(v,16);
        gw.set_label_type(v,no_label);
      }

      gw.adjust_coords_to_win(xcoord,ycoord); 
      gw.set_layout(xcoord,ycoord);

      gw.wait("This Graph is planar. \
               I show you a PLANAR DRAWING (click done).");
      gw.restore_all_attributes();
    }
    else
    { 
      list<node> V_k;
      list<edge> E_k;
      node_array<int> kind(G);
      KURATOWSKI(G,V_k,E_k,kind);

      gw.save_all_attributes();
      highlight(gw,V_k,E_k,kind);

      gw.wait("This Graph is not planar. \
               I show you a KURATOWSKI SUBDIVISION (click done).");
      gw.restore_all_attributes();
    }

   }

  return 0;
}
