package h03.robots;

import fopbot.Robot;
import fopbot.RobotTrace;

import java.util.Arrays;
import java.util.Random;

public class HackingRobot extends Robot {

    private MovementType type;

    private MovementType[] robotTypes = new MovementType[] {
        MovementType.TELEPORT, MovementType.OVERSTEP, MovementType.DIAGONAL
    };


    public HackingRobot(int x, int y, boolean order) {
        super(x, y);

        if (order) { // right shift
            var t = robotTypes[2];
            robotTypes[2] = robotTypes[1];
            robotTypes[1] = robotTypes[0];
            robotTypes[0] = t;
        } else { // left shift
            var t = robotTypes[0];
            robotTypes[0] = robotTypes[1];
            robotTypes[1] = robotTypes[2];
            robotTypes[2] = t;
        }

        type = robotTypes[0];
    }

    public MovementType getType() {
        return type;
    }

    public MovementType getNextType() {
        var i = Arrays.asList(robotTypes).indexOf(type);
        i = (i + 1) % 3;
        return robotTypes[i];
    }

    public int getRandom(int limit) {
        return new Random().nextInt(limit);
    }

    public boolean shuffle(int itNr) {
        var old = type;
        for (int i = 0; i < itNr; i++) {
            int index = getRandom(3);
            type = robotTypes[index];
        }
        return type != old;
    }

    public void shuffle() {
        while (!shuffle(1));
    }
}
