import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:garden_planner/entities/plant.dart';
import 'package:garden_planner/entities/plant_in_row.dart';
import 'package:garden_planner/widgets/content_widgets/dashboard_widgets/plant_element.dart';

import '../../../helpers/beet_repository_generator.dart';
import '../../../helpers/plant_generator.dart';

void main() {
  testWidgets('PlantElement image onRemovePlant fires',
      (WidgetTester tester) async {
    bool isRemoved = false;
    Plant? plantToRemove;

    // Arrange
    onRemovePlant(Plant plant) {
      isRemoved = true;
      plantToRemove = plant;
    }

    final plant = PlantInRow(position: 1, plant: PlantGenerator.getPlant());

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: PlantElement(
            showImages: true,
            onRemovePlant: onRemovePlant,
            plant: plant,
            beetRepository: BeetRepositoryGenerator.getBeetRepository(),
            date: DateTime.now(),
          ),
        ),
      ),
    );

    // Act
    final deleteButtonFinder = find.byIcon(Icons.delete);
    await tester.tap(deleteButtonFinder);
    await tester.pump();

    //Assert
    expect(isRemoved, true, reason: 'Plant is not removed');
    expect(plantToRemove, plant, reason: 'Wrong Plant is returned');
  });

  testWidgets('PlantElement Text onRemovePlant fires',
      (WidgetTester tester) async {
    bool isRemoved = false;
    Plant? plantToRemove;

    // Arrange
    onRemovePlant(Plant plant) {
      isRemoved = true;
      plantToRemove = plant;
    }

    final plant = PlantInRow(position: 1, plant: PlantGenerator.getPlant());

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: PlantElement(
            showImages: false,
            onRemovePlant: onRemovePlant,
            plant: plant,
            beetRepository: BeetRepositoryGenerator.getBeetRepository(),
            date: DateTime.now(),
          ),
        ),
      ),
    );

    // Act
    final deleteButtonFinder = find.byIcon(Icons.delete);
    await tester.tap(deleteButtonFinder);
    await tester.pump();

    //Assert
    expect(isRemoved, true, reason: 'Plant is not removed');
    expect(plantToRemove, plant, reason: 'Wrong Plant is returned');
  });
}
