package h03;

import h03.robots.DoublePowerRobot;
import h03.robots.MovementType;

import javax.swing.text.html.parser.TagElement;
import java.awt.*;

public class RobotsChallenge {

    private final int winThreshold = 2;

    private final int begin;

    private final int goal;

    private final DoublePowerRobot[] robots;

    public RobotsChallenge(int begin, int goal, DoublePowerRobot... robots) {
        this.goal = goal;
        this.robots = robots;
        this.begin = begin / 2;
    }

    public int calculateStepsDiagonal() {
        return Math.abs(begin - goal);
    }

    public int calculateStepsOverstep() {
        var diff = Math.abs(begin - goal);
        if (diff % 2 == 0) {
            return diff;
        }
        return diff+1;
    }

    public int calculateStepsTeleport() {
        var diff = Math.abs(begin - goal);
        if (diff % 2 == 0) {
            return diff / 2;
        }
        return (diff / 2) + 2;
    }

    public int calculateSteps(MovementType type) {
        return switch (type) {
            case DIAGONAL -> calculateStepsDiagonal();
            case OVERSTEP -> calculateStepsOverstep();
            case TELEPORT -> calculateStepsTeleport();
        };
    }

    public DoublePowerRobot[] findWinners() {
        var arr = new DoublePowerRobot[robots.length];
        int index = 0;

        for (var r : robots) {
            if (Math.min(calculateSteps(r.getType()), calculateSteps(r.getNextType())) <= winThreshold) {
                arr[index++] = r;
            }
        }

        return arr;
    }
}
