package vassistent.service;


import vassistent.model.AppState;
import vassistent.model.ProblemLevel;

/**
 * Converts statistical ratios into problem levels and triggers animation updates.
 */
public class EvaluationService {
    private final StatisticsService statisticsService;
    private final AppState appState;
    private final AnimationFileService animationFileService;

    /**
     * Creates an evaluation service.
     *
     * @param statisticsService service used to calculate ratios
     * @param appState shared application state to update
     * @param animationFileService service used to propagate animation changes
     */
    public EvaluationService(
            StatisticsService statisticsService,
            AppState appState,
            AnimationFileService animationFileService
    ) {

        this.statisticsService = statisticsService;
        this.appState = appState;
        this.animationFileService = animationFileService;
    }

    /**
     * Recalculates the current ratio, updates state, and writes animation output.
     */
    public void evaluate() {
        double ratio = statisticsService.getRatio(20);

        ProblemLevel level = calculateLevel(ratio);

        appState.setProblemLevel(level);
        animationFileService.wirteAnimationState(level);
    }

    /**
     * Maps a ratio value to a problem level threshold.
     *
     * @param ratio computed ratio in the range {@code [0, 1]}
     * @return derived problem level
     */
    private ProblemLevel calculateLevel(Double ratio) {
        if (ratio >= 0.9) {
            return ProblemLevel.DISASTER;
        } else if (ratio >= 0.8) {
            return ProblemLevel.HIGH;
        } else if (ratio >= 0.5) {
            return ProblemLevel.WARNING;
        } else {
            return ProblemLevel.NONE;
        }
    }
}
