import 'package:app_hit_a_wod/src/utils/fontello/my_flutter_app_icons.dart';
import 'package:app_hit_a_wod/src/styles.dart';
import 'package:flutter/material.dart';

class SnackbarProvider {

  BuildContext _context;

  static SnackbarProvider _instancia;
  factory SnackbarProvider(BuildContext context) {
    if(_instancia==null) {
      _instancia = SnackbarProvider._internal(context);
    }
    return _instancia;
  }
  SnackbarProvider._internal(this._context);

  void success(String obj) {
    obj = _truncateWithEllipsis(50, obj);
    SnackBar snackbar = new SnackBar(
      duration: Duration(seconds: 1),
      backgroundColor: colors["six"],
      content: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(MyFlutterApp.ok),
          SizedBox(width: 20),
          Expanded(child: Text(obj))
        ]
      )
    );

    _showSnackbar(snackbar);
  }

  void error(String obj) {
    obj = _truncateWithEllipsis(50, obj);
    SnackBar snackbar = new SnackBar(
      duration: Duration(seconds: 1),
      backgroundColor: colors["seven"],
      content: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(MyFlutterApp.cross),
          SizedBox(width: 20),
          Expanded(child: Text(obj))
        ]
      )
    );

    _showSnackbar(snackbar);
  }

  void warning(String obj) {
    obj = _truncateWithEllipsis(50, obj);
    SnackBar snackbar = new SnackBar(
      duration: Duration(seconds: 1),
      backgroundColor: Colors.orange,
      content: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(MyFlutterApp.warning),
          SizedBox(width: 20),
          Expanded(child: Text(obj))
        ]
      )
    );

    _showSnackbar(snackbar);
  }

  void _showSnackbar(SnackBar snack) {
    Scaffold.of(_context).showSnackBar(snack);
  }

  void setContext(BuildContext context) {
    _context = context;
  }
  
  String _truncateWithEllipsis(int cutoff, String myString) {
  return (myString.length <= cutoff)
    ? myString
    : '${myString.substring(0, cutoff)}...';
}

}