/*
 * #%L
 * IsisFish data
 * %%
 * Copyright (C) 2014 Ifremer, Code Lutin, Chatellier Eric
 * %%
 * 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 2 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-2.0.html>.
 * #L%
 */

package scripts;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.nuiton.topia.TopiaException;

import exports.Abundance_ExportEnd;
import exports.Biomasse_ExportEnd;
import exports.MortalitePecheGroupe;
import exports.Landings_ExportEnd;
import exports.GrossValues_ExportEnd;
import exports.Discards_ExportEnd;
import exports.CatchWeightSpStrYear;
import exports.CapturesPoids_exportEnd;
import exports.MortalitePecheTotale;
import exports.Discards_AgeStructure;
import exports.Captures_AgeStructure;
import exports.Landings_AgeStructure;
import exports.Landings_month;
import exports.RegionDefinition;
import exports.Captures_AgeStructure_month;
import fr.ifremer.isisfish.datastore.SimulationStorage;
import fr.ifremer.isisfish.simulator.*;
import fr.ifremer.isisfish.export.ExportInfo;
import fr.ifremer.isisfish.export.ExportHelper;

/**
 * Ce script sert �� rejouer des exports sur un grand nombre de simulation en lisant le
 * ResultStrage et en rejouant les fonctions d'export.
 * 
 * Ce script peut ��tre lanc�� via l'option "Evaluer" depuis l'interface des scripts d'Isis.
 * 
 * @author Eric Chatellier
 */
public class RunExportOnManySimulationResult {

    // nom des exports a rejouer (�� modifier selon le besoin)
    protected List<? extends ExportInfo> exports = Arrays.asList(

        //new Discards_AgeStructure(),
        new Captures_AgeStructure(),
        //new Landings_AgeStructure()
        //new Landings_month(),
        //new Captures_AgeStructure_month()
        //new RegionDefinition()
    );

    public void doAllExports() throws Exception {
        // prefix des noms de simulations (�� configurer)
        String simulationPrefix = "sim_DiscardLessRome_2017-03-10-14-57_";//"sim_DiscardLessRome0_2017-02-22-13-06_";
        int simulationCount = 11;

        // parcourt de chaque simualtion
        for (int index = 0; index <= simulationCount; index++) {
            
            // get current simulation
            String currentSimulationName = simulationPrefix + index;
            SimulationStorage currentSimulation = SimulationStorage.getSimulation(currentSimulationName);

            // temp fix for matrix semantics decorations (no opened transaction)
            SimulationContext.get().setSimulationStorage(currentSimulation);

            // run each export for this simulation
            runExports(currentSimulation, exports);
            System.out.println("Exports done on simulation " + currentSimulationName);
            
            // close all
            currentSimulation.closeStorage();
        }
    }

    protected void runExports(SimulationStorage currentSimulation, List<? extends ExportInfo> exports) throws Exception {
        File rootDirectory = currentSimulation.getDirectory();
        File exportDir = SimulationStorage.getResultExportDirectory(rootDirectory);
            
        for (ExportInfo export : exports) {
            File file = getExport(exportDir, export);
            ExportHelper.exportToFile(currentSimulation, export.getClass().getSimpleName(), file);
        }
    }

    protected File getExport(File destdir, ExportInfo exportInfo) {
        String filename = exportInfo.getExportFilename();
        String extension = exportInfo.getExtensionFilename();
        
        File file = new File(destdir, filename + extension);
        // prevent two export with same name
        // name MyExport.csv become MyExport_1.csv
        int val = 0;
        while (file.exists()) {
            val++;
            file = new File(destdir, filename + extension + "_" + val);
        }
        return file;
    }

    public static void main(String[] args) throws Exception {
        RunExportOnManySimulationResult runExportJob = new RunExportOnManySimulationResult();
        runExportJob.doAllExports();
    }
}
