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

class PeerWidget extends StatelessWidget {
  const PeerWidget({
    super.key,
    required this.device,
    required this.isIosBrowser,
    required this.onConnect,
    required this.communicationChannelState,
  });

  final NearbyDevice device;
  final bool isIosBrowser;
  final ValueChanged<NearbyDevice> onConnect;
  final CommunicationChannelState communicationChannelState;

  @override
  Widget build(BuildContext context) {
    final connectedStateCaption = communicationChannelState.isLoading
        ? 'Wait for connection'
        : 'Tap to chat';

    final status =
        device.status.isConnected ? connectedStateCaption : device.status.name;

    final canTap = !communicationChannelState.isLoading;

    return ListTile(
      title: Text(
        '${isIosBrowser ? 'Found device' : 'Pending invitation'} | ${device.info.displayName} | $status',
      ),
      onTap: canTap ? () => onConnect(device) : null,
      tileColor: Colors.blueAccent,
      textColor: Colors.white,
    );
  }
}
