// ignore_for_file: use_build_context_synchronously

import 'package:another_flushbar/flushbar.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:hoshan/core/routes/route_generator.dart';
import 'package:hoshan/data/snackbar_messages_model.dart';
import 'package:hoshan/ui/theme/colors.dart';
import 'package:hoshan/ui/theme/responsive.dart';
import 'package:hoshan/ui/theme/text.dart';

List<SnackbarMessagesModel> snackBarMessages = [];

enum SnackBarStatus { info, success, error }

class SnackBarManager {
  BuildContext context;
  String? id;
  SnackBarManager(this.context, {this.id});

  show(
      {required final String message,
      final SnackBarStatus? status,
      Color? backgroundColor,
      Color? borderColor,
      final Widget? btns,
      final bool isTop = true}) {
    if (snackBarMessages.any(
      (element) => element.id == id,
    )) {
      return;
    }

    switch (status) {
      case SnackBarStatus.success:
        borderColor = AppColors.green[50];
        backgroundColor = AppColors.green.defaultShade;
        break;
      case SnackBarStatus.error:
        borderColor = AppColors.red[50];
        backgroundColor = AppColors.red.defaultShade;
        break;
      case SnackBarStatus.info:
        borderColor = AppColors.primaryColor[50];
        backgroundColor = AppColors.primaryColor.defaultShade;
        break;
      default:
        borderColor = borderColor ?? Theme.of(context).colorScheme.onSurface;
        backgroundColor =
            backgroundColor ?? Theme.of(context).colorScheme.surface;
    }
    final flush = Flushbar(
      textDirection: TextDirection.rtl,
      backgroundColor: backgroundColor,
      borderColor: borderColor,
      messageText: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          if (btns != null) btns,
          Expanded(
            child: Text(
              message,
              style: AppTextStyles.body4.copyWith(
                  color: status == null
                      ? Theme.of(context).colorScheme.onSurface
                      : Colors.white),
              textDirection: TextDirection.rtl,
              maxLines: 3,
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ],
      ),
      flushbarPosition: isTop ? FlushbarPosition.TOP : FlushbarPosition.BOTTOM,
      flushbarStyle: FlushbarStyle.FLOATING,
      dismissDirection: FlushbarDismissDirection.HORIZONTAL,
      isDismissible: true,
      animationDuration: const Duration(milliseconds: 300),
      margin: const EdgeInsets.all(16),
      borderRadius: BorderRadius.circular(16),
      maxWidth: Responsive(context).isDesktop()
          ? 800
          : Responsive(context).isTablet()
              ? 400
              : null,
      duration: const Duration(seconds: 3),
      onStatusChanged: (status) {
        if (status == FlushbarStatus.DISMISSED) {
          snackBarMessages.removeWhere(
            (element) => element.id == id,
          );
        }
      },
    )..show(context);
    if (id != null) {
      snackBarMessages.add(SnackbarMessagesModel(id: id!, flushbar: flush));
    }
  }

  showCompleteProfileAlert({bool isTop = true}) {
    show(
        isTop: isTop,
        status: SnackBarStatus.info,
        message: 'ابتدا باید پروفایل خود را تکمیل کنید',
        btns: GestureDetector(
          onTap: () {
            if (id != null) {
              dismiss(id!);
            }
            context.go(Routes.editProfile);
          },
          child: Text(
            'ویرایش پروفایل',
            style: AppTextStyles.body6.copyWith(
                color: AppColors.red.defaultShade, fontWeight: FontWeight.bold),
          ),
        ));
  }

  static dismiss(String id) {
    final message = snackBarMessages.firstWhere(
      (element) => element.id == id,
    );
    message.flushbar.dismiss();
    snackBarMessages.removeWhere(
      (element) => element.id == id,
    );
  }

  static dismissAll() {
    for (final message in snackBarMessages) {
      message.flushbar.dismiss();
    }
    snackBarMessages.clear();
  }
}
