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

import '../../api/marianumconnect/queries/get_breakers/get_breakers_response.dart';
import '../../state/app/modules/breaker/bloc/breaker_bloc.dart';

class Breaker extends StatelessWidget {
  final BreakerArea breaker;
  final Widget child;

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

  @override
  Widget build(BuildContext context) {
    final bloc = context.watch<BreakerBloc>();
    final blocked = bloc.isBlocked(breaker);
    if (blocked != null) {
      return _BreakerBlockedView(message: blocked);
    }
    return child;
  }
}

class _BreakerBlockedView extends StatelessWidget {
  final String message;

  const _BreakerBlockedView({required this.message});

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final scheme = theme.colorScheme;
    final reason = message.trim();

    return Material(
      color: scheme.surface,
      child: CustomScrollView(
        physics: const AlwaysScrollableScrollPhysics(),
        slivers: [
          SliverFillRemaining(
            hasScrollBody: false,
            child: Center(
              child: Padding(
                padding: const EdgeInsets.symmetric(
                  horizontal: 32,
                  vertical: 24,
                ),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Icon(
                      Icons.construction_outlined,
                      size: 60,
                      color: scheme.onSurfaceVariant,
                    ),
                    const SizedBox(height: 24),
                    Text(
                      'Momentan nicht verfügbar',
                      textAlign: TextAlign.center,
                      style: theme.textTheme.titleLarge?.copyWith(
                        fontWeight: FontWeight.w600,
                        color: scheme.onSurface,
                      ),
                    ),
                    if (reason.isNotEmpty) ...[
                      const SizedBox(height: 10),
                      Text(
                        reason,
                        textAlign: TextAlign.center,
                        style: theme.textTheme.bodyMedium?.copyWith(
                          color: scheme.onSurfaceVariant,
                          height: 1.4,
                        ),
                      ),
                    ],
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
