import 'dart:io';

import 'package:baseproject/core/constants/constant_string.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:animations/animations.dart';

void showSuccessMessage(String message, {Color? backgroundColor, Color? textColor, double? time}) {
  Fluttertoast.showToast(
      msg: message,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.SNACKBAR,
      timeInSecForIosWeb: 2,
      backgroundColor: backgroundColor ?? Colors.green,
      textColor: textColor ?? Colors.white,
      fontSize: 16.0);
}

void showMessage(String message, {Color? backgroundColor, Color? textColor, double? time}) {
  Fluttertoast.showToast(
      msg: message,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.SNACKBAR,
      timeInSecForIosWeb: 2,
      backgroundColor: backgroundColor ?? const Color(0xff161C2C),
      textColor: textColor ?? Colors.white,
      fontSize: 16.0);
}

void showErrorMessage(String message, {Color? backgroundColor, Color? textColor, double? time}) {
  Fluttertoast.showToast(
      msg: message,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.SNACKBAR,
      timeInSecForIosWeb: 2,
      backgroundColor: backgroundColor ?? const Color(0xffC11C3A),
      textColor: textColor ?? Colors.white,
      fontSize: 16.0);
}

Future<void> showConfirmDialog(
  BuildContext context,
  String content, {
  String? title,
  String? cancelText,
  String? submitText,
  Function? onCancel,
  Function? onSubmit,
}) {
  return showModal(
      context: context,
      configuration: const FadeScaleTransitionConfiguration(transitionDuration: Duration(milliseconds: 400)),
      builder: (_) {
        if (Platform.isIOS) {
          return _alertConfirm(
            context,
            content,
            title: title,
            cancelText: cancelText,
            submitText: submitText,
            onCancel: onCancel,
            onSubmit: onSubmit,
          );
        }
        return AlertDialog(
          title: Text(title ?? ConstantString.appName),
          elevation: 8,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
          content: Text(content),
          actions: <Widget>[
            TextButton(
                child: Text(
                  cancelText ?? 'Huỷ',
                  style: const TextStyle(color: Colors.red),
                ),
                onPressed: () {
                  Navigator.of(context).pop();
                  if (onCancel != null) {
                    onCancel();
                  }
                }),
            TextButton(
              child: Text(submitText ?? 'Đồng ý'),
              onPressed: () {
                Navigator.of(context).pop();
                if (onSubmit != null) {
                  onSubmit();
                }
              },
            )
          ],
        );
      });
}

Future<bool?> showAlertDialog(
  BuildContext context, {
  Function? onOk,
  Function? onCancel,
  String? cancelText,
  String? okText,
  String? alertTitle,
  String? content,
}) {
  // set up the buttons
  final Widget cancelButton = TextButton(
    child: Text(cancelText ?? "Huỷ"),
    onPressed: () {
      Navigator.of(context).pop();
    },
  );
  final Widget continueButton = TextButton(
    child: Text(okText ?? "Tiếp tục"),
    onPressed: () {
      if (onOk != null) onOk();
      Navigator.of(context).pop();
    },
  );

  // set up the AlertDialog
  final AlertDialog alert = AlertDialog(
    title: Text(alertTitle ?? "Xác nhận"),
    content: Text(content ?? "Bạn chắc chắn muốn xóa?"),
    actions: <Widget>[
      cancelButton,
      continueButton,
    ],
  );
  //
  // // show the dialog
  // showDialog<bool>(
  //   context: context,
  //   builder: (BuildContext context) {
  //     return alert;
  //   },
  // );
  return showModal(
      context: context,
      configuration: const FadeScaleTransitionConfiguration(transitionDuration: Duration(milliseconds: 500)),
      builder: (_) {
        if (Platform.isIOS) {
          return _alertConfirm(
            context,
            content,
            title: alertTitle,
            cancelText: cancelText,
            submitText: okText,
            onCancel: onCancel,
            onSubmit: onOk,
          );
        }
        return alert;
      });
}

CupertinoAlertDialog _alertConfirm(
  BuildContext context,
  String? content, {
  String? title,
  String? cancelText,
  String? submitText,
  Function? onCancel,
  Function? onSubmit,
}) {
  return CupertinoAlertDialog(
    title: Text(title ?? ""),
    content: Text(content ?? ''),
    actions: <Widget>[
      CupertinoDialogAction(
          child: Text(cancelText ?? "Huỷ"),
          onPressed: () {
            Navigator.of(context).pop();
            if (onCancel != null) {
              onCancel();
            }
          }),
      CupertinoDialogAction(
        child: Text(submitText ?? "Đồng ý"),
        onPressed: () {
          Navigator.of(context).pop();
          if (onSubmit != null) {
            onSubmit();
          }
        },
      )
    ],
  );
}
