import 'package:flutter/material.dart';
import 'package:aves/remote/remote_sync_bus.dart';

class RemoteProgressBanner extends StatelessWidget {
  const RemoteProgressBanner({super.key});

  @override
  Widget build(BuildContext context) {
    final bus = RemoteSyncBus.instance;

    return ValueListenableBuilder<RemoteSyncProgress?>(
      valueListenable: bus.progressNotifier,
      builder: (context, prog, _) {
        // Mostra SOLO quando è bootstrap (showOverlay=true)
        if (prog == null || !prog.showOverlay) return const SizedBox.shrink();

        final total = prog.total;
        final done = prog.done;
        final value = total > 0 ? done / total : null;

        final label = 'Agg remoti $done/$total';

        return SafeArea(
          child: Align(
            alignment: Alignment.topCenter,
            child: Container(
              margin: const EdgeInsets.all(12),
              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
              width: 380,
              decoration: BoxDecoration(
                color: Colors.black87,
                borderRadius: BorderRadius.circular(12),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(label, style: const TextStyle(color: Colors.white)),
                  const SizedBox(height: 6),
                  ClipRRect(
                    borderRadius: BorderRadius.circular(6),
                    child: LinearProgressIndicator(
                      value: value, // determinata se total>0, altrimenti indeterminata
                      minHeight: 4,
                      backgroundColor: Colors.white24,
                      color: Colors.lightBlueAccent,
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
