package scripts;

import java.util.List;

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

import org.codelutin.math.matrix.*;

import fr.ifremer.isisfish.entities.Population;
import fr.ifremer.isisfish.entities.PopulationGroup;
import fr.ifremer.isisfish.entities.Species;
import fr.ifremer.isisfish.simulator.SimulationContext;
import fr.ifremer.isisfish.types.Date;

/**
 * ChangeMatrixBackend.java
 *
 * Created: 6 septembre 2006
 *
 * @author anonymous <anonymous@labs.libre-entreprise.org>
 * @version $Revision: 1.3 $
 *
 * Last update: $Date: 2007-11-20 15:51:05 $
 * by : $Author: bpoussin $
 */
public class RuleUtil {

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

    /**
     * @param context le context de simulation
     * @param species l'espece sur lequel on souhaite le total
     * @param date la date pour laquel on veut le calcul, cet argument est
     * passe seulement pour que le cache ne retourne pas toujours la meme valeur
     * @return
     */
    public static double getTotalCatchTons(SimulationContext context, Species species, Date date) {
        double result = 0;
        for (Population pop : species.getPopulation()) {
            MatrixND mat = context.getPopulationMonitor().getHoldCatch(pop);
            if(mat!=null) {
                mat = mat.copy();
                mat = mat.sumOverDim(0); // sum over Strategies
                mat = mat.sumOverDim(1); // sum over metiers
                mat = mat.sumOverDim(3); // sum over zones
//                List<PopulationGroup> groups = mat.getSemantics(2);

                List<PopulationGroup> groups = pop.getPopulationGroup();
                
                for (int c=0; c<groups.size(); c++) {
                    PopulationGroup group = groups.get(c);
                    double weight = group.getMeanWeight();
                    result += mat.getValue(0, 0, c, 0) * weight / 1000.0;
                }
            }
        }
        return result;
    }

    
    
    
}
