// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

//import 'package:aitrainer_app/bloc/login_form_bloc.dart';
import 'package:aitrainer_app/bloc/login/login_bloc.dart';
import 'package:aitrainer_app/helper/database.dart';
import 'package:aitrainer_app/library_keys.dart';
import 'package:aitrainer_app/util/app_localization.dart';
import 'package:aitrainer_app/model/user.dart';
import 'package:aitrainer_app/repository/user_repository.dart';
import 'package:aitrainer_app/util/common.dart';
import 'package:aitrainer_app/view/login.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter/material.dart';

import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';

class MockUserRepository extends Mock implements UserRepository {
  final User user = User();

  setEmail(String email) {
    this.user.email = email;
  }

  setPassword(String password) {
    this.user.password = password;
  }

  Future<void> getUser() async {
    if (this.user.email == "sw@andio.biz" && this.user.password == "PalKataPeter1") {
      //OK
    } else {
      throw new Exception("Customer does not exist");
    }
  }
}

class MockCommon with Common {
  String getEmailError() {
    return emailError;
  }
}

void main() {
  group('LoginScreen', () {
    late Widget loginWidget;
    late MockCommon common;

    setUp(() async {
      await DB().initDb();
      common = MockCommon();

      loginWidget = MaterialApp(home: LoginPage(), localizationsDelegates: [
        AppLocalizationsDelegate(isTest: true),
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ], routes: {
        'home': (context) => LoginPage(),
      });
    });

    tearDown(() {
      //loginBloc.close();
    });

    testWidgets('Display login page', (WidgetTester tester) async {
      // Build our app and trigger a frame.
      await tester.pumpWidget(loginWidget);

      await tester.pumpAndSettle();

      expect(find.byKey(LibraryKeys.loginEmailField), findsOneWidget);
      expect(find.byKey(LibraryKeys.loginPasswordField), findsOneWidget);
      expect(find.byKey(LibraryKeys.loginOKButton), findsOneWidget);
    });

    testWidgets('Add zero length email', (WidgetTester tester) async {
      await tester.pumpWidget(loginWidget);
      await tester.pumpAndSettle();

      var emailField = find.byKey(LibraryKeys.loginEmailField);
      var pwdField = find.byKey(LibraryKeys.loginPasswordField);
      var okButton = find.byKey(LibraryKeys.loginOKButton);
      expect(emailField, findsOneWidget);
      expect(pwdField, findsOneWidget);
      expect(okButton, findsOneWidget);

      await tester.tap(emailField);
      await tester.enterText(emailField, "sw");
      await tester.enterText(pwdField, "123");

      await tester.pumpAndSettle();
      await tester.tap(okButton);
      await tester.pump(const Duration(milliseconds: 500)); // add delay
      final emailErrorFinder = find.text(common.getEmailError());
      expect(emailErrorFinder, findsOneWidget);
    });
  });
}
