package exports;

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

import java.io.Writer;

import static org.codelutin.i18n.I18n._;
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)

/**
 * Abundance.java
 *
 * Created: 1 septembre 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 Abundances") 
public class Abundances implements Export {

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

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

    public String getExportFilename() {
        return "Abondances";
    }
    
    public String getExtensionFilename() {
        return ".csv";
    }
    
    public String getDescription() {
        return _("Export les abondances en nombre 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_ABUNDANCE);
            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");
            }
        }
/*
writeln("debut export abondances");
var dateexport=new Packages.java.util.Date();
var formatteur= new Packages.java.text.SimpleDateFormat();
writeln("heure de debut: "+formatteur.format(dateexport));

var Parametre=sim.getParametre();
var Populations=Parametre.getPopulations();
var PDate=Packages.fr.ifremer.nodb.Date;

var result="";

var finsimu=resultats.getLastDate().getDate();

for (var ipop=Populations.iterator();ipop.hasNext();){
	var pop=ipop.next();
	writeln("population "+pop.getNom());
	for (var idate=0;idate<=finsimu;idate++){
		var date=new PDate(idate);

		var matrice=resultats.getMatrix(date,pop, "Abondance");
		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 abondances");

return ""+result;
*/
    }

}
