/*
 *
 * modifyGroup :modify the number of nb_* parameter
 * modifyValue :modify a parameter value
 */
#include "Tools/Viewer/Bdd/include/BaseV.h"
#include "qstring.h"
namespace morpheo{
namespace tools{
namespace viewer{
namespace bdd{     

  int BaseV::modifyGroup(string namegroup,int step)
  {

    int i,level,position_tab_init,position_tab;
    Group *g; Param *p; QList<Param> p2;LimitParam lp;
    
    if(listgroup->indexOf(namegroup)==-1)
      return -1;
    
    switch (step){
    case 1:
      position_tab_init=listgroup->lastIndexOf(namegroup);
      level=(groups->operator[](position_tab_init)).getLevel();  
      //search the position to add a group
      if(level>=groups->operator[](position_tab_init+1).getLevel())
	position_tab=position_tab_init;
      else
	for(position_tab=position_tab_init ;position_tab+1<groups->size()&&level<groups->operator[](position_tab+1).getLevel();position_tab++);
      cout << position_tab << "         " << position_tab_init<<endl;
      //create a new group and add
      for(i=position_tab;i>=position_tab_init;i--)
	{
	  g=new Group(groups->operator[](i).getName(),groups->operator[](i).getLevel());
	  p2=groups->operator[](i).getParam();
	  for(QList<Param>::iterator it=p2.begin();it!=p2.end();++it)
	    {
	      
	      if(it->getName() == "id")
		{
		  p=new Param(it->getName(),it->getValue()+1,g->getName());
		  g->addParam(p);
		}
             else if(QString::fromStdString(it->getName()).contains("link",Qt::CaseInsensitive))
 		{
		  p=new Param(it->getName(),it->getValue(),g->getName());
 		  g->addParam(p);		  
 		}
	      else
		{
		  lp=searchLimitParam(it->getName());
		  p=new Param(lp.getName(),lp.getDefault(),g->getName());
		  g->addParam(p);
		}
	    }
	      groups->insert(position_tab+1,*g);
	      listgroup->insert(position_tab+1,g->getName());
	  
	  delete g;
	  
	}
      
      break;
    case -1:
      if(nbGroup(namegroup)==1)
	break;
      else 
	{
	  groups->removeAt(listgroup->lastIndexOf(namegroup));  
	  listgroup->removeAt(listgroup->lastIndexOf(namegroup));  
	}
      break;
    default:
      return -1;
    }
    return 0;
  }

  void BaseV::modifyValue(string nomgroup,string nameparam,int value)
  {  
    Group g=searchGroup(nomgroup);
    Param *np=new Param(nameparam,value,nomgroup); 
    g.addParam(nameparam,np); 
    delete np;
  }

};//end bdd
};//end viewer
};//end tools
};//end morpheo
