import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:minstrel/shared/delayed_loading.dart';

void main() {
  testWidgets('renders nothing for the first 100ms of loading', (tester) async {
    await tester.pumpWidget(const MaterialApp(
      home: DelayedLoading(
        isLoading: true,
        delay: Duration(milliseconds: 200),
        whileDelayed: Text('LOADING'),
        whenReady: Text('READY'),
      ),
    ));
    await tester.pump(const Duration(milliseconds: 100));
    expect(find.text('LOADING'), findsNothing);
    expect(find.text('READY'), findsNothing);
  });

  testWidgets('renders whileDelayed after delay elapses', (tester) async {
    await tester.pumpWidget(const MaterialApp(
      home: DelayedLoading(
        isLoading: true,
        delay: Duration(milliseconds: 200),
        whileDelayed: Text('LOADING'),
        whenReady: Text('READY'),
      ),
    ));
    await tester.pump(const Duration(milliseconds: 250));
    expect(find.text('LOADING'), findsOneWidget);
  });

  testWidgets('renders whenReady when not loading', (tester) async {
    await tester.pumpWidget(const MaterialApp(
      home: DelayedLoading(
        isLoading: false,
        whileDelayed: Text('LOADING'),
        whenReady: Text('READY'),
      ),
    ));
    expect(find.text('READY'), findsOneWidget);
  });

  testWidgets('resets timer when isLoading flips false then true', (tester) async {
    await tester.pumpWidget(const MaterialApp(
      home: DelayedLoading(
        isLoading: true,
        delay: Duration(milliseconds: 200),
        whileDelayed: Text('LOADING'),
        whenReady: Text('READY'),
      ),
    ));
    await tester.pump(const Duration(milliseconds: 150));
    // Switch to not-loading, then back to loading.
    await tester.pumpWidget(const MaterialApp(
      home: DelayedLoading(
        isLoading: false,
        whileDelayed: Text('LOADING'),
        whenReady: Text('READY'),
      ),
    ));
    await tester.pumpWidget(const MaterialApp(
      home: DelayedLoading(
        isLoading: true,
        delay: Duration(milliseconds: 200),
        whileDelayed: Text('LOADING'),
        whenReady: Text('READY'),
      ),
    ));
    // Original 150ms shouldn't carry over — at 100ms past restart
    // we should still see nothing.
    await tester.pump(const Duration(milliseconds: 100));
    expect(find.text('LOADING'), findsNothing);
  });
}
