package market;

import java.awt.Graphics;

public class Salesman implements Runnable{
    int c;
    boolean full;
    int x, y, dx, dy;
    MarketArea area;
    
    public Salesman(int c, boolean full, 
	    int x, int y, int dx, int dy, 
	    MarketArea area) {
	this.c=c;
	this.full=full;
	this.x=x;
	this.y=y;
	this.dx=dx;
	this.dy=dy;
	this.area=area;
    }
    
    void drawSelf(Graphics g){
	if(!full)
	    g.drawOval(x,y,10,10);
	else
	    g.fillOval(x,y,10,10);
     }

    void move(){
	Market m= area.getMarket(this);
	if(m!=null){
	    m.doSale(this);
	    //ovde je potrebno implementirati logiku 
	    //odbijanja od trzista
	    //ispitati x i y koordinate, i odbiti se u
	    //adekvatnom pravcu
	}else{
	    if(x>=area.w || x<=0)
		dx*=-1;
	    if(y>=area.h || y<=0)
		dy*=-1;
	}
	x+=(dx*2);
	y+=(dy*2);
    }
    
    @Override
    public void run() {
	while(true){
	    move();
	    try {
		Thread.sleep(100);
	    } catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	    area.repaint();
	}
    }
}
