import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../blocs/emergency_contact_bloc.dart';

class EmergencyContactSaveButton extends StatelessWidget {
  final EmergencyContactState state;

  const EmergencyContactSaveButton({super.key, required this.state});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(UiConstants.space4),
      decoration: BoxDecoration(
        color: UiColors.bgPopup,
        border: Border(top: BorderSide(color: UiColors.border)),
      ),
      child: SafeArea(
        child: SizedBox(
          width: double.infinity,
          child: ElevatedButton(
            onPressed: state.isValid
                ? () => context
                    .read<EmergencyContactBloc>()
                    .add(EmergencyContactsSaved())
                : null,
            style: ElevatedButton.styleFrom(
              backgroundColor: UiColors.primary,
              foregroundColor: UiColors.primaryForeground,
              disabledBackgroundColor: UiColors.textPlaceholder,
              padding: EdgeInsets.symmetric(vertical: UiConstants.space4),
              shape: RoundedRectangleBorder(
                borderRadius: UiConstants.radiusFull,
              ),
              elevation: 0,
            ),
            child: state.status == EmergencyContactStatus.saving
                ? SizedBox(
                    height: 20.0,
                    width: 20.0,
                    child: CircularProgressIndicator(
                      strokeWidth: 2,
                      valueColor:
                          AlwaysStoppedAnimation<Color>(UiColors.primaryForeground),
                    ),
                  )
                : Text(
                    'Save & Continue',
                    style: UiTypography.title2b,
                  ),
          ),
        ),
      ),
    );
  }
}
