// ignore_for_file: constant_identifier_names

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

///弹出框位置
enum PromptBoxSnackbarPosition {
  //上部弹出
  Top,

  //底部弹出
  Bottom,
}

///弹出框
class PromptBox {
  PromptBox._();

  static void init() {
    EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 2000)
      ..indicatorType = EasyLoadingIndicatorType.ring
      ..loadingStyle = EasyLoadingStyle.custom
      ..indicatorSize = 35.0
      ..lineWidth = 2
      ..radius = 10.0
      ..progressColor = Colors.white
      ..backgroundColor = Colors.black.withOpacity(0.7)
      ..indicatorColor = Colors.white
      ..textColor = Colors.white
      ..fontSize = 16
      ..maskColor = Colors.black.withOpacity(0.6)
      ..successWidget = const Icon(Icons.done, color: Colors.green, size: 35)
      ..errorWidget = const Icon(Icons.close, color: Colors.red, size: 35)
      ..userInteractions = true
      ..dismissOnTap = false;
  }

  static bool _enable = true;
  static void $SetEnable(bool val) {
    _enable = val;
  }

  /// 等待提示
  ///
  /// [text] 自定义提示文案
  static void loading([String? text]) {
    if (!_enable) return;

    _setLoadingStyle();
    EasyLoading.show(
      // indicator: LoadingWidget(text: text ?? '${i18nBook.common.loading.t}...'),
      indicator: const SizedBox(
        width: 80,
        height: 80,
        child: Center(child: CircularProgressIndicator()),
      ),
      maskType: EasyLoadingMaskType.custom,
      status: "",
    );
  }

  /// 轻提示
  ///
  /// [text] 提示内容
  static void toast(String text) {
    if (!_enable) return;

    _resetStyle();
    EasyLoading.showToast(text);
  }

  /// 成功提示
  ///
  /// [text] 自定义提示文案
  static void success(String text) {
    if (!_enable) return;

    _resetStyle();
    EasyLoading.showSuccess(text);
  }

  /// 错误提示
  ///
  /// [text] 自定义提示文案
  static void error(String text) {
    if (!_enable) return;

    _resetStyle();
    EasyLoading.showError(text);
  }

  /// 停止提示
  static void dismiss() {
    EasyLoading.instance.userInteractions = true;
    EasyLoading.dismiss();
  }

  /// 提示框(带标题)
  ///
  /// [message] 提示内容
  ///
  /// [title] 标题
  ///
  /// [position] 弹出位置
  static void snackbar(
    String message, {
    String? title,
    PromptBoxSnackbarPosition position = PromptBoxSnackbarPosition.Top,
  }) {
    final isTop = PromptBoxSnackbarPosition.Top == position;

    Get.snackbar(
      title ?? "提示",
      message,
      maxWidth: 400,
      snackPosition: isTop ? SnackPosition.TOP : SnackPosition.BOTTOM,
      margin: isTop
          ? const EdgeInsets.only(top: 36)
          : const EdgeInsets.only(bottom: 48),
    );
  }

  static void _setLoadingStyle() {
    EasyLoading.instance
      ..userInteractions = false
      ..backgroundColor = Colors.transparent
      ..maskColor = Colors.black.withOpacity(0.08)
      ..boxShadow = <BoxShadow>[];
  }

  static void _resetStyle() {
    EasyLoading.instance
      ..backgroundColor = Colors.black.withOpacity(0.7)
      ..maskColor = Colors.black.withOpacity(0.6)
      ..boxShadow = null;
  }
}
