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

import '../../../../application/order/order_loader/order_loader_bloc.dart';
import '../../../../common/theme/theme.dart';
import '../../../components/card/order_card.dart';
import '../../../components/error/order_loader_error_state_widget.dart';
import '../../../components/loader/loader_with_text.dart';
import 'order_title.dart';

class OrderLeftPanel extends StatefulWidget {
  final String status;
  final OrderLoaderState state;
  const OrderLeftPanel({super.key, required this.state, required this.status});

  @override
  State<OrderLeftPanel> createState() => _OrderLeftPanelState();
}

class _OrderLeftPanelState extends State<OrderLeftPanel> {
  ScrollController scrollController = ScrollController();
  @override
  Widget build(BuildContext context) {
    return NotificationListener<ScrollNotification>(
      onNotification: (notification) {
        if (notification is ScrollEndNotification &&
            scrollController.position.extentAfter == 0) {
          context.read<OrderLoaderBloc>().add(
            OrderLoaderEvent.fetched(status: widget.status),
          );
          return true;
        }

        return true;
      },
      child: Column(
        children: [
          OrderTitle(
            totalOrder: widget.state.totalOrder,
            startDate: widget.state.startDate,
            endDate: widget.state.endDate,
            title: widget.status == 'pending'
                ? "Pending Pesanan"
                : "Daftar Pesanan",
            onChanged: (value) {
              Future.delayed(const Duration(milliseconds: 800), () {
                context.read<OrderLoaderBloc>().add(
                  OrderLoaderEvent.searchChange(value),
                );
              });
            },
            onDateRangeChanged: (start, end) {
              context.read<OrderLoaderBloc>().add(
                OrderLoaderEvent.dateTimeRangeChange(start!, end!),
              );
            },
          ),
          Expanded(
            child: widget.state.failureOption.fold(
              () {
                if (widget.state.isFetching) {
                  return const Center(child: LoaderWithText());
                }

                if (widget.state.orders.isEmpty) {
                  return Center(
                    child: Text(
                      "Belum ada transaksi saat ini. ",
                      style: AppStyle.lg.copyWith(fontWeight: FontWeight.bold),
                    ),
                  );
                }

                return ListView.builder(
                  itemCount: widget.state.orders.length,
                  controller: scrollController,
                  itemBuilder: (context, index) => GestureDetector(
                    onTap: () {
                      context.read<OrderLoaderBloc>().add(
                        OrderLoaderEvent.setSelectedOrder(
                          widget.state.orders[index],
                        ),
                      );
                    },
                    child: OrderCard(
                      order: widget.state.orders[index],
                      isActive:
                          widget.state.orders[index] ==
                          widget.state.selectedOrder,
                    ),
                  ),
                );
              },
              (f) => OrderLoaderErrorStateWidget(
                failure: f,
                status: widget.status,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
