import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';
import 'package:taafee_mobile/common/extensions/widget_extension.dart';
import 'package:taafee_mobile/common/widgets/responsive_view.dart';
import 'package:taafee_mobile/common/widgets/toast.dart';
import 'package:taafee_mobile/features/card/presentation_layer/widgets/card.dart';
import 'package:taafee_mobile/features/category/business_logic_layer/category_controller.dart';
import 'package:taafee_mobile/features/category/data_layer/model/category.dart';

import '../../../../common/const/const.dart';
import '../../../../common/widgets/header_screen.dart';
import '../../../../common/widgets/rx_viewer.dart';
import '../../../../common/widgets/text.dart';

// ignore: must_be_immutable
class CategoryDetailsScreen extends StatelessWidget {
  CategoryDetailsScreen({
    super.key,
  });
  CategoryModel category = Get.arguments;
  CategoryController categoryController = Get.find<CategoryController>();
  final ScrollController scrollController = ScrollController();

  void moreSearchResults() {
    scrollController.addListener(() {
      if (scrollController.position.atEdge && scrollController.offset != 0) {
        categoryController.getCategoryCards(
            categoryId: category.id,
            onConnectionError: (e) {
              Toast.showToast('no_internert_connection'.tr);
            });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    moreSearchResults();
    return Scaffold(
      backgroundColor: AppColors.backGroundColor,
      body: SingleChildScrollView(
        controller: scrollController,
        child: Column(
          children: [
            HeaderScreen(category.name).paddingSymmetric(
                vertical: 25, horizontal: Responsive.isTablet() ? 40 : 20),
            RxViewer(
              withPagination: true,
              withSizedBox: true,
              loaderHeight: 400,
              loaderWidth: 400,
              rxFuture: categoryController.categoryCardsState,
              child: () =>
                  (categoryController.categoryCardsState.result.data.isNotEmpty)
                      ? Column(
                          children: [
                            ResponsiveView(
                              mainAxisExtent: Responsive.isTablet() ? 180 : 180,
                              itemCount: categoryController
                                  .categoryCardsState.result.data.length,
                              childBuilder: (index) => CardWidget(
                                  categoryController
                                      .categoryCardsState.result.data[index]),
                            ),
                          ],
                        )
                      : Column(
                          children: [
                            Lottie.asset(
                              'assets/animations/folder.json',
                              repeat: false,
                            ).paddingAll(40),
                            RegularTextWidget(
                              'no_cards_in_this_category'.tr,
                              color: AppColors.textColor,
                              fontSize: 18.0,
                            )
                          ],
                        ),
            )

            // ListViewWidget(
            //     itemCount: 6,
            //     childBuilder: (index) {
            //       return const CardWidget();
            //     }).paddingOnly(bottom: 20),
          ],
        ),
      ),
    ).makeSafeArea();
  }
}
