import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/i18n/strings.g.dart';
import 'package:tane/services/ocr/label_text_extractor.dart';
import 'package:tane/ui/draft_triage.dart';

/// A stand-in OCR engine so the dialog can be tested without a native backend.
class _FakeExtractor implements LabelTextExtractor {
  _FakeExtractor({this.result, this.supported = true});
  final String? result;
  final bool supported;
  @override
  bool get isSupported => supported;
  @override
  Future<String?> suggestLabel(Uint8List photo) async => result;
}

Widget _host(Widget child) {
  LocaleSettings.setLocaleSync(AppLocale.en);
  return TranslationProvider(
    child: MaterialApp(
      locale: AppLocale.en.flutterLocale,
      supportedLocales: AppLocaleUtils.supportedLocales,
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      home: Scaffold(body: child),
    ),
  );
}

void main() {
  // A valid 1×1 PNG so Image.memory can decode it in the widget test.
  final photo = Uint8List.fromList([
    0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, //
    0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
    0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
    0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4,
    0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41,
    0x54, 0x78, 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00,
    0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00,
    0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE,
    0x42, 0x60, 0x82,
  ]);

  testWidgets(
    'a supported engine offers a suggestion that pre-fills the name',
    (tester) async {
      await tester.pumpWidget(
        _host(
          NameDraftDialog(
            photo: photo,
            extractor: _FakeExtractor(result: 'Aubergine Black Beauty'),
          ),
        ),
      );
      await tester.pumpAndSettle();

      expect(find.byKey(const Key('draft.suggestFromPhoto')), findsOneWidget);
      await tester.tap(find.byKey(const Key('draft.suggestFromPhoto')));
      await tester.pumpAndSettle();

      expect(find.text('Aubergine Black Beauty'), findsOneWidget);
    },
  );

  testWidgets('an unsupported engine hides the suggestion button', (
    tester,
  ) async {
    await tester.pumpWidget(
      _host(
        NameDraftDialog(
          photo: photo,
          extractor: _FakeExtractor(supported: false),
        ),
      ),
    );
    await tester.pumpAndSettle();
    expect(find.byKey(const Key('draft.suggestFromPhoto')), findsNothing);
  });

  testWidgets('a null suggestion leaves the field untouched (no crash)', (
    tester,
  ) async {
    await tester.pumpWidget(
      _host(NameDraftDialog(photo: photo, extractor: _FakeExtractor())),
    );
    await tester.pumpAndSettle();
    await tester.tap(find.byKey(const Key('draft.suggestFromPhoto')));
    await tester.pumpAndSettle();
    // Nothing was entered; the save still returns the (empty) field.
    expect(find.byKey(const Key('draft.nameField')), findsOneWidget);
  });
}
