// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility in the flutter_test package. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:wireless_text_syncer_android/main.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  const connectionChannel = MethodChannel('wireless_text_syncer/connection');
  const stateChannel = MethodChannel('wireless_text_syncer/connection_state');

  setUp(() {
    TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
        .setMockMethodCallHandler(connectionChannel, (call) async {
          switch (call.method) {
            case 'getState':
              return {
                'connected': false,
                'connecting': false,
                'host': '',
                'port': 8181,
                'lastError': null,
              };
            case 'startDiscovery':
              return [
                {'name': 'Desktop-WIN11', 'host': '192.168.1.10', 'port': 8181},
              ];
            case 'connect':
            case 'disconnect':
              return null;
            case 'sendText':
              return true;
          }
          return null;
        });
    TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
        .setMockMethodCallHandler(stateChannel, (call) async => null);
  });

  tearDown(() {
    TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
        .setMockMethodCallHandler(connectionChannel, null);
    TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
        .setMockMethodCallHandler(stateChannel, null);
  });

  testWidgets('shows v2 draft board controls', (WidgetTester tester) async {
    SharedPreferences.setMockInitialValues({});

    await tester.pumpWidget(const WirelessTextSyncerApp());
    await tester.pump(const Duration(milliseconds: 100));
    await tester.pump(const Duration(seconds: 2));

    expect(find.text('未连接 (点击配置)'), findsOneWidget);
    expect(find.text('IP 地址'), findsOneWidget);
    expect(find.text('端口'), findsOneWidget);
    expect(find.text('局域网设备雷达'), findsOneWidget);
    expect(find.text('Desktop-WIN11'), findsOneWidget);
    expect(find.text('可连接'), findsOneWidget);
    expect(find.text('草稿板'), findsOneWidget);
    expect(find.text('发送后清空'), findsOneWidget);
    expect(find.text('追加回车'), findsOneWidget);
    expect(find.text('发送到电脑'), findsOneWidget);
  });
}
