package at.acdp.urweb.fhpp;

import at.acdp.urweb.Params;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class FHMasterSim implements IFHMaster {
    private final static Logger logger = LoggerFactory.getLogger(FHMasterSim.class);

    private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    private volatile boolean started = false;
    private int targetPos=-1;
    private float curPos=-1;
    private float simSpeed=0.1f;


    public FHMasterSim() {
    }

    @Override
    public void start(Params app) throws InterruptedException {
        started = true;
        while(true) {
            Thread.sleep(30);
            curPos = curPos + ((targetPos - curPos) * simSpeed);
        }
    }

    @Override
    public void direktAuftrag(int pos, int speed, int start, boolean relative) {
        logger.info("direktAuftrag {},{},{},{}", pos, speed, start, relative);
        targetPos=pos;
    }

    @Override
    public CompletableFuture<Status> readStatus() {
        Status s=new Status();
        s.istPosition = (int) curPos;
        s.enabled = true;
        return CompletableFuture.completedFuture(s);
    }

    @Override
    public void bereitschaft() {
        logger.info("bereitschaft");
    }

    @Override
    public void stop() {
        started = false;
    }
}