import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:tane/app.dart';
import 'package:tane/i18n/strings.g.dart';

import '../support/test_support.dart';

void main() {
  Widget app(db) => TranslationProvider(
    child: TaneApp(
      repository: newTestRepository(db),
      species: newTestSpeciesRepository(db),
      onboarding: newTestOnboardingStore(),
    ),
  );

  setUp(() {
    PackageInfo.setMockInitialValues(
      appName: 'Tanemaki',
      packageName: 'org.comunes.tane',
      version: '0.1.0',
      buildNumber: '1',
      buildSignature: '',
    );
  });

  testWidgets('drawer About item opens the About screen', (tester) async {
    LocaleSettings.setLocaleSync(AppLocale.en);
    final db = newTestDatabase();
    addTearDown(db.close);

    await tester.pumpWidget(app(db));
    await tester.pumpAndSettle();

    await tester.tap(find.byIcon(Icons.menu));
    await tester.pumpAndSettle();
    await tester.tap(find.text('About')); // drawer entry
    await tester.pumpAndSettle();

    expect(find.text('種まき'), findsOneWidget);
    expect(find.textContaining('digital Plantare'), findsOneWidget);
    await disposeTree(tester);
  });

  testWidgets('Settings About tile opens the About screen', (tester) async {
    LocaleSettings.setLocaleSync(AppLocale.en);
    final db = newTestDatabase();
    addTearDown(db.close);

    await tester.pumpWidget(app(db));
    await tester.pumpAndSettle();

    await tester.tap(find.byIcon(Icons.menu));
    await tester.pumpAndSettle();
    await tester.tap(find.text('Settings'));
    await tester.pumpAndSettle();

    // The backup section pushed the About tile below the fold.
    await tester.scrollUntilVisible(find.text('About Tanemaki'), 200);
    await tester.tap(find.text('About Tanemaki'));
    await tester.pumpAndSettle();

    // Explanation text and kanji are shown above the fold.
    expect(find.text('種まき'), findsOneWidget);
    expect(find.textContaining('helps people and collectives'), findsOneWidget);
    expect(find.textContaining('digital Plantare'), findsOneWidget);

    // License and version live below the fold; scroll them into view.
    await tester.scrollUntilVisible(find.text('AGPL-3.0'), 200);
    expect(find.text('AGPL-3.0'), findsOneWidget);
    expect(find.text('0.1.0 (1)'), findsOneWidget);
    await disposeTree(tester);
  });

  testWidgets('About screen opens the open-source licenses page', (
    tester,
  ) async {
    LocaleSettings.setLocaleSync(AppLocale.en);
    final db = newTestDatabase();
    addTearDown(db.close);

    await tester.pumpWidget(app(db));
    await tester.pumpAndSettle();

    await tester.tap(find.byIcon(Icons.menu));
    await tester.pumpAndSettle();
    await tester.tap(find.text('Settings'));
    await tester.pumpAndSettle();
    await tester.scrollUntilVisible(find.text('About Tanemaki'), 200);
    await tester.tap(find.text('About Tanemaki'));
    await tester.pumpAndSettle();

    await tester.scrollUntilVisible(find.text('Open-source licenses'), 200);
    await tester.tap(find.text('Open-source licenses'));
    await tester.pumpAndSettle();

    // Flutter's built-in LicensePage renders the app name.
    expect(find.byType(LicensePage), findsOneWidget);
    await disposeTree(tester);
  });
}
