import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class AppToast {
  static void showToast({
    required String message,
    Toast? toastLength,
    int? timeInSeconds,
    double? fontSize,
    ToastGravity? toastGravity,
    Color? backgroundColor,
    Color? textColor,
    int? radius,
    int? elevation,
    int? imageSize,
  }) {
    Fluttertoast.showToast(
        msg: message, toastLength: toastLength, gravity: toastGravity, timeInSecForIosWeb: timeInSeconds!, backgroundColor: backgroundColor, textColor: textColor, fontSize: fontSize);
  }

  static void showSuccessToast({
    required String message,
    Toast toastLength = Toast.LENGTH_LONG,
    int timeInSeconds = 2,
    double fontSize = 16,
    ToastGravity toastGravity = ToastGravity.TOP,
    Color textColor = Colors.white,
    int? radius,
    int? elevation,
    int imageSize = 32,
  }) {
    Fluttertoast.showToast(
        msg: message, toastLength: toastLength, gravity: toastGravity, timeInSecForIosWeb: timeInSeconds ?? 0, backgroundColor: Colors.green, textColor: textColor, fontSize: fontSize);
  }

  static void showErrorToast(
      {required String message,
      Toast toastLength = Toast.LENGTH_LONG,
      int timeInSeconds = 2,
      double fontSize = 16,
      ToastGravity toastGravity = ToastGravity.TOP,
      Color textColor = Colors.white,
      int radius = 15,
      int? elevation,
      int imageSize = 32,
      BuildContext? localContext}) {
    FToast fToast = FToast();
    fToast.init(localContext != null ? localContext : AppGlobal.context);
    Widget toast = Container(
      padding: EdgeInsets.all(8),
      alignment: Alignment.topCenter,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(radius.toDouble()),
        color: Colors.red,
      ),
      child: Wrap(
        children: [
          Text(
            message,
            style: TextStyle(color: textColor, fontSize: fontSize),
            softWrap: true,
            overflow: TextOverflow.clip,
          ),
        ],
      ),
    );
    fToast.showToast(
        child: toast,
        gravity: ToastGravity.TOP,
        toastDuration: Duration(seconds: timeInSeconds),
        positionedToastBuilder: (context, child, gravity) {
          return Positioned(top: 50, left: 10, right: 10, child: child);
        });
  }

  void cancelToast() {
    Fluttertoast.cancel();
  }

  void backWithEmpty() {
    AppToast.showErrorToast(message: TranslationBase.of(AppGlobal.context).empty);
    Navigator.of(AppGlobal.context).pop();
  }

  void back() {
    Navigator.of(AppGlobal.context).pop();
  }
}
