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

import '../../../application/order/order_loader/order_loader_bloc.dart';
import '../../../domain/order/order.dart';
import '../card/error_card.dart';

class OrderLoaderErrorStateWidget extends StatelessWidget {
  final OrderFailure failure;
  final String? status;
  final Function()? onRefresh;
  const OrderLoaderErrorStateWidget({
    super.key,
    required this.failure,
    this.status,
    this.onRefresh,
  });

  @override
  Widget build(BuildContext context) {
    return failure.maybeMap(
      orElse: () => ErrorCard(
        title: 'Pesanan',
        message: 'Terjadi kesalahan saat memuat pesanan',
        onTap:
            onRefresh ??
            () {
              context.read<OrderLoaderBloc>().add(
                OrderLoaderEvent.fetched(status: status ?? '', isRefresh: true),
              );
            },
      ),
      dynamicErrorMessage: (value) => ErrorCard(
        title: 'Pesanan',
        message: value.erroMessage,
        onTap:
            onRefresh ??
            () {
              context.read<OrderLoaderBloc>().add(
                OrderLoaderEvent.fetched(status: status ?? '', isRefresh: true),
              );
            },
      ),
      empty: (value) => ErrorCard(
        title: 'Pesanan',
        message: 'Data Pesanan Kosong',
        onTap:
            onRefresh ??
            () {
              context.read<OrderLoaderBloc>().add(
                OrderLoaderEvent.fetched(status: status ?? '', isRefresh: true),
              );
            },
      ),
      serverError: (value) => ErrorCard(
        title: 'Pesanan',
        message: 'Terjadi kesalahan saat memuat pesanan',
        onTap:
            onRefresh ??
            () {
              context.read<OrderLoaderBloc>().add(
                OrderLoaderEvent.fetched(status: status ?? '', isRefresh: true),
              );
            },
      ),
      unexpectedError: (value) => ErrorCard(
        title: 'Pesanan',
        message: 'Terjadi kesalahan saat memuat pesanan',
        onTap:
            onRefresh ??
            () {
              context.read<OrderLoaderBloc>().add(
                OrderLoaderEvent.fetched(status: status ?? '', isRefresh: true),
              );
            },
      ),
    );
  }
}
