package exports;

import static org.codelutin.i18n.I18n._;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Writer;

import org.codelutin.math.matrix.*;

import scripts.ResultName;

import fr.ifremer.isisfish.entities.*;
import fr.ifremer.isisfish.export.Export;
import fr.ifremer.isisfish.types.Date;
import fr.ifremer.isisfish.datastore.SimulationStorage;
import fr.ifremer.isisfish.datastore.ResultStorage;

import fr.ifremer.isisfish.util.Doc; // pour pouvoir afficher une aide contextuelle (BUG#1605)

/**
 * Biomasses.java
 *
 * Created: 23 novembre 2006
 *
 * @author anonymous <anonymous@labs.libre-entreprise.org>
 * @version $Revision: 1.3 $
 *
 * Last update: $Date: 2007-05-24 09:30:07 $
 * by : $Author: bpoussin $
 */
 @Doc(value="do the doc of class Biomasses")
public class Biomasses implements Export {

    /** to use log facility, just put in your code: log.info("..."); */
    static private Log log = LogFactory.getLog(Biomasses.class);

    protected String [] necessaryResult = {
            ResultName.MATRIX_BIOMASS
    };
    
    public String[] getNecessaryResult() {
        return this.necessaryResult;
    }

    public String getExportFilename() {
        return "Biomasses";
    }
    
    public String getExtensionFilename() {
        return ".csv";
    }
    
    public String getDescription() {
        return _("Export les biomasses tableau avec des lignes pop;id;zone;date;nombre");
    }

    public void export(SimulationStorage simulation, Writer out) throws Exception {
        for (Population pop : simulation.getParameter().getPopulations()) {
            MatrixND mat = simulation.getResultStorage().getMatrix(pop, ResultName.MATRIX_BIOMASS);
            for (MatrixIterator i=mat.iterator(); i.hasNext();) {
                i.next();
                Object [] sems = i.getSemanticsCoordinates();
                Date date = (Date)sems[0];
                PopulationGroup group = (PopulationGroup)sems[1];
                Zone zone = (Zone)sems[2];
                
                double val = i.getValue();
                out.write(pop.getName() +";"+ group.getId() +";"+ zone.getName() +";"+ date.getDate() +";"+ val +"\n");
            }
        }

        
        /*
var Parametre=sim.getParametre();
var Populations=Parametre.getPopulations();
var PDate=Packages.fr.ifremer.nodb.Date;
var finsimu=resultats.getLastDate().getDate();


var result="";
writeln("debut export biomasse");
var dateexport=new Packages.java.util.Date();
var formatteur= new Packages.java.text.SimpleDateFormat();
writeln("heure de debut: "+formatteur.format(dateexport));


for (var ipop=Populations.iterator();ipop.hasNext();){
	var pop=ipop.next();
	for (var idate=0;idate<=finsimu;idate++){
		var date=new PDate(idate);
		var matrice=resultats.getMatrix(date,pop, "matriceBiomass");
		var BadZones=matrice.getSemantics(1);
		var Zones = new Packages.java.util.ArrayList(BadZones);
		var BadClasses=matrice.getSemantics(0);
		var Classes=new Packages.java.util.ArrayList(BadClasses);
  		for(var iz=Zones.iterator(); iz.hasNext();){
			var z=iz.next();
			for(var ic=Classes.iterator(); ic.hasNext();){
				var c = ic.next();
				result+=pop.getNom()+";"+c.getId()+";"+z.getNom()+";"+idate+";"+matrice.getValue(c,z)+"\n";
			}
		}
	}
}
writeln("fin export biomasse");

return ""+result;
*/
    }

}
