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

import 'package:chanora_flutter/l10n/generated/app_localizations.dart';
import 'package:chanora_flutter/main.dart';
import 'package:chanora_flutter/widgets/connect_widgets.dart';
import 'package:chanora_flutter/widgets/input_dialogs.dart';
import 'package:chanora_flutter/widgets/ptt_capability_badge.dart';

void main() {
  Widget localizedHarness(Widget child, {Locale? locale}) {
    return MaterialApp(
      locale: locale,
      localizationsDelegates: AppL10n.localizationsDelegates,
      supportedLocales: AppL10n.supportedLocales,
      home: Scaffold(body: child),
    );
  }

  testWidgets('connect actions stack on narrow mobile widths', (tester) async {
    await tester.pumpWidget(
      localizedHarness(
        SizedBox(
          width: 320,
          child: ConnectForm(
            hostCtl: TextEditingController(text: 'server.example.com'),
            nickCtl: TextEditingController(text: 'MobileUser'),
            passwordCtl: TextEditingController(),
            onConnect: () {},
            onAddBookmark: () {},
          ),
        ),
      ),
    );

    await tester.pumpAndSettle();

    final connectTop = tester.getTopLeft(find.text('Connect')).dy;
    final bookmarkTop = tester.getTopLeft(find.text('Save bookmark')).dy;

    expect(bookmarkTop, greaterThan(connectTop + 44));
  });

  testWidgets('connect actions stack at standard phone content widths', (
    tester,
  ) async {
    await tester.pumpWidget(
      localizedHarness(
        SizedBox(
          width: 361,
          child: ConnectForm(
            hostCtl: TextEditingController(text: 'server.example.com'),
            nickCtl: TextEditingController(text: 'MobileUser'),
            passwordCtl: TextEditingController(),
            onConnect: () {},
            onAddBookmark: () {},
          ),
        ),
      ),
    );

    await tester.pumpAndSettle();

    final connectTop = tester.getTopLeft(find.text('Connect')).dy;
    final bookmarkTop = tester.getTopLeft(find.text('Save bookmark')).dy;

    expect(bookmarkTop, greaterThan(connectTop + 44));
  });

  testWidgets('disconnected mobile chrome keeps content near the safe area', (
    tester,
  ) async {
    const primaryContentKey = Key('primary-content');

    await tester.binding.setSurfaceSize(const Size(393, 852));
    addTearDown(() => tester.binding.setSurfaceSize(null));

    await tester.pumpWidget(
      localizedHarness(
        ChanoraMobileScaffold(
          compactIdleChrome: true,
          canDisconnect: false,
          title: null,
          actions: [
            IconButton(
              tooltip: 'About',
              icon: const Icon(Icons.info_outline),
              onPressed: () {},
            ),
          ],
          onDisconnect: () {},
          compactHeader: const SizedBox(height: 44, child: Text('Chanora')),
          body: const KeyedSubtree(
            key: primaryContentKey,
            child: Text('Primary content'),
          ),
        ),
      ),
    );

    await tester.pumpAndSettle();

    expect(find.byType(AppBar), findsNothing);
    expect(tester.getTopLeft(find.byKey(primaryContentKey)).dy, lessThan(120));
  });

  testWidgets('PTT explanation sheet is scrollable for mobile text scaling', (
    tester,
  ) async {
    await tester.pumpWidget(
      localizedHarness(
        MediaQuery(
          data: const MediaQueryData(textScaler: TextScaler.linear(1.8)),
          child: const SizedBox(
            width: 320,
            child: PttCapabilityBadge(
              level: 'L0Focused',
              backendId: 'focused',
              boundInputClass: 'keyboard',
            ),
          ),
        ),
      ),
    );

    await tester.tap(find.byIcon(Icons.info_outline));
    await tester.pumpAndSettle();

    expect(find.byType(SingleChildScrollView), findsWidgets);
    expect(tester.takeException(), isNull);
  });

  testWidgets('PTT capture dialog wraps content for small screens', (
    tester,
  ) async {
    await tester.pumpWidget(
      localizedHarness(
        Builder(
          builder: (context) {
            return Center(
              child: FilledButton(
                onPressed: () => showDialog<void>(
                  context: context,
                  builder: (_) => const PttBindingCaptureDialog(),
                ),
                child: const Text('Open'),
              ),
            );
          },
        ),
      ),
    );

    await tester.tap(find.text('Open'));
    await tester.pumpAndSettle();

    expect(find.byType(SingleChildScrollView), findsOneWidget);
    expect(tester.takeException(), isNull);
  });
}
