/* **********************************
 *   CreateAgent.java
 *     Handles the creation of new derivitives
 * **********************************/
package player.playeragent;

import player.*;
import edu.neu.ccs.demeterf.demfgen.lib.*;
import gen.*;

/** Class for creating a derivative */
public class CreateAgent implements PlayerI.CreateAgentI{
    static final double DEFAULT_PRICE = 0.8; 
    
    /** Returns a newly created derivative of a different type than already existing derivatives */
    public  Derivative createDerivative(Player player, final List<Type> existing){
        List<Pair<Integer,Double>> left = hidden.Tools.PREFS.filter(new List.Pred<Pair<Integer,Double>>(){
            public boolean huh(Pair<Integer,Double> p){ return !existing.contains(typeFor(p.a)); }});
        
        int rand = Util.random(left.length()/3);
        Type t = left.isEmpty()?Util.freshType(existing):
            typeFor(left.lookup(rand).a);
        
        Price price = new Price(calcPrice(t, left.isEmpty()?DEFAULT_PRICE:left.lookup(rand).b));
        
        Util.display(Util.color("Creating","green")+" "+Util.tag(t.print(),"b")+" @ "+
                Util.color(Util.format(price.val),"blue"));
        
        return new Derivative(Util.freshName(player), player.id, price, t);
    }
    
    /** Calculate Price */
    static double calcPrice(Type t, double breakEven){
        if(t.isSecret())return Math.min(1.0, (Util.random()*.1)+.9);
        return Math.min(1.0, breakEven+(Util.random()*.2));
    }
    
    
    /** Give a Type for a single RelationNr */
    static Type typeFor(int r){
        return new Type(/*Util.coinFlip()?*/new Secret(),//:new Classic(), 
                List.create(new TypeInstance(new RelationNr(r))));
    }
}