import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_user_repository_impl.dart';
import 'package:yumi/ui_kit/components/dialog/dialog_base.dart';
import 'package:yumi/ui_kit/components/sc_tts.dart';
import 'package:yumi/shared/tools/sc_loading_manager.dart';
import 'package:yumi/shared/tools/sc_message_utils.dart';

class SCAccountHelper {
  static void optBlacklist(String userId,bool isBlacklist,BuildContext context,Function(bool succ) callback) {
    SmartDialog.dismiss(
      tag:
      "showUserInfoOptMenu",
    );
    SmartDialog.show(
      tag: "showConfirmDialog",
      alignment:
      Alignment.center,
      debounce: true,
      animationType:
      SmartAnimationType
          .fade,
      builder: (_) {
        return MsgDialog(
          title:
          SCAppLocalizations.of(
            context!,
          )!.tips,
          msg:
          isBlacklist
              ? SCAppLocalizations.of(
            context,
          )!.areYouSureToCancelBlacklist
              : SCAppLocalizations.of(
            context,
          )!.areYouSureYouWantToBlockThisUser,
          btnText:
          SCAppLocalizations.of(
            context,
          )!.confirm,
          onEnsure: () {
            SCLoadingManager.show();
            if (isBlacklist) {
              SCAccountRepository()
                  .deleteUserBlacklist(
                userId,
              )
                  .then((
                  result,
                  ) {
                SCTts.show(
                  SCAppLocalizations.of(
                    context,
                  )!.successfullyRemovedFromTheBlacklist,
                );
                SCLoadingManager.hide();
                isBlacklist =
                !isBlacklist;
                callback.call(isBlacklist);
              })
                  .catchError((
                  e,
                  ) {
                SCLoadingManager.hide();
              });
            } else {
              SCAccountRepository()
                  .addUserBlacklist(
                userId,
              )
                  .then((
                  result,
                  ) {
                SCTts.show(
                  SCAppLocalizations.of(
                    context,
                  )!.successfullyAddedToTheBlacklist,
                );
                SCMessageUtils.clearAllMessages(
                  userId,
                );
                SCLoadingManager.hide();
                isBlacklist =
                !isBlacklist;
                callback.call(isBlacklist);
              })
                  .catchError((
                  e,
                  ) {
                SCLoadingManager.hide();
              });
            }
          },
        );
      },
    );

  }
}
