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/src/rust/api.dart' as rust;
import 'package:chanora_flutter/widgets/client_info_sheet.dart';

void main() {
  rust.BridgeClientProfile profile() {
    return rust.BridgeClientProfile(
      id: BigInt.from(101),
      channel: BigInt.from(7),
      name: 'Bob',
      uniqueId: 'client-unique-id',
      databaseId: BigInt.from(55),
      countryCode: 'US',
      description: 'Operator',
      version: '3.6.2',
      platform: 'Windows',
      onlineSeconds: 3661,
      idleMilliseconds: 42000,
      pingMilliseconds: 38,
      clientAddress: '203.0.113.24',
      serverGroups: const ['Admin', 'Talk Power'],
      channelGroup: 'Guest',
      avatarPath: '/avatar_aabbcc',
      bytesDownloadedMonth: BigInt.from(2048),
      bytesUploadedMonth: BigInt.from(4096),
      bytesDownloadedTotal: BigInt.from(1048576),
      bytesUploadedTotal: BigInt.from(2097152),
      packetLossClientToServerTotal: 0.0123,
      packetLossServerToClientTotal: 0.0456,
    );
  }

  testWidgets('renders loaded profile data in a modal sheet', (tester) async {
    await tester.pumpWidget(
      MaterialApp(
        localizationsDelegates: AppL10n.localizationsDelegates,
        supportedLocales: AppL10n.supportedLocales,
        home: Builder(
          builder: (context) => Scaffold(
            body: Center(
              child: FilledButton(
                onPressed: () => showModalBottomSheet<void>(
                  context: context,
                  isScrollControlled: true,
                  useSafeArea: true,
                  builder: (context) => FractionallySizedBox(
                    heightFactor: 0.86,
                    child: ClientInfoSheet(
                      clientName: 'Bob',
                      loadProfile: () async => profile(),
                    ),
                  ),
                ),
                child: const Text('Open'),
              ),
            ),
          ),
        ),
      ),
    );

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

    expect(tester.takeException(), isNull);
    expect(find.text('Bob'), findsOneWidget);
    expect(find.text('Identity'), findsOneWidget);
    expect(find.text('Membership'), findsOneWidget);
    expect(find.text('Admin, Talk Power'), findsOneWidget);

    await tester.drag(find.byType(ListView), const Offset(0, -360));
    await tester.pumpAndSettle();

    expect(find.text('Connection'), findsOneWidget);
    expect(find.text('1h 1m 1s'), findsOneWidget);
    expect(find.text('42.00 s'), findsOneWidget);
    expect(find.text('203.0.113.24'), findsOneWidget);
  });

  testWidgets('loading and error states use localized copy', (tester) async {
    await tester.pumpWidget(
      MaterialApp(
        locale: const Locale('zh'),
        localizationsDelegates: AppL10n.localizationsDelegates,
        supportedLocales: AppL10n.supportedLocales,
        home: Scaffold(
          body: SizedBox(
            height: 500,
            child: ClientInfoSheet(
              clientName: 'Bob',
              loadProfile: () => Future<rust.BridgeClientProfile>.error(
                StateError('not available'),
              ),
            ),
          ),
        ),
      ),
    );

    expect(find.text('正在加载资料…'), findsOneWidget);
    expect(find.text('Loading profile...'), findsNothing);

    await tester.pumpAndSettle();

    expect(find.text('资料不可用'), findsOneWidget);
    expect(find.text('重试'), findsOneWidget);
    expect(find.text('Profile unavailable'), findsNothing);
  });
}
