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

import '../../../bloc/message_wrapper/message_bloc.dart';
import '../../../bloc/message_wrapper/message_state.dart';

class MessageWrapperWidget extends StatefulWidget {
  final Widget child;

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

  @override
  State<StatefulWidget> createState() => MessageWrapperWidgetState();

  static MessageWrapperWidgetState of(BuildContext context) {
    return context.findAncestorStateOfType<MessageWrapperWidgetState>()!;
  }
}

class MessageWrapperWidgetState extends State<MessageWrapperWidget> {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<MessageBloc, MessageState>(
      builder: (context, state) {
        if (state is MessageShowState) {
          return Stack(
            children: [
              widget.child,
              Positioned(
                top: 50,
                left: 0,
                right: 0,
                child: Padding(
                  padding: const EdgeInsets.all(10),
                  child: Material(
                    color: Colors.transparent,
                    child: Container(
                      margin: EdgeInsets.all(8),
                      padding: EdgeInsets.all(16),
                      decoration: BoxDecoration(
                        color: Theme.of(
                          context,
                        ).colorScheme.primaryContainer,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Padding(
                        padding: const EdgeInsets.all(10),
                        child: Row(
                          children: [
                            Padding(
                              padding: const EdgeInsets.only(right: 15),
                              child: Icon(Icons.info, size: 32),
                            ),
                            Expanded(child: Text(state.message)),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          );
        }

        return widget.child;
      },
    );
  }
}
