import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/customer.dart';
import 'package:aitrainer_app/model/evaluation.dart';
import 'package:aitrainer_app/model/evaluation_attribute.dart';
import 'package:aitrainer_app/util/enums.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class EvaluationRepository {
  List<Evaluation>? evaluations;

  EvaluationRepository() {
    evaluations = Cache().evaluations;
  }

  String getEvaluationTextByExerciseType(int exerciseTypeId, double value) {
    String? eval;

    Evaluation? evaluation = this.getEvaluationByExerciseTypeId(exerciseTypeId);
    Customer? customer = Cache().userLoggedIn;

    if (evaluation != null && customer != null) {
      int? age = customer.age;
      if (age != null) {
        for (var attribute in evaluation.attributes) {
          EvaluationAttribute attr = attribute as EvaluationAttribute;
          if (age >= attr.ageMin && age <= attr.ageMax && value >= attr.valueMin && value <= attr.valueMax) {
            eval = attr.evaluationText;
            break;
          }
        }
      }
    }

    if (eval == null) {
      eval = EvaluationText.fair.toStr();
    }
    return eval;
  }

  Evaluation? getEvaluationByExerciseTypeId(int exerciseTypeId) {
    Evaluation? eval;
    if (evaluations != null) {
      for (var evaluation in evaluations!) {
        if (evaluation.exerciseTypeId == exerciseTypeId) {
          eval = evaluation;
          break;
        }
      }
    }
    return eval;
  }

  Color getEvaluationColor(String eval) {
    Color color = Colors.yellow[100]!;

    if (EvaluationText.very_poor.equalsStringTo(eval)) {
      return Colors.red[800]!;
    } else if (EvaluationText.poor.equalsStringTo(eval)) {
      return Colors.red[400]!;
    } else if (EvaluationText.below_average.equalsStringTo(eval)) {
      return Colors.orange[600]!;
    } else if (EvaluationText.average.equalsStringTo(eval)) {
      return Colors.yellow[600]!;
    } else if (EvaluationText.above_average.equalsStringTo(eval)) {
      return Colors.greenAccent;
    } else if (EvaluationText.good.equalsStringTo(eval)) {
      return Colors.green[400]!;
    } else if (EvaluationText.excellent.equalsStringTo(eval)) {
      return Colors.green[600]!;
    } else if (EvaluationText.elite.equalsStringTo(eval)) {
      return Colors.green[800]!;
    }

    return color;
  }
}
