part of '../rooms_panel.dart';

class _InviteItem extends ConsumerStatefulWidget {
  final Room room;
  final PyramidTheme pt;
  const _InviteItem({required this.room, required this.pt});

  @override
  ConsumerState<_InviteItem> createState() => _InviteItemState();
}

class _InviteItemState extends ConsumerState<_InviteItem> {
  bool _accepting = false;
  bool _declining = false;

  Future<void> _accept() async {
    setState(() => _accepting = true);
    try {
      await widget.room.join();
      if (mounted) {
        ref.read(activeRoomIdProvider.notifier).state = widget.room.id;
        ref.read(viewModeProvider.notifier).state = ViewMode.chat;
      }
    } catch (_) {
      if (mounted) setState(() => _accepting = false);
    }
  }

  Future<void> _decline() async {
    setState(() => _declining = true);
    try {
      await widget.room.leave();
    } catch (_) {
      if (mounted) setState(() => _declining = false);
    }
  }

  @override
  Widget build(BuildContext context) {
    final pt = widget.pt;
    final name = widget.room.getLocalizedDisplayname();
    final isDm = widget.room.isDirectChat;
    return Container(
      margin: const EdgeInsets.symmetric(vertical: 1),
      padding: const EdgeInsets.fromLTRB(10, 8, 10, 8),
      decoration: BoxDecoration(
        color: pt.accentSoft,
        borderRadius: BorderRadius.circular(pt.rSm),
        border: Border.all(color: pt.accent.withAlpha(50)),
      ),
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Row(children: [
          Icon(isDm ? Icons.person_rounded : Icons.tag_rounded,
              size: 12, color: pt.accent),
          const SizedBox(width: 6),
          Expanded(
            child: Text(name,
                style: TextStyle(
                    color: pt.fg,
                    fontSize: 13,
                    fontWeight: FontWeight.w500),
                overflow: TextOverflow.ellipsis),
          ),
        ]),
        const SizedBox(height: 6),
        Row(children: [
          Expanded(
            child: OutlinedButton(
              style: OutlinedButton.styleFrom(
                foregroundColor: pt.danger,
                side: BorderSide(color: pt.danger.withAlpha(80)),
                padding: const EdgeInsets.symmetric(vertical: 4),
                minimumSize: Size.zero,
                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(6)),
              ),
              onPressed: (_declining || _accepting) ? null : _decline,
              child: _declining
                  ? const SizedBox(
                      width: 12,
                      height: 12,
                      child: CircularProgressIndicator.adaptive(strokeWidth: 1.5))
                  : const Text('Ablehnen', style: TextStyle(fontSize: 12)),
            ),
          ),
          const SizedBox(width: 6),
          Expanded(
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                backgroundColor: pt.accent,
                foregroundColor: pt.accentFg,
                elevation: 0,
                padding: const EdgeInsets.symmetric(vertical: 4),
                minimumSize: Size.zero,
                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(6)),
              ),
              onPressed: (_accepting || _declining) ? null : _accept,
              child: _accepting
                  ? const SizedBox(
                      width: 12,
                      height: 12,
                      child: CircularProgressIndicator.adaptive(strokeWidth: 1.5))
                  : const Text('Annehmen', style: TextStyle(fontSize: 12)),
            ),
          ),
        ]),
      ]),
    );
  }
}
