import 'package:flutter/material.dart';

abstract interface class SnackbarUtils {
  void showSnackbar(BuildContext context, String msg, bool warning);
  void showSnackbarPopup(BuildContext context, String msg, bool warning);
}

class SnackbarUtilsImpl implements SnackbarUtils {
  @override
  void showSnackbar(BuildContext context, String msg, bool warning) {
    var snackBar = SnackBar(
      content: _contentFor(context, msg, warning),
      backgroundColor: Colors.white,
      behavior: SnackBarBehavior.floating,
      showCloseIcon: true,
      closeIconColor: Theme.of(context).colorScheme.inversePrimary,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(6),
        side: BorderSide(width: 2.0, style: BorderStyle.solid, color: Theme.of(context).colorScheme.inversePrimary),
      ),
      margin: EdgeInsets.only(bottom: MediaQuery.of(context).size.height - 130, left: MediaQuery.of(context).size.width - 400, right: 10),
    );

    ScaffoldMessenger.of(context).showSnackBar(snackBar);
  }

  @override
  void showSnackbarPopup(BuildContext context, String msg, bool warning) {
    var snackBar = SnackBar(
      content: _contentFor(context, msg, warning),
      backgroundColor: Colors.white,
      behavior: SnackBarBehavior.floating,
      showCloseIcon: true,
      closeIconColor: Theme.of(context).colorScheme.inversePrimary,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(6),
        side: BorderSide(width: 2.0, style: BorderStyle.solid, color: Theme.of(context).colorScheme.inversePrimary),
      ),
      width: 350,
      //margin: EdgeInsets.only(bottom: MediaQuery.of(context).size.height - 100, left: MediaQuery.of(context).size.width - 350, right: 10),
    );

    ScaffoldMessenger.of(context).showSnackBar(snackBar);
  }

  Widget _contentFor(BuildContext context, String msg, bool warning) {
    var icon = _iconFor(context, warning);

    var style = _textStyleFor(context, warning);
    return Wrap(
      alignment: WrapAlignment.start,
      crossAxisAlignment: WrapCrossAlignment.center,
      children: [
        icon,
        Text(
          msg,
          style: style,
        ),
      ],
    );
  }

  Icon _iconFor(BuildContext context, bool warning) {
    var color = _contentColor(context, warning);
    return warning ? Icon(Icons.error, color: color) : Icon(Icons.check_circle_outline, color: color);
  }

  TextStyle _textStyleFor(BuildContext context, bool warning) {
    var color = _contentColor(context, warning);
    var bodyLarge = Theme.of(context).primaryTextTheme.bodyLarge!;
    var style = TextStyle(
        color: color,
        decoration: bodyLarge.decoration,
        fontFamily: bodyLarge.fontFamily,
        fontSize: bodyLarge.fontSize,
        fontWeight: bodyLarge.fontWeight,
        letterSpacing: bodyLarge.letterSpacing,
        textBaseline: bodyLarge.textBaseline);
    return style;
  }

  Color _contentColor(BuildContext context, bool warning) {
    return warning ? Theme.of(context).colorScheme.error : Theme.of(context).colorScheme.inversePrimary;
  }
}
