import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../application/payment_method/payment_method_loader/payment_method_loader_bloc.dart';
import '../../../domain/payment_method/payment_method.dart';
import '../card/error_card.dart';

class PaymentMethodErrorStateWidget extends StatelessWidget {
  final PaymentMethodFailure failure;
  const PaymentMethodErrorStateWidget({super.key, required this.failure});

  @override
  Widget build(BuildContext context) {
    return failure.maybeMap(
      orElse: () => ErrorCard(
        title: 'Metode Pembayaran',
        message: 'Terjadi Kesalahan saat memuat metode pembayaran',
        onTap: () {
          context.read<PaymentMethodLoaderBloc>().add(
            const PaymentMethodLoaderEvent.fetched(isRefresh: true),
          );
        },
      ),
      dynamicErrorMessage: (value) => ErrorCard(
        title: 'Metode Pembayaran',
        message: value.erroMessage,
        onTap: () {
          context.read<PaymentMethodLoaderBloc>().add(
            const PaymentMethodLoaderEvent.fetched(isRefresh: true),
          );
        },
      ),
      empty: (value) => ErrorCard(
        title: 'Metode Pembayaran',
        message:
            'Metode pembayaran masih kosong, silahkan tambahkan metode pembayaran',
        onTap: () {
          context.read<PaymentMethodLoaderBloc>().add(
            const PaymentMethodLoaderEvent.fetched(isRefresh: true),
          );
        },
      ),
      serverError: (value) => ErrorCard(
        title: 'Metode Pembayaran',
        message: 'Terjadi Kesalahan saat memuat metode pembayaran',
        onTap: () {
          context.read<PaymentMethodLoaderBloc>().add(
            const PaymentMethodLoaderEvent.fetched(isRefresh: true),
          );
        },
      ),
      localStorageError: (value) => ErrorCard(
        title: 'Metode Pembayaran',
        message: value.erroMessage,
        onTap: () {
          context.read<PaymentMethodLoaderBloc>().add(
            const PaymentMethodLoaderEvent.fetched(isRefresh: true),
          );
        },
      ),
      unexpectedError: (value) => ErrorCard(
        title: 'Metode Pembayaran',
        message: 'Terjadi Kesalahan saat memuat metode pembayaran',
        onTap: () {
          context.read<PaymentMethodLoaderBloc>().add(
            const PaymentMethodLoaderEvent.fetched(isRefresh: true),
          );
        },
      ),
    );
  }
}
