import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_bloc.dart';

import '../bloc/operation_state.dart';

/// Listens to [OperationBloc] and shows:
/// - SnackBars for success and error messages.
/// - A blocking modal barrier with a spinner while a mutation is in flight,
///   so the user gets unambiguous "wait" feedback and cannot double-tap or
///   navigate away mid-request.
class OperationViewEnforcer extends StatelessWidget {
  final Widget child;

  const OperationViewEnforcer({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<OperationBloc, OperationState>(
      listener: (context, state) {
        if (state is OperationFinished && state.message != null) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text(state.message!)),
          );
        }

        if (state is OperationFailed) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text(state.message)),
          );
        }
      },
      builder: (context, state) {
        final isInProgress = state is OperationInProgress;
        final progressMessage =
            isInProgress ? state.message : null;

        return Stack(
          children: [
            child,
            if (isInProgress)
              PopScope(
                canPop: false,
                child: Stack(
                  children: [
                    const ModalBarrier(
                      dismissible: false,
                      color: Colors.black54,
                    ),
                    Center(
                      // Material liefert einen DefaultTextStyle — sonst rendert
                      // der Text hier (über dem Navigator, ohne Scaffold) mit
                      // der gelb-unterstrichenen Fallback-Darstellung.
                      child: Material(
                        type: MaterialType.transparency,
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            const CircularProgressIndicator(),
                            if (progressMessage != null) ...[
                              const SizedBox(height: 16),
                              Text(
                                progressMessage,
                                textAlign: TextAlign.center,
                                style: const TextStyle(
                                  color: Colors.white,
                                  fontSize: 14,
                                  fontWeight: FontWeight.w500,
                                  decoration: TextDecoration.none,
                                ),
                              ),
                            ],
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
          ],
        );
      },
    );
  }
}
