import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:garden_planner/constance.dart';
import 'package:garden_planner/widgets/header.dart';

void main() {
  testWidgets('Header widget displays correctly', (WidgetTester tester) async {
    //Arrange
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: Header(
            onSidebarToggle: () {},
            onSave: () {},
          ),
        ),
      ),
    );

    final sidebarToggleButton = find.byIcon(Icons.menu_open);
    final saveButton = find.byIcon(Icons.save);

    //Act
    expect(find.text(Constance.apptitle), findsOneWidget,
        reason: 'Title is missing');
    expect(sidebarToggleButton, findsOneWidget,
        reason: 'SidbarToggel is missing');
    expect(saveButton, findsOneWidget, reason: 'Savebutton is missing');
  });

  testWidgets('Header widget save abort should work',
      (WidgetTester tester) async {
    bool savePressed = false;

    //Arrange
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: Header(
            onSidebarToggle: () {},
            onSave: () {
              savePressed = true;
            },
          ),
        ),
      ),
    );

    final saveButton = find.byIcon(Icons.save);

    //Act
    await tester.tap(saveButton);
    await tester.pumpAndSettle();

    final saveDialog = find.byType(AlertDialog);
    expect(saveDialog, findsOneWidget);

    // Test false
    final cancelButton = find.text('Abbrechen');
    await tester.tap(cancelButton);
    await tester.pumpAndSettle();

    //Assert
    expect(savePressed, false, reason: 'save was not confirmed but saved');
  });

  testWidgets('Header widget save ok should work', (WidgetTester tester) async {
    bool savePressed = false;

    //Arrange
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: Header(
            onSidebarToggle: () {},
            onSave: () {
              savePressed = true;
            },
          ),
        ),
      ),
    );

    final saveButton = find.byIcon(Icons.save);

    //Act
    await tester.tap(saveButton);
    await tester.pumpAndSettle();

    final saveDialog = find.byType(AlertDialog);
    expect(saveDialog, findsOneWidget);

    final saveOkButton = find.text('Ja');
    await tester.tap(saveOkButton);
    await tester.pumpAndSettle();

    //Assert
    expect(savePressed, true, reason: 'Save confirmed but not saved');
  });

  testWidgets('Header widget sidbar toggle should work',
      (WidgetTester tester) async {
    bool sidebarToggle = false;

    //Arrange
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: Header(
            onSidebarToggle: () {
              sidebarToggle = true;
            },
            onSave: () {},
          ),
        ),
      ),
    );

    final sidebarToggleButton = find.byIcon(Icons.menu_open);

    //Act
    await tester.tap(sidebarToggleButton);
    await tester.pumpAndSettle();

    //Assert
    expect(sidebarToggle, true);
  });
}
