#include <map_win.h>
#include <set>

bool MapWin::close_if_escape_is_pressed(GdkEventKey* e)
{
  if(e->keyval==GDK_Escape)
  {
    hide();
  }
  return true;
}

MapWin::MapWin(const std::string& title, MapStorage & mapst, GraphDisplayerCanvas & grdispc):gdc(grdispc),ms(mapst),table(PROPERTY_NUM, 2, false)
{
  set_title(title);
  set_default_size(200, 50);

  signal_key_press_event().connect(sigc::mem_fun(*this, &MapWin::close_if_escape_is_pressed));

  combo_array=new Gtk::Combo [PROPERTY_NUM];

  for(int i=0;i<PROPERTY_NUM;i++)
  {

    std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=ms.beginOfEdgeMaps();
    std::set<int> props;

    int actprop;

    //here we find out, which map is the default in MapStorage for this property, which are not
    for(int j=0;j<ms.numOfEdgeMaps();j++)
    {
      //this is the default value for this property
      if(emsi->second==&(ms.default_edgemaps[i]))
      {
	actprop=j;
      }
      //this is the other maps to show for this property
      for(int k=0;k<PROPERTY_NUM;k++)
      {
	if(emsi->second==&(ms.default_edgemaps[k]))
	{
	  props.insert(j);
	}
      }
      emsi++;
    }

    //combo_array[i].set_group(group);

    //filling in combo box with choices
    std::list<Glib::ustring> listStrings;

    listStrings.push_back("Default");

    emsi=ms.beginOfEdgeMaps();

    for(int j=0;j<ms.numOfEdgeMaps();j++)
    {
      if( ( props.find(j) )==( props.end() ) )
      {
	listStrings.push_back(emsi->first);
      }
      emsi++;
    }

    combo_array[i].set_popdown_strings(listStrings);

    //Restrict it to these choices only:
    combo_array[i].set_value_in_list();

    //binding signal to thew actual entry
    combo_array[i].get_entry()->signal_changed().connect
    (
     sigc::bind
     (
      sigc::mem_fun(*this, &MapWin::combo_changed),
      i
     )
    );

    //placing actual entry in the right place

    label=new Gtk::Label;
    label->set_text(property_strings[i]);

    //    labelpluscombo=new Gtk::HBox;
    //    labelpluscombo->pack_start(*label);
    //    labelpluscombo->pack_start(combo_array[i]);
        
    table.attach(*label,0,1,i,i+1,Gtk::SHRINK,Gtk::SHRINK,10,3);
    table.attach(combo_array[i],1,2,i,i+1,Gtk::SHRINK,Gtk::SHRINK,10,3);

    /*
    if(actpos<(ms.numOfEdgeMaps()-PROPERTY_NUM+1)/2)
    {
      vbox_r1.pack_start(*labelpluscombo);
    }
    else
    {
      vbox_r2.pack_start(*labelpluscombo);
    }
    actpos++;
    //*/

  }

  combos.pack_start(vbox_r1);
  combos.pack_start(vbox_r2);

  //add(combos);
  add(table);

  show_all_children();

}

void MapWin::combo_changed(int prop)
{

  //most nem kommentezem fel, mert ugyis valtozik
  Gtk::Entry* entry = combo_array[prop].get_entry();

  if(entry)
  {
    Glib::ustring mapname = entry->get_text();
    if(!(mapname.empty())) //We seem to get 2 signals, one when the text is empty.
    {
      if(mapname=="Default")
      {
	mapname=property_strings[prop];
      }

      if( (ms.edgemap_storage).find(mapname) != (ms.edgemap_storage).end() )
      {
	switch(prop)
	{
          case WIDTH:
	    gdc.changeLineWidth(mapname);
	    break;
          case COLOR:
	    gdc.changeColor(mapname);
	    break;
          case TEXT:
	    gdc.changeText(mapname);
	    break;
          default:
	    std::cout<<"Error\n";
	}
      }
    }
  }
};
