package cz.vutbr.feec.muller;

import java.util.Random;

import cz.vutbr.feec.simlib.Proc;

public class Receiver extends Proc {
	// feedback
	private FT feedback = null;
	private final int time;
	// local value
	private int value;
	
	public Receiver(int value, int time) {
		this.value = value;
		this.time = time;
	}
	
	public FT getFeedback() {
		return feedback;
	}

	public void setFeedback(FT feedback) {
		this.feedback = feedback;
	}

	@Override
	public void action() {
		Random rnd = new Random();
		while(!getSimulation().isFinished()) {
			sleepProc((double)time * (rnd.nextDouble() + 0.5));
			sendRSI(value);
		}
	}

	private void sendRSI(Integer value2) {
		if(getFeedback() != null) {
			if(value == 0) {
				getFeedback().receiveBlack(0);
			} else {
				getFeedback().receiveWhite(1);
			}
		}
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}
}
