import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nomo/config/theme/theme_extensions/theme_extension.dart';

import '../constants/assets.dart';

class IXSnackBar {
  static String? _lastMessage;
  static DateTime? _lastShowTime;

  static bool _shouldShowSnackbar(String message) {
    final now = DateTime.now();
    if (message == _lastMessage &&
        _lastShowTime != null &&
        now.difference(_lastShowTime!).inSeconds < 3) {
      return false;
    }
    _lastMessage = message;
    _lastShowTime = now;
    return true;
  }

  static showIXSnackBar({
    required String title,
    required String message,
    Duration? duration,
  }) {
    if (!_shouldShowSnackbar(message)) return;
    // Get.snackbar(
    //   title,
    //   message,
    //   duration: duration ?? const Duration(seconds: 3),
    //   margin: const EdgeInsets.only(top: 10, left: 10, right: 10),
    //   colorText: Palette.white,
    //   backgroundColor: Palette.snackbarBgColor,
    //   borderColor: Palette.snackbarBorderColor,
    //   borderWidth: 1,
    //   borderRadius: 16,
    //   icon: Image.asset(
    //     Assets.pwdSuccess,
    //     width: 20,
    //     height: 20,
    //   ),
    // );
    Get.rawSnackbar(
      // message: message,
      backgroundColor: Get.reactiveTheme.cardColor, // 先让背景透明
      snackStyle: SnackStyle.floating, // 悬浮样式
      duration: duration ?? const Duration(seconds: 3),
      margin: const EdgeInsets.only(top: 10, left: 10, right: 10),
      snackPosition: SnackPosition.top,
      // backgroundColor: Palette.snackbarBgColor,
      barBlur: 7.0,
      // borderColor: ,
      borderWidth: 1,
      borderRadius: 16,
      icon: Image.asset(Assets.success, width: 20, height: 20),
      messageText: Text(
        message,
        style: TextStyle(color: Get.reactiveTheme.textTheme.bodyLarge!.color),
      ),
    );
  }

  static showIXErrorSnackBar({
    required String title,
    required String message,
    Color? color,
    Duration? duration,
  }) {
    if (!_shouldShowSnackbar(message)) return;
    // Get.snackbar(
    //   title,
    //   message,
    //   duration: duration ?? const Duration(seconds: 3),
    //   margin: const EdgeInsets.only(top: 10, left: 10, right: 10),
    //   colorText: Palette.white,
    //   backgroundColor: color ?? Palette.snackbarBgColor,
    //   borderColor: Palette.snackbarBorderColor,
    //   borderWidth: 1.w,
    //   borderRadius: 16.r,
    //   icon: Image.asset(
    //     Assets.pwdError,
    //     width: 20.w,
    //     height: 20.w,
    //   ),
    // );

    Get.rawSnackbar(
      // message: message,
      backgroundColor: Get.reactiveTheme.cardColor, // 先让背景透明
      snackStyle: SnackStyle.floating, // 悬浮样式
      duration: duration ?? const Duration(seconds: 3),
      margin: const EdgeInsets.only(top: 10, left: 10, right: 10),
      snackPosition: SnackPosition.top,
      // backgroundColor: Palette.snackbarBgColor,
      barBlur: 7.0,
      // borderColor: Palette.snackbarBorderColor,
      borderWidth: 1,
      borderRadius: 16,
      icon: Image.asset(Assets.error, width: 20, height: 20),
      messageText: Text(
        message,
        style: TextStyle(color: Get.reactiveTheme.textTheme.bodyLarge!.color),
      ),
    );
  }

  static showIXToast({
    String? title,
    required String message,
    Color? color,
    Duration? duration,
  }) {
    if (!_shouldShowSnackbar(message)) return;
    Get.rawSnackbar(
      title: title,
      duration: duration ?? const Duration(seconds: 3),
      snackStyle: SnackStyle.grounded,
      backgroundColor: color ?? Colors.green,
      onTap: (snack) {
        Get.closeAllSnackbars();
      },
      //overlayBlur: 0.8,
      message: message,
    );
  }

  static showIXErrorToast({
    String? title,
    required String message,
    Color? color,
    Duration? duration,
  }) {
    if (!_shouldShowSnackbar(message)) return;
    Get.rawSnackbar(
      title: title,
      duration: duration ?? const Duration(seconds: 3),
      snackStyle: SnackStyle.grounded,
      backgroundColor: color ?? Colors.redAccent,
      onTap: (snack) {
        Get.closeAllSnackbars();
      },
      //overlayBlur: 0.8,
      message: message,
    );
  }
}
