import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';
import 'package:taafee_mobile/common/const/const.dart';
import 'package:taafee_mobile/common/extensions/widget_extension.dart';
import 'package:taafee_mobile/common/widgets/gridview.dart';
import 'package:taafee_mobile/common/widgets/rx_viewer.dart';
import 'package:taafee_mobile/common/widgets/text.dart';
import 'package:taafee_mobile/common/widgets/toast.dart';
import 'package:taafee_mobile/core/routing/routing_manager.dart';
import 'package:taafee_mobile/features/card/business_logic_layer/card_controller.dart';
import 'package:taafee_mobile/features/favorite/business_logic_layer/favorite_controller.dart';
import 'package:taafee_mobile/features/favorite/presentation_layer/widgets/favorite_card.dart';
import 'package:taafee_mobile/features/home/business_logic_layer/home_controller.dart';

class FavoriteScreen extends StatelessWidget {
  final CardController cardController = Get.find<CardController>();
  final FavoriteController favoriteController = Get.find<FavoriteController>();
  final HomeController homeController = Get.find<HomeController>();
  FavoriteScreen({super.key});

  @override
  Widget build(BuildContext context) {
    if (favoriteController.getFavoriteState.result.isEmpty) {
      favoriteController.getFavorites(onConnectionError: (e) {
        Toast.showToast('no_internert_connection'.tr);
      });
    }
    return Scaffold(
      backgroundColor: AppColors.backGroundColor,
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            BoldTextWidget(
              "favorite".tr,
              fontSize: 16,
              color: AppColors.textColor,
            ).paddingOnly(top: 30, bottom: 20).paddingSymmetric(horizontal: 20),
            RxViewer(
              errorHeight: Get.width,
              errorWidth: Get.height * 0.75,
              width: Get.width,
              height: Get.height * 0.75,
              rxFuture: favoriteController.getFavoriteState,
              child: () => (favoriteController
                      .getFavoriteState.result.isNotEmpty)
                  ? GridViewWidget(
                      mainAxisExtent: 150,
                      count: Responsive.isTablet() ? 3 : 2,
                      itemCount:
                          favoriteController.getFavoriteState.result.length,
                      child: (index) => Obx(() {
                        return FavoriteCardWidget(
                          favoriteModel:
                              favoriteController.getFavoriteState.result[index],
                        ).onTap(() {
                          homeController.setCardAppointment(favoriteController
                              .getFavoriteState.result[index].cardModel.id);
                          RoutingManager.to(RouteName.cardDetails,
                              arguments: favoriteController
                                  .getFavoriteState.result[index].cardModel);
                        });
                      }),
                    )
                  : SizedBox(
                      height: Get.height * 0.75,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          SizedBox(
                            width: 128,
                            height: 128,
                            child: Lottie.asset(
                              'assets/animations/NO Favorite.json',
                              repeat: false,
                            ),
                          ),
                          RegularTextWidget(
                            'add_some_favorite_cards !'.tr,
                            fontSize: Responsive.isTablet() ? 18 : 16,
                          ),
                        ],
                      ).center(),
                    ),
            )
            // const ListViewWidget(
            //   itemCount: 8,
            //   child: CardWidget(),
            // ).paddingSymmetric(vertical: 10)
            ,
            // if (favoriteController.getFavoriteState.result.length < 10)
            //   SizedBox(
            //     height: Get.height * 0.6,
            //   ),
          ],
        ),
      ),
      //),
    ).makeSafeArea();
  }
}
