import 'package:another_flushbar/flushbar.dart';
import 'package:flutter/material.dart';

import '../../../common/theme/theme.dart';
import '../../../domain/auth/auth.dart';
import '../../../domain/order/order.dart';
import '../../../domain/printer/printer.dart';
import '../../../domain/table/table.dart';

class AppFlushbar {
  static void showSuccess(BuildContext context, String message) {
    Flushbar(
      messageText: Text(
        message,
        style: AppStyle.md.copyWith(
          color: Colors.white,
          fontWeight: FontWeight.w700,
        ),
      ),
      icon: const Icon(Icons.check_circle, color: Colors.white),
      duration: const Duration(seconds: 2),
      flushbarPosition: FlushbarPosition.BOTTOM,
      backgroundColor: AppColor.success,
      borderRadius: BorderRadius.circular(12),
      margin: const EdgeInsets.all(12),
    ).show(context);
  }

  static void showError(BuildContext context, String message) {
    Flushbar(
      messageText: Text(
        message,
        style: AppStyle.md.copyWith(
          color: Colors.white,
          fontWeight: FontWeight.w700,
        ),
      ),
      icon: const Icon(Icons.error, color: Colors.white),
      duration: const Duration(seconds: 3),
      flushbarPosition: FlushbarPosition.BOTTOM,
      backgroundColor: AppColor.error,
      borderRadius: BorderRadius.circular(12),
      margin: const EdgeInsets.all(12),
    ).show(context);
  }

  static void showAuthFailureToast(BuildContext context, AuthFailure failure) =>
      showError(
        context,
        failure.map(
          serverError: (value) => value.failure.toStringFormatted(context),
          dynamicErrorMessage: (value) => value.erroMessage,
          unexpectedError: (value) => 'Terjadi kesalahan, silahkan coba lagi',
        ),
      );

  static void showTableFailureToast(
    BuildContext context,
    TableFailure failure,
  ) => showError(
    context,
    failure.map(
      serverError: (value) => value.failure.toStringFormatted(context),
      dynamicErrorMessage: (value) => value.erroMessage,
      unexpectedError: (value) => 'Terjadi kesalahan, silahkan coba lagi',
      empty: (value) => 'Tidak ada data',
      localStorageError: (value) => 'Terjadi kesalahan, silahkan coba lagi',
    ),
  );

  static void showOrderFailureToast(
    BuildContext context,
    OrderFailure failure,
  ) => showError(
    context,
    failure.map(
      serverError: (value) => value.failure.toStringFormatted(context),
      dynamicErrorMessage: (value) => value.erroMessage,
      unexpectedError: (value) => 'Terjadi kesalahan, silahkan coba lagi',
      empty: (value) => 'Tidak ada data',
      localStorageError: (value) => 'Terjadi kesalahan, silahkan coba lagi',
    ),
  );

  static void showPrinterFailureToast(
    BuildContext context,
    PrinterFailure failure,
  ) => showError(
    context,
    failure.map(
      serverError: (value) => value.failure.toStringFormatted(context),
      dynamicErrorMessage: (value) => value.erroMessage,
      unexpectedError: (value) => 'Terjadi kesalahan, silahkan coba lagi',
      empty: (value) => 'Tidak ada data',
      localStorageError: (value) => 'Terjadi kesalahan, silahkan coba lagi',
    ),
  );
}
