import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fotodocumentation/dto/picture_dto.dart';
import 'package:fotodocumentation/pages/customer/picture_delete_dialog.dart';
import 'package:fotodocumentation/pages/customer/picture_widget.dart';
import 'package:fotodocumentation/utils/global_router.dart';
import 'package:fotodocumentation/utils/login_credentials.dart';
import 'package:mockito/mockito.dart';

import 'package:fotodocumentation/controller/customer_controller.dart';
import 'package:fotodocumentation/controller/picture_controller.dart';
import 'package:fotodocumentation/dto/customer_dto.dart';
import 'package:fotodocumentation/utils/di_container.dart';

import '../testing/test_utils.dart';
import '../testing/test_utils.mocks.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();
  DiContainer.instance.initState();
  DiContainer.instance.put(LoginCredentials, getDefaultLoginCredentials());
  group('Customer Test', () {
    testWidgets('Customer screen', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      var controller = MockCustomerController();
      var pictureController = MockPictureController();
      DiContainer.instance.put(PictureController, pictureController);
      DiContainer.instance.put(CustomerController, controller);

      when(controller.get(id: 1)).thenAnswer((_) async => _dto);
      when(controller.getAll("", "")).thenAnswer((_) async => _list);

      await pumpAppConfig(tester, "${GlobalRouter.pathHome}${GlobalRouter.pathCustomer}/1");
      verify(controller.get(id: 1)).called(1);

      // Click on the first row (InkWell) to open the picture popup
      await tester.tap(find.byKey(Key("table_row_1")).first);
      await tester.pumpAndSettle();

      // Verify that the popup is shown by checking for the PictureWidget
      expect(find.byType(PictureWidget), findsOneWidget); 
    });

    testWidgets('Customer delete yes', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      var controller = MockCustomerController();
      var pictureController = MockPictureController();
      DiContainer.instance.put(PictureController, pictureController);
      DiContainer.instance.put(CustomerController, controller);

      when(controller.get(id: 1)).thenAnswer((_) async => _dto);
      when(controller.getAll("", "")).thenAnswer((_) async => _list);
      when(pictureController.delete(argThat(isA<PictureDto>()))).thenAnswer((_) async => true);

      await pumpAppConfig(tester, "${GlobalRouter.pathHome}${GlobalRouter.pathCustomer}/1");
      verify(controller.get(id: 1)).called(1);

      // Click on the first row (InkWell) to open the picture popup
      await tester.tap(find.byKey(Key("table_row_delete_1")).first);
      await tester.pumpAndSettle();

      // Verify that the popup is shown by checking for the PictureWidget
      expect(find.byType(PictureDeleteDialog), findsOneWidget);

      // Click the yes button to confirm delete
      await tester.tap(find.byKey(Key("picture_delete_yes")));
      await tester.pumpAndSettle();

      // Verify that the delete method was called on the picture controller
      verify(pictureController.delete(argThat(isA<PictureDto>()))).called(1); 
    });

    testWidgets('Customer delete no', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      var controller = MockCustomerController();
      var pictureController = MockPictureController();
      DiContainer.instance.put(PictureController, pictureController);
      DiContainer.instance.put(CustomerController, controller);

      when(controller.get(id: 1)).thenAnswer((_) async => _dto);
      when(controller.getAll("", "")).thenAnswer((_) async => _list);

      await pumpAppConfig(tester, "${GlobalRouter.pathHome}${GlobalRouter.pathCustomer}/1");
      verify(controller.get(id: 1)).called(1);

      // Click on the first row (InkWell) to open the picture popup
      await tester.tap(find.byKey(Key("table_row_delete_1")).first);
      await tester.pumpAndSettle();

      // Verify that the popup is shown by checking for the PictureWidget
      expect(find.byType(PictureDeleteDialog), findsOneWidget);

      // Click the yes button to confirm delete
      await tester.tap(find.byKey(Key("picture_delete_no")));
      await tester.pumpAndSettle();

      // Verify that the delete method was called on the picture controller
      verifyNever(pictureController.delete(argThat(isA<PictureDto>()))); 
    });
  });
}

CustomerDto _dto = CustomerDto(id: 1, customerNumber: "CODE1", name: "Customer 1", pictures: [
  PictureDto(id: 1, comment: "Some comment", category: "category", pictureDate: DateTime.now(), username: "username", image:"[encoded data, 40916 characters]", evaluation: 1),
  ]);

List<CustomerListDto> _list = [CustomerListDto(id: 1, customerNumber: "CODE1", name: "Customer 1"), CustomerListDto(id: 2, customerNumber: "CODE2", name: "Customer 2")];