/*save configuration file
 * name: file name
 * type: 0 ->user save a file 
 *       1 ->function apply() temporary save
 */
#include "Tools/Viewer/Parser/include/parser.h"
#include "qmessagebox.h"
namespace morpheo{
namespace tools{
namespace viewer{
namespace parser{

//   QDomElement Parse::createXMLNode( QDomDocument &d , morpheo::tools::viewer::bdd::Group &grp)
//   {
//     QDomElement cn;
//     cn= d.createElement( QString::fromStdString(grp.getName()));
      
//     QList<morpheo::tools::viewer::bdd::Param> p=grp.getParam();
//     if(p.size()!=0)
//       {
// 	for(QList<morpheo::tools::viewer::bdd::Param>::iterator it2=p.begin();it2!=p.end();++it2)
// 	  {
// 	    cn.setAttribute( QString::fromStdString(it2->getName()) ,it2->getValue() );
// 	  }
//       }
    
       
//     return cn;
//   }

  
  int Parse::save_file(string name,int type){
 //    int level=0;
//     QDomDocument doc( "Viewercfg" );
//     QDomElement root = doc.createElement("cpu"); 
//     QDomNode child=root.setAttribute("name",toplevel);
//     doc.appendChild(root);
    
    
//     QList<morpheo::tools::viewer::bdd::Group> g=base->getGroups();
//    for( QList<morpheo::tools::viewer::bdd::Group>::iterator it = g.begin(); it != g.end(); ++it )
//       {
// 	//3 cas
// 	if(it->getLevel()==0)
//  	  save=root.appendChild(createXMLNode( doc , it));
// 	if(it->getLevel()>level){
// 	  child=root.appendChild(createXMLNode( doc , it));
// 	  root=(QDomElement)child;
// 	}
// 	if(it->getLevel()==level){
// 	  root= createXMLNode( doc , it);
// 	  save=root;
// 	}
// 	if(it->getLevel()<level){
// 	  root.appendChild(createXMLNode( doc , it));
	  
// 	}
// 	root.appendChild(createXMLNode( doc , it));
    
//     }

    
    
    QString s; 
    if (type==0)
      s.append(QString::fromStdString(name));
    else {
      s.append(QString::fromStdString(name));
      s.append("~");
    }

    QFile file(s);
    if (!file.open(QIODevice::WriteOnly))
      {
	//QMessageBox::warning( this, "Saving", "Failed to save file." );
	cerr <<"Failed to save file. " <<endl;
	return -1;
      }
    QString ecart;
    QTextStream ts( &file );
    ts << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" <<endl<<endl;
    ts << "<cpu name=\"" << QString::fromStdString(toplevel) << "\" >" <<endl<<endl;
    
    
    QList<morpheo::tools::viewer::bdd::Group> *g=base->getGroups();
    for( QList<morpheo::tools::viewer::bdd::Group>::iterator it = g->begin(); it != g->end(); ++it )
      {

	ecart = "  ";
	if(it->getLevel() == 1)
	  ecart.append("     ");
	if(it->getLevel() == 2)
	  ecart.append("        ");
	if(it->getLevel() == 3)
	  ecart.append("          ");
	if(it->getLevel() == 4)
	  ecart.append("            ");
	if(it->getLevel() == 5)
	  ecart.append("              ");

	ts << ecart << "<" << QString::fromStdString(it->getName()) ;
	QList<morpheo::tools::viewer::bdd::Param> p=it->getParam();
	if(p.size()!=0)
	  {
	    for(QList<morpheo::tools::viewer::bdd::Param>::iterator it2=p.begin();it2!=p.end();++it2)
	      {
		ts<< " " << QString::fromStdString(it2->getName()) << "=\"" << it2->getValue() << "\" " ;
	      }
	    if (it->getLevel()==0 ||it->getName()=="front_end" ||it->getName()=="prediction_unit"||\
		it->getName()=="out_of_order_engine"||it->getName()=="execution_loop"||it->getName()=="reservation_station"||\
		it->getName()=="writeback"||it->getName()=="memory_unit")
	      ts  << "> " <<endl <<endl;
	    else
	      ts  << "/> " <<endl <<endl;
	  }
	else
	  {
	    ts << ">" <<endl <<endl;
	  }

	
	if((it+1)!=g->end() && (it+1)->getName()=="front_end" && (it)->getName()=="dcache")
	  ts << "     </cache_unit>" <<endl <<endl;
	  
	if((it+1)!=g->end() && (it+1)->getName()=="prediction_unit" && (it)->getName()=="ifetch")
	  ts << "        </fetch_unit>" <<endl <<endl;
	if((it+1)!=g->end() && (it+1)->getName()=="decod_unit" && (it)->getName()=="meta_predictor")
	  ts << "        </prediction_unit>" <<endl <<endl;
	  
	if((it+1)!=g->end() && (it+1)->getName()=="out_of_order_engine" && (it)->getName()=="decod")
	  {
	    ts << "        </decod_unit>" <<endl <<endl;
	    ts << "     </front_end>" <<endl <<endl;
	  }
	if((it+1)!=g->end() && (it+1)->getName()=="execution_loop" && (it)->getName()=="commit")
	  ts << "       </out_of_order_engine>" <<endl <<endl;
	if((it+1)!=g->end() && (it+1)->getName()=="memory_unit" && (it)->getName()=="link")
	  {
	    ts << "          </reservation_station>" <<endl <<endl;
	    ts << "        </read_unit>" <<endl <<endl;
	  }
	
	if((it+1)!=g->end() && (it+1)->getName()=="execution_unit" && (it)->getName()=="link")
	    ts << "        </memory_unit>" <<endl <<endl;

	if((it+1)!=g->end() && (it+1)->getName()=="write_unit" && (it)->getName()=="execution")
	    ts << "        </execution_unit>" <<endl <<endl;

	if((it+1)!=g->end() && (it+1)->getName()=="writeback" && (it)->getName()=="link")
	    ts << "        </writeback>" <<endl <<endl;
	
	
      }

    ts << "         </writeback>" <<endl <<endl;
    ts << "      </write_unit>" <<endl<<endl;
    ts << "    </execution_loop>" <<endl<<endl;
    ts << "  </core>" <<endl<<endl;
    ts << "</cpu>" <<endl<<endl;
    file.close();
    return 0;


  }


};//end parser
};//end viewer
};//end tools
};//end morpheo

