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)

/**
 * MetierZone.java
 *
 * Created: 17 janvier 2007
 *
 * @author bpoussin <bpoussin@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 MetierZone")
public class MetierZone implements Export {

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

    public String [] necessaryResult = {
            ResultName.MATRIX_METIER_ZONE,
    };

    public String[] getNecessaryResult() {
        return this.necessaryResult;
    }

    public String getExportFilename() {
        return "MetierZone";
    }
    
    public String getExtensionFilename() {
        return ".csv";
    }
    
    public String getDescription() {
        return _("Export zone used by metier during simulation");
    }

    public void export(SimulationStorage simulation, Writer out) throws Exception {
        Date lastDate = simulation.getResultStorage().getLastDate();       
        
        for (Date date = new Date(0); !date.after(lastDate); date = date.next() ) {
            MatrixND mat = simulation.getResultStorage().getMatrix(date, ResultName.MATRIX_METIER_ZONE);
            if (mat != null) { // can be null if simulation is stopped before last year simulation
                for (MatrixIterator i=mat.iterator(); i.hasNext();) {
                    i.next();
                    if (i.getValue() == 1) {
                        Object metier = i.getSemanticsCoordinates()[0];
                        Object zone = i.getSemanticsCoordinates()[1];
                        out.write(metier + ";" + zone + ";" + date.getDate() + "\n");
                    }
                }
            }
        }
    }

}
