/*
 * Copyright (C) 2011 Camille
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 
package analyseplans;

import static org.nuiton.i18n.I18n._;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import scripts.ResultName;

import java.io.Writer;
import org.nuiton.math.matrix.*;
import org.nuiton.util.*;
import org.nuiton.topia.*;

import fr.ifremer.isisfish.util.Doc;
import fr.ifremer.isisfish.*;
import fr.ifremer.isisfish.simulator.SimulationContext;
import fr.ifremer.isisfish.types.Date;
import fr.ifremer.isisfish.entities.*;
import fr.ifremer.isisfish.simulator.AnalysePlan;
import fr.ifremer.isisfish.simulator.AnalysePlanContext;
import fr.ifremer.isisfish.simulator.SimulationParameter;
import fr.ifremer.isisfish.datastore.SimulationStorage;
import fr.ifremer.isisfish.datastore.ResultStorage;

/**
 * SELECTIVITY.java
 *
 * Created: 27 juin 2011
 *
 * @author Camille <user.name@vcs.hostName>
 * @version $Revision: 1545 $
 * Last update: $Date: 27 juin 2011 $
 * by : $Author: Camille $
 */
public class SELECTIVITY implements AnalysePlan {

    /** to use log facility, just put in your code: log.info("..."); */
    private static Log log = LogFactory.getLog(SELECTIVITY.class);
    
    public Population param_Population = null; // choix de la population : ici merlu
    public Engin1 param_Engin1=null; // j'en ai mis 4 pour pouvoir changer la selectivite de toutes les sortes de chaluts
    public Engin2 param_Engin2=null;
    public Engin3 param_Engin3=null;
    public Engin4 param_Engin4=null;
    public NbCourbes param_nb_courbe=7;  //nombre de courbes de selectivite a  tester
    
 
    public String [] necessaryResult = {
	    // put here all necessary result for this rule
	    // example: 
	    // ResultName.MATRIX_BIOMASS,
	    // ResultName.MATRIX_NET_VALUE_OF_LANDINGS_PER_STRATEGY_MET,
    };

    @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 descrition
        return _("TODO SELECTIVITY description plan");
    }

    /**
     * Appele au demarrage de la simulation, cette methode permet d''initialiser
     * des valeurs
     * @param simulation La simulation pour lequel on utilise cette regle
     */
    @Override
    public void init(AnalysePlanContext context) throws Exception {
      
    }

    /**
     * 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
     */
// Les parametres correspondants aux 7 courbes a tester
     double a1=-6.6117851;
     double b1=0.3487131;
     double a2=-7.0728618;
     double b2=0.3814865;
     double a3=-5.54;
     double b3=0.24;
     double a4=-5.29;
     double b4=0.21;
     double a5=-9.1;
     double b5=0.32;
     double a6=-9.3;
     double b6=0.24;
     double a7=-10.1;
     double b7=0.53;

// partie ou on decide si on refait une simulation ou si on s'arrete. 
         public boolean beforeSimulation(AnalysePlanContext context, SimulationStorage nextSimulation) throws Exception {
   	    boolean doNext = true;
log.info("before simulation");

	Equation eqs = compute (SimulationContext().getPopulation().getPopulationGroup().getMetier().getSelectivityEquation()); // on va chercher l'equation de selectivite dans ISIS
    	int number = nextSimulation.getParameter().getAnalysePlanNumber(); // numero de la simualtion suivante. 
    	
   	if (number = 1){ // changement des equations de selectivite chaque simulation. 
   		String eq1 = Math.exp(a1+b1*group.getLength())/(1+Math.exp((a1)+b1* group.getLength())); // equation pour la première simulation
   		eqs.setContent(eq1); // on met cette equation comme equation de selectivite dans ISIS
  	}
   	if (number = 2){
   		String eq2 = Math.exp(a2+b2*group.getLength())/(1+Math.exp((a2)+b2* group.getLength()));
   		eqs.setContent(eq2);
   	}
   	if (number = 3) {
   		String eq3 = Math.exp(a3+b3*group.getLength())/(1+Math.exp((a3)+b3* group.getLength()));
   		eqs.setContent(eq3);
   	} 
   	if (number = 4) {
   		String eq4 = Math.exp(a4+b4*group.getLength())/(1+Math.exp((a4)+b4* group.getLength()));
   		eqs.setContent(eq4);
   	} 
   	if (number = 5) {
   		String eq5 = Math.exp(a5+b5*group.getLength())/(1+Math.exp((a5)+b5* group.getLength()));
   		eqs.setContent(eq5);
   	} 
	if (number = 6) {
   		String eq6 = Math.exp(a6+b6*group.getLength())/(1+Math.exp((a6)+b6* group.getLength()));
   		eqs.setContent(eq6);
   	} 
	if (number = 7) {
   		String eq7 = Math.exp(a7+b7*group.getLength())/(1+Math.exp((a7)+b7* group.getLength()));
   		eqs.setContent(eq7);
   	} 
     else {
          doNext = false; // Si on a fait les 7 simulations, on s'arrete. Probleme : je ne me sers pas de nb_courbes_param et si on veut faire 8 simulations, il faut retourner dans le code et rajouter une ligne. 
     }
   	
   }
    }

    /**
     * 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(AnalysePlanContext context,
            SimulationStorage lastSimulation) throws Exception {
        if number<=param_nb_courbe{ // si le numero de la simulation est inferieur ou eal a  param_nb_courbe, on continue
        		return true; 
        }
        else{
        		return false; // sinon, on arrete les simulations. 
        }
    }
