package G1.market_final;

import java.awt.Graphics;

public class Salesman  extends Thread implements Drawable{
	private int x,y;
	private int c;
	private boolean full;
	private int dx,dy;
	private MarketPanel panel;
	private int eps=5;
	
	public Salesman(int x, int y, int c, boolean full, int dx, int dy, MarketPanel panel) {
		super();
		this.x = x;
		this.y = y;
		this.c = c;
		this.full = full;
		this.dx = dx;
		this.dy = dy;
		this.panel=panel;
	}
	
	@Override
	public void drawSelf(Graphics g) {
		if(!full)
			g.drawOval(x, y, 10,10);
		else
			g.fillOval(x, y, 10,10);
	}
	
	private void move(){
		Market m = panel.getMarket(x,y);
		if(m!=null){
			m.doSale(this);
			if((x-m.getX())<eps || (m.getX()+m.getW()-x)<eps)
				dx*=-1;
			if((y-m.getY())<eps || (m.getY()+m.getH()-y)<eps)
				dy*=-1;
		}
		if(x<=0 || x>=MarketGUI.w)
			dx*=-1;
		if(y<=0 || y>=MarketGUI.h)
			dy*=-1;
		x+=dx;
		y+=dy;
	}
	
	@Override
	public void run() {
		while(true){
			move();
			panel.repaint();
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public boolean isFull(){
		return full;
	}

	public int getCapacity() {
		return c;
	}

	public void setFull(boolean b) {
		this.full=b;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}
}
