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)

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

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

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

    public String getExportFilename() {
        return "RejetsNombre";
    }
    
    public String getExtensionFilename() {
        return ".csv";
    }
    
    public String getDescription() {
        return _("Export les rejets en nombre de la simulation. tableau pop;metier;id;zone;date;nombre");
    }

    public void export(SimulationStorage simulation, Writer out) throws Exception {
        Date lastDate = simulation.getResultStorage().getLastDate();       
        
        for (Population pop : simulation.getParameter().getPopulations()) {
            for (Date date = new Date(0); !date.after(lastDate); date = date.next() ) {
                MatrixND mat = simulation.getResultStorage().getMatrix(date, pop, ResultName.MATRIX_DISCARDS_PER_STR_MET);
                if (mat != null) { // can be null if simulation is stopped before last year simulation
                    mat = mat.sumOverDim(0); //sum on strategy
                    for (MatrixIterator i=mat.iterator(); i.hasNext();) {
                        i.next();
                        Object [] sems = i.getSemanticsCoordinates();
                        Metier metier = (Metier)sems[1];
                        PopulationGroup group = (PopulationGroup)sems[2];
                        Zone zone = (Zone)sems[3];
                        
                        double val = i.getValue();
                        out.write(pop.getName() +";"+ metier.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 capture=0.0;
var result="";
writeln("debut de export rejets nombre");
var dateexport=new Packages.java.util.Date();
var formatteur= new Packages.java.text.SimpleDateFormat();
writeln("heure de debut: "+formatteur.format(dateexport));
var finsimu=resultats.getLastDate().getDate();

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 ,"Rejet par metier");
		if (matrice==null){
			matrice=MatrixFactory.create(resultats.getMatrix(new PDate(0),pop ,"Rejet par metier"));
			matrice.mults(0);
		}
		var temp=matrice.sumOverDim(0);
		writeln("on a la matrice");
		for (var iiterateur=temp.iterator();iiterateur.next();){
			var coordonnees=iiterateur.getSemanticsCoordinates();
			var metier=coordonnees[1];
			var c=coordonnees[2];
			var z=coordonnees[3];
			capture=iiterateur.getValue();
			result+=pop.getNom()+";"+metier.getNom()+";"+c.getId()+";"+z.getNom()+";"+idate+";"+capture+"\n";
		}
	}
}

writeln("fin de RejetsNombre");
return ""+result;
*/
    }

}
