import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:simple_database/main.dart';
import 'package:simple_database/screens/contact_list.dart';
import 'package:simple_database/utils/contact.dart';

List<Contact> cl = [
  Contact(name: "Risa", email: "risa@gmail.com", number: "087654389090", company: "Polinema")
];
void main() {

  void checkNWidget(Finder finder, int n, String reason) {
    expect(finder, findsNWidgets(n), reason: reason);
  }

  void checkOneWidget(Finder finder){
    const String trimStr1 = 'zero widgets with type "';
    const String trimStr2 = '" (ignoring offstage widget)';
    final finderMsg =
    finder.toString().replaceAll(trimStr1, '').replaceAll(trimStr2, '');
    expect(finder, findsOneWidget, reason: "Problem: Widget $finderMsg not found ");
  }

  testWidgets('UI Component-Scaffold Found', (WidgetTester tester) async {
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    Finder findAppBar = find.byKey(const Key('scaffold_contactlist'));
    checkOneWidget(findAppBar);
  });

  testWidgets('UI Component-AppBar Found', (WidgetTester tester) async {
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    Finder findAppBar = find.byKey(const Key('appbar_contactlist'));
    checkOneWidget(findAppBar);
  });

  testWidgets('UI Component-Title AppBar Found', (WidgetTester tester) async {
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    Finder findTitleAppBar = find.byKey(const Key('title_appbar_contactlist'));
    checkOneWidget(findTitleAppBar);
  });

  testWidgets('UI Component-ListView Found', (WidgetTester tester) async {
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    Finder findListViewWidget = find.byKey(const Key('listview_contactlist'));
    checkOneWidget(findListViewWidget);
  });

  testWidgets('UI Component- Padding ListTile found', (tester) async{
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    Finder findListTile = find.byKey(const Key('padding_contactlist'));
    checkOneWidget(findListTile);
  });

  testWidgets('UI Component- Padding email found', (tester) async{
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    Finder findListTile = find.byKey(const Key('padding_email'));
    checkOneWidget(findListTile);
  });
  testWidgets('UI Component- Padding number found', (tester) async{
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    Finder findListTile = find.byKey(const Key('padding_number'));
    checkOneWidget(findListTile);
  });
  testWidgets('UI Component- Padding company found', (tester) async{
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    Finder findListTile = find.byKey(const Key('padding_company'));
    checkOneWidget(findListTile);
  });
  testWidgets('UI Component- Fittedbox  found', (tester) async{
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    Finder findListTile = find.byKey(const Key('fittedbox_contactlist'));
    checkOneWidget(findListTile);
  });

  testWidgets('UI Component- Row  found', (tester) async{
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    Finder findListTile = find.byKey(const Key('row_contactlist'));
    checkOneWidget(findListTile);
  });

  testWidgets('UI Component- Column found', (tester) async{
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    Finder findcolum = find.byKey(const Key('column_contactlist'));
    checkOneWidget(findcolum);
  });

  testWidgets("Contact List when there is data - List Tiles appear with the same number", (WidgetTester tester)async{
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    for (int i = 0; i < cl.length; i++) {
      Finder findListTile = find.byKey(Key('litstile_$i'));
      checkOneWidget(findListTile);
    }
  });

  testWidgets("ContactList has data name", (WidgetTester tester) async {
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    for (int i = 0; i < cl.length; i++) {
      String textName = '${cl[i].name}';
      Finder findTextName = find.text(textName);
      checkOneWidget(findTextName);
    }
  });
  //correctcode
  testWidgets("ContactList has data number phone", (WidgetTester tester)async{
    await tester.pumpWidget(const MaterialApp(home: const ContactList()));
    for (int i = 0; i < cl.length; i++) {
      String textNumber = "Phone: ${cl[i].number}";
      Finder findTextNumber = find.text(textNumber);
      checkOneWidget(findTextNumber);
    }
  });

  testWidgets("ContactList has data company ", (WidgetTester tester)async{
    await tester.pumpWidget(const MaterialApp(home: const ContactList()));
    for (int i = 0; i < cl.length; i++) {
      String textCompany = "Company: ${cl[i].company}";
      Finder findTextCompany = find.text(textCompany);
      checkOneWidget(findTextCompany);
    }
  });

  testWidgets("ContactList has data email", (WidgetTester tester)async{
    await tester.pumpWidget(const MaterialApp(home: const ContactList()));
    for (int i = 0; i < cl.length; i++) {
      String textEmail = "Email: ${cl[i].email}";
      Finder findTextEmail = find.text(textEmail);
      checkOneWidget(findTextEmail);
    }
  });

  testWidgets("Person icon found", (WidgetTester tester)async{
    await tester.pumpWidget(const MaterialApp(home: const ContactList()));
    for (int i = 0; i < cl.length; i++) {
      Finder findIconPerson = find.byKey(Key('icon_person'));
      int count = cl.length;
      checkNWidget(findIconPerson, count, 'Icon person on left data not found');
    }
  });

  testWidgets("Delete icon found", (WidgetTester tester)async{
    await tester.pumpWidget(const MaterialApp(home: const ContactList()));
    for (int i = 0; i < cl.length; i++) {
      Finder findIconDelete = find.byKey(Key('icon_delete'));
      int count = cl.length;
      checkNWidget(findIconDelete, count, 'Icon person on left data not found');
    }
  });

  testWidgets("Edit icon found", (WidgetTester tester)async{
    await tester.pumpWidget(const MaterialApp(home: const ContactList()));
    for (int i = 0; i < cl.length; i++) {
      Finder findIconEdit = find.byKey(Key('icon_edit'));
      int count = cl.length;
      checkNWidget(findIconEdit, count, 'Icon person on left data not found');
    }
  });
  //correct code
  testWidgets("Delete icon found", (WidgetTester tester)async{
    await tester.pumpWidget(const MaterialApp(home: const ContactList()));
    for (int i = 0; i < cl.length; i++) {
      Finder findIconDelete = find.byKey(Key('iconbutton_delete_$i'));
      int count = cl.length;
      checkNWidget(findIconDelete, count, 'Icon Delete not found');
      await tester.tap(findIconDelete);
      await tester.pumpAndSettle();
      Finder findAlertDialog = find.byKey(Key('alertdialog'));
      checkOneWidget(findAlertDialog);
      Finder findsizedbox = find.byKey(Key('sizedbox'));
      checkOneWidget(findsizedbox);
      Finder findtextdelete = find.byKey(const Key('textdeletebutton'));
      checkOneWidget(findtextdelete);
      Finder findtextcancel = find.byKey(const Key('textcancelbutton'));
      checkOneWidget(findtextcancel);
    }
  });

  //correctcode
  testWidgets("Edit icon found", (WidgetTester tester)async{
    await tester.pumpWidget(const MaterialApp(home: const ContactList()));
    for (int i = 0; i < cl.length; i++) {
      Finder findIconEdit = find.byKey(Key('iconbutton_edit_$i'));
      int count = cl.length;
      checkNWidget(findIconEdit, count, 'tidak ditemukan');
      await tester.tap(findIconEdit);
      await tester.pumpAndSettle();
      Finder findTitleAppBar = find.byKey(Key('contact_form_appbar'));
      checkOneWidget(findTitleAppBar);
    }
  });
  testWidgets('UI Component- FloatingButton Found', (WidgetTester tester) async{
    await tester.pumpWidget(const MaterialApp(home: ContactList()));
    Finder findButton = find.byKey(const Key('button_add'));
    Finder findIconAdd = find.byKey( const Key('icon_add'));
    checkOneWidget(findIconAdd);
    checkOneWidget(findButton);
    await tester.tap(findButton);
    await tester.pumpAndSettle();
    Finder findAppBar = find.byType(AppBar);
    checkOneWidget(findAppBar);
    Finder findTitleAppBar = find.byKey(const Key('title_appbar_contactform'));
    checkOneWidget(findTitleAppBar);
  });

  testWidgets('UI Component-Material App Found', (WidgetTester tester) async {
    await tester.pumpWidget(const MyApp());
    Finder findAppBar = find.byKey(const Key('material_app'));
    checkOneWidget(findAppBar);
  });

  testWidgets('UI Component-Home Found', (WidgetTester tester) async {
    await tester.pumpWidget(const MyApp());
    Finder findTitleAppBar = find.byKey(const Key('contact_list'));
    checkOneWidget(findTitleAppBar);
  });
}



