import 'package:commons_core/commons_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/db/enums.dart';
import 'package:tane/ui/quantity_picker.dart';

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

/// A countable amount must never be blank: picking cob/pod/… defaults to 1 and
/// the stepper can't drop below 1 — you can't save "a cob" with no figure.
void main() {
  Quantity? emitted;

  Future<void> pumpPicker(WidgetTester tester,
      {LotType type = LotType.seed}) async {
    emitted = null;
    final db = newTestDatabase();
    addTearDown(db.close);
    await tester.pumpWidget(wrapScreen(
      repository: newTestRepository(db),
      child: Scaffold(
        body: QuantityPicker(
          type: type,
          onChanged: (q) => emitted = q,
        ),
      ),
    ));
    await tester.pump();
  }

  testWidgets('selecting a countable unit defaults the count to 1',
      (tester) async {
    await pumpPicker(tester);
    await tester.tap(find.byKey(const Key('quantity.kind.cob')));
    await tester.pump();

    expect(emitted, isNotNull);
    expect(emitted!.kind, QuantityKind.cob);
    expect(emitted!.count, 1);
    expect(
      find.widgetWithText(TextField, '1'),
      findsOneWidget,
      reason: 'the count field shows 1, not an empty box',
    );
  });

  testWidgets('the minus button never takes a countable amount below 1',
      (tester) async {
    await pumpPicker(tester);
    await tester.tap(find.byKey(const Key('quantity.kind.cob')));
    await tester.pump();
    // Two decrements from 1 must clamp at 1, never 0 or blank.
    await tester.tap(find.byIcon(Icons.remove));
    await tester.pump();
    await tester.tap(find.byIcon(Icons.remove));
    await tester.pump();

    expect(emitted!.count, 1);
  });

  testWidgets('a vibe unit (a few) carries no number and shows no stepper',
      (tester) async {
    await pumpPicker(tester);
    await tester.tap(find.byKey(const Key('quantity.kind.aFew')));
    await tester.pump();

    expect(emitted!.kind, QuantityKind.aFew);
    expect(emitted!.count, isNull);
    expect(find.byKey(const Key('quantity.count')), findsNothing);
  });

  testWidgets('typing then clearing the box still stores 1, never empty',
      (tester) async {
    await pumpPicker(tester);
    await tester.tap(find.byKey(const Key('quantity.kind.pod')));
    await tester.pump();
    await tester.enterText(find.byKey(const Key('quantity.count')), '');
    await tester.pump();

    expect(emitted!.kind, QuantityKind.pod);
    expect(emitted!.count, 1);
  });
}
