/*
 * #%L
 * IsisFish data
 * %%
 * Copyright (C) 2006 - 2016 Ifremer, CodeLutin
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
package exports;

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

import java.io.Writer;

import org.nuiton.math.matrix.*;

import fr.ifremer.isisfish.export.ExportStep;
import fr.ifremer.isisfish.types.TimeStep;
import resultinfos.MatrixMetierZone;
import fr.ifremer.isisfish.datastore.SimulationStorage;

/**
 * MetierZone.java
 */
public class MetierZone implements ExportStep {

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

    protected String [] necessaryResult = {
        MatrixMetierZone.NAME
    };

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

    @Override
    public String getExportFilename() {
        return "MetierZone";
    }

    @Override
    public String getExtensionFilename() {
        return ".csv";
    }

    @Override
    public String getDescription() {
        return "Export zone used by metier during simulation";
    }

    /*@Override
    public void export(SimulationStorage simulation, Writer out) throws Exception {
        TimeStep lastStep = simulation.getResultStorage().getLastStep();       
        
        for (TimeStep step = new TimeStep(0); !step.after(lastStep); step = step.next() ) {
            MatrixND mat = simulation.getResultStorage().getMatrix(step, MatrixMetierZone.NAME);
            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 + ";" + step.getStep() + "\n");
                    }
                }
            }
        }
    }*/

    @Override
    public void exportBegin(SimulationStorage simulation, Writer out) throws Exception {
        out.write("step;metier;zone\n");
    }

    @Override
    public void export(SimulationStorage simulation, TimeStep step, Writer out) throws Exception {
        MatrixND mat = simulation.getResultStorage().getMatrix(step, MatrixMetierZone.NAME);
        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(step.getStep() + ";" + metier + ";" + zone + "\n");
            }
        }
    }

    @Override
    public void exportEnd(SimulationStorage simulation, Writer out) throws Exception {
        
    }
}
