package alive;

/**
 * <ALIVE Project>
 * Environment Template class
 *
 * Alga
 *
 * By Rogerio Neves
 * LSI - POLI - USP
 */

import javax.vecmath.*;
import javax.media.j3d.*;

public class XP_Template_Env extends Environment {

  // Change class name to match your filename
  Color3f c = new Color3f(1,1,1);
  Actor bulb = new Actor_Sphere();

  // Example interface
  java.awt.Scrollbar sr = new java.awt.Scrollbar(1,100,1,0,201);
  java.awt.Scrollbar sg = new java.awt.Scrollbar(1,130,1,0,201);
  java.awt.Scrollbar sb = new java.awt.Scrollbar(1,100,1,0,201);
  java.awt.Label lr = new java.awt.Label("------");
  java.awt.Label lg = new java.awt.Label("------");
  java.awt.Label lb = new java.awt.Label("------");


  public XP_Template_Env(EnvConfig cfg) {
    // Change constructor name to match your class name
    configureEnvironment(cfg);  // Do not remove
    param = new double[6];      // set to total number of parameters +1
    paramName = new String[6];  // set to total number of parameters +1
    set(0,"temperature",20);    // name your chromossomes or parameters
    set(1,"ph",4);
    set(2,"r",0);
    set(3,"g",.3);
    set(4,"b",0);
    set(5,"friction",.05);
    message("\nTemplate started...");  // Ready message
    super.start(); // Starts the experiment
  }

  public void createContent() {

    // Replace with your own agents and objects

    bulb.emissiveColor.set(0,0,0);
    bulb.diffuseColor.set(0,0,0);
    bulb.specularColor.set(0,0,0);
    bulb.emissiveColor.set(0,0,0);
    bulb.shininess = 10;
    bulb.setAllColors();
    bulb.set(new Vector3d(0,0,-100),50);
    addActor(bulb);
    for (int i=0; i<config.POP_LIMIT/10;i++) addAgent(new XP_Template_Agent(this));
    rest(1000);  // Give time to agents to startup
  }

  public void createCustomUI() {

    // Replace with your interface or remove this method

    control.ui.add(new java.awt.Label("Experiment_Template Interface"));
    java.awt.Button b = new java.awt.Button("Add");
    control.ui.add(sr);
    control.ui.add(lr);
    control.ui.add(sg);
    control.ui.add(lg);
    control.ui.add(sb);
    control.ui.add(lb);
    b.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(java.awt.event.ActionEvent e) {b_actionPerformed();}});
    control.ui.add(b);
  }

  public void b_actionPerformed() {
    addAgent(new XP_Template_Agent(this));
  }

  public void update() {

    // Put environmental computations here

    set("r", (double) (sr.getValue()-100)/100);  // update color bars
    lr.setText(String.valueOf(get("r")));

    set("g", (double) (sg.getValue()-100)/100);
    lg.setText(String.valueOf(get("g")));

    set("b", (double) (sb.getValue()-100)/100);
    lb.setText(String.valueOf(get("b")));

    c.set((float)get("r"),(float)get("g"),(float)get("b"));
    bulb.setColor(c);

    for (int i=0; i<agents.size();i++) { // Friction for all agents
      if (agents.get(i)!=null) getAgent(i).speed.scale(1-get("friction"));
    }
  }

  // *** Main method: Makes experiment executable
  public static void main(String[] args) {
    EnvConfig cfg = new EnvConfig();
    new  XP_Template_Env(cfg);  // Must start this class, change name
  }
}