/*
 * Pheromone.java
 *
 * Created on 15 October 2006, 23:31
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ants;

import ants.PheromoneParticle;
import java.awt.Color;
import java.awt.Point;
import java.util.HashMap;
import java.util.Vector;

/**
 *
 * @author James Hamilton
 */
public class Pheromone extends MyObject implements Runnable {
    
    
    private Vector<PheromoneParticle> particles = new Vector<PheromoneParticle>();
    
    
    private double strength = 0;
    private Thread move;
    private int rate = 90000;
    private boolean go = true;
    private int maxStrength = 3;
    
    private int direction = -1;
    
    public static final int AWAY_FROM_NEST = 1;
    public static final int TOWARD_NEST = 2;
    
    /** Creates a new instance of Pheromone */
    public Pheromone() {
        setStrength(0);
                    move = new Thread(this);
            move.setDaemon(true);
            move.start();
    }
    
    public Pheromone(GridSquare gs) {
        super(gs);
        move = new Thread(this);
        move.setDaemon(true);
        move.start();
        setStrength(0);

    }
    
    public void run() {
        while(direction != -1) {
            
            try {
            
                    move.sleep(rate);//strength * 1000);
                    if(particles.size() > 0)  particles.remove(0);
                       
            }catch (InterruptedException e) {
                System.out.println(e);
            }
            
        }
    }
    
    
    public void increaseStrength(Ant ant, int direction) {
        particles.add(new PheromoneParticle(ant, direction));
    }
    
    
    
    public void increaseStrength(int direction) {
        strength += 0.5;
        Point thisP = getGridSquare().getCoordinateXY();
        GridVector squares = getGridSquare().getGridSquares(5);
        for(GridSquare s : squares) {
             Point p = s.getCoordinateXY();
             double c2 = Math.pow( thisP.x - p.x , 2) + Math.pow( thisP.y - p.y , 2);
             
             s.getPheromone().increaseStrength(1/(c2*500));
        }  
        
        if(strength > maxStrength) strength = maxStrength;
    }
    
    public void increaseStrength() {
       
        strength += 0.5;
        Point thisP = getGridSquare().getCoordinateXY();
        
        GridVector squares = getGridSquare().getGridSquares(5);
        for(GridSquare s : squares) {
             Point p = s.getCoordinateXY();
             double c2 = Math.pow( thisP.x - p.x , 2) + Math.pow( thisP.y - p.y , 2);
             
             s.getPheromone().increaseStrength(1/(c2*500));
        }
            
         if(strength > maxStrength) strength = maxStrength;
    }

    public void increaseStrength(double n) {
       
        strength += n;
         if(strength > maxStrength) strength = maxStrength;
    }    
    
    public Color getColor() {
     
        int direction = getDirection();
        if(direction == -1) {
            return Color.black;
        }else if(direction == PheromoneParticle.AWAY_FROM_NEST) {
            return Color.blue;
        }else{
            return Color.green;
        }
    }

    public double getStrength() {
        return particles.size();
    }

    public void setStrength(int strength) {
        if(strength > 10) strength = 10;
        this.strength = strength;
    }
    
    public void start() {
        go = true;
        if(move == null) {
                            move = new Thread(this);
            move.setDaemon(true);
            move.start();
        }
            
    }
    
    public void stop() {
        go = false;
        strength = 0;
    }

    public int getDirection() {
         if(particles.size() == 0) return -1;
         
        int toward_nest = 0;
        int away_from_nest = 0;
        
        for(PheromoneParticle p : particles) {
            if(p.getDirection() == PheromoneParticle.AWAY_FROM_NEST)
                away_from_nest++;
            else if(p.getDirection() == PheromoneParticle.TOWARD_NEST)
                toward_nest++;
        }
              
        return toward_nest > away_from_nest ? PheromoneParticle.TOWARD_NEST : PheromoneParticle.AWAY_FROM_NEST;
    }
    
    public boolean laidBy(Ant ant) {
        for(PheromoneParticle p : particles) {
            if(p.getAnt().equals(ant)) return true;
        }
        
        return false;
    }

    public int getDirection(Ant ant) {
        if(particles.size() == 0) return -1;
        
        int toward_nest = 0;
        int away_from_nest = 0;
     
        for(int i = particles.size(); i >= 0; i--) {
            PheromoneParticle p = particles.elementAt(i);
            if(!p.getAnt().equals(ant)) continue;
            
            if(p.getDirection() == PheromoneParticle.AWAY_FROM_NEST)
                away_from_nest++;
            else if(p.getDirection() == PheromoneParticle.TOWARD_NEST)
                toward_nest++;
        }
        
         return toward_nest > away_from_nest ? PheromoneParticle.TOWARD_NEST : PheromoneParticle.AWAY_FROM_NEST;
    }
    
    public void setDirection(int direction) {
       
        this.direction = direction;
    }
    
    public String toString() {
        int toward_nest = 0;
        int away_from_nest = 0;
        
        for(PheromoneParticle p : particles) {
            if(p.getDirection() == PheromoneParticle.AWAY_FROM_NEST)
                away_from_nest++;
            else if(p.getDirection() == PheromoneParticle.TOWARD_NEST)
                toward_nest++;
        }
        return "T: " + toward_nest  + ", A: " + away_from_nest + ", direction: " + getDirection();
    }
}

