import '../api/garden_api.service.dart';
import '../constance.dart';
import '../entities/beet.dart';
import '../entities/beet_row.dart';
import '../entities/plant.dart';
import '../logic/beet.service.dart';
import '../logic/beet_row.service.dart';
import '../logic/plant.service.dart';

class BeetRepository {
  late Beet beet;
  final BeetService beetService;
  final BeetRowService beetRowService;
  final PlantService plantService;
  final GardenApiService apiService;

  late String _messages;

  BeetRepository(this.beetRowService, this.plantService, this.apiService,
      this.beetService) {
    beet = Beet();
    _messages = "Keine Meldungen";
  }

  String get messages => _messages;

  bool get newRowAllowed => beet.beetRows.length < Constance.maxNumberOfRows;

  void addPlantToRowById(int rowId, Plant plant) {
    beetService.addPlantToRowById(beet, beetRowService, rowId, plant);
  }

  void addNewRowToBeet() {
    beetService.addNewRow(beet);
  }

  double getMaxHorizontalSpace() {
    return beetService.getMaxHorizontalSpaceOfRows(beetRowService, beet);
  }

  bool isActionNeeded(DateTime date) {
    return beetService.isActionNeeded(beetRowService, plantService, beet, date);
  }

  List<double> getVerticalSpaceValue() {
    return beetService.getVerticalPlantingSpace(beetRowService, beet);
  }

  void removePlantFromRowById(int rowId, int plantInRowId) {
    var row = beetRowService.getRowById(beet, rowId);
    beetRowService.removePlantFromRowById(row, plantInRowId);
  }

  Future<void> saveBeet() async {
    _messages = await beetService.saveBeet(apiService, beet);
  }

  Future<Iterable<Plant>> getAllPlants() async {
    return apiService.getAllAvailablePlants();
  }

  getBackgroundColorOfPlant(Plant plant, DateTime date) {
    return plantService.getColor(plant, date);
  }

  String getTimeDescription(Plant plant, DateTime date) {
    return plantService.getTimeDescription(plant, date);
  }

  Future<void> loadBeet() async {
    await beetService.loadBeet(apiService, beetRowService, beet);
    _messages = "Beet geladen";
  }

  List<double> getHorizontalPlantingPositionForRow(BeetRow beetRow) {
    return beetRowService.getHorizontalPlantingPosition(beetRow);
  }

  BeetRow getRow(int index) {
    return beet.beetRows[index];
  }
}
