/*
 * Copyright (C) 2022 aricouar
 *
 * 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>.
 */
package simulationplans;

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

import java.io.Writer;
import org.nuiton.math.matrix.*;
import org.nuiton.util.*;
import org.nuiton.topia.*;
import resultinfos.*;

import fr.ifremer.isisfish.annotations.Doc;
import fr.ifremer.isisfish.*;
import fr.ifremer.isisfish.simulator.SimulationContext;
import fr.ifremer.isisfish.types.TimeStep;
import fr.ifremer.isisfish.entities.*;
import fr.ifremer.isisfish.simulator.SimulationPlan;
import fr.ifremer.isisfish.simulator.SimulationPlanContext;
import fr.ifremer.isisfish.simulator.SimulationParameter;
import fr.ifremer.isisfish.datastore.SimulationStorage;
import fr.ifremer.isisfish.simulator.ResultManager;

import fr.ifremer.isisfish.rule.Rule;
import fr.ifremer.isisfish.rule.RuleHelper;
import fr.ifremer.isisfish.datastore.RuleStorage;
import rules.EffortReduction;

import java.util.Properties;
import java.util.List;
import java.io.File;
import java.io.FileReader;

/**
 * MyPlan1.java
 */
public class MyPlan1 implements SimulationPlan {

    public int param_parameterNumber = 1;
    public int param_first = 0;
    public int param_simulationNumber = 2;

    public String param_directory = "C:/Users/aricouar/Documents/MesProjets_local/ISIS_simul/isis-fish-4.4.7.2/input/effort-ranges";
    public String param_MATRIX = "range1"; //contains the value of effort reduction to be simulated
    private MatrixND matrix = null;

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


    protected String[] necessaryResult = {
        // put here all necessary result for this rule
        // example:
        // MatrixBiomass.NAME,
        // MatrixNetValueOfLandingsPerStrategyMet.NAME,
    };

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

    /**
     * Permet d'afficher a l'utilisateur une aide sur le plan.
     * @return L''aide ou la description du plan
     */
    @Override
    public String getDescription() throws Exception {
        // TODO change description
        return "TODO MyPlan1 description plan";
    }

    /**
     * Called once before {@code beforeSimulation} call.
     * 
     * @param context plan context
     */
    @Override
    public void init(SimulationPlanContext context) throws Exception {
        // TODO
        // Reads the range of effort reduction factor to be simulated
       File dir = new File(param_directory);
        
       matrix = MatrixFactory.getInstance().create(new int[]{param_simulationNumber, param_parameterNumber});
       matrix.importCSV(new FileReader(new File(dir, param_MATRIX + ".csv")), new int[]{0,0});
        
    }

    /**
     * Call before each simulation.
     * 
     * @param context plan context
     * @param nextSimulation storage used for next simulation
     * @return true if we must do next simulation, false to stop plan
     * @throws Exception
     */
    @Override
    public boolean beforeSimulation(SimulationPlanContext context, SimulationStorage nextSimulation) throws Exception {
        
        int simNum = nextSimulation.getParameter().getSimulationPlanNumber() + param_first;
        System.out.println("etape3 : determiner getDouble valeur simNum : " + simNum);

        
        if (simNum+param_first < param_simulationNumber) {
           
            //////////////////////////////////////////////////////////////////////////////////////////
            // Modif rule
            List<Rule> paramRules = nextSimulation.getParameter().getRules();

            nextSimulation.getParameter().addExtraRules("EffortReduction");

            double param_PercentReduction = matrix.getValue(simNum,0);

            System.out.println("etape4 , PercentReduction =" + param_PercentReduction);

            EffortReduction rule = new EffortReduction();
            rule.param_beginStep = new TimeStep(0);
            rule.param_endStep = new TimeStep(132);
            rule.param_PercentReduction = param_PercentReduction;
            paramRules.add(rule);

            System.out.println("etape4 : fin du changement de reduction");
            return true;
        }else return false;

    }          
       

    /**
     * Call after each simulation.
     * 
     * @param context plan context
     * @param lastSimulation storage used for simulation
     * @return true if we must do next simulation, false to stop plan
     * @throws Exception
     */
    @Override
    public boolean afterSimulation(SimulationPlanContext context,
            SimulationStorage lastSimulation) throws Exception {
        
        return true;
    }
}
