import 'package:flutter/material.dart';

import '../constance.dart';
import '../entities/plant.dart';
import '../entities/plant_time.dart';
import 'date.helper.dart';

class PlantService {
  String getTimeDescription(final Plant plant, final DateTime date) {
    var plantTimes = _getPlantTimes(plant, date);
    if (plantTimes.isNotEmpty) {
      return plantTimes.first.description;
    }

    return Constance.defaultDescription;
  }

  Color getColor(final Plant plant, final DateTime date) {
    Color color = Constance.defaultColor;

    if (plant.times.isNotEmpty) {
      for (var time in plant.times) {
        if (DateHelper.isDateBetween(date, time.from, time.until)) {
          color = time.color;
          break;
        }
      }
    }

    return color;
  }

  bool isActionNeeded(final Plant plant, final DateTime date) {
    return _getPlantTimes(plant, date).any((time) => time.action);
  }

  List<PlantTime> _getPlantTimes(final Plant plant, final DateTime date) {
    return plant.times
        .where((time) => DateHelper.isDateBetween(date, time.from, time.until))
        .toList();
  }
}
