import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:taafee_mobile/common/const/const.dart';
import 'package:taafee_mobile/common/extensions/widget_extension.dart';
import 'package:taafee_mobile/common/widgets/rx_viewer.dart';
import 'package:taafee_mobile/common/widgets/toast.dart';
import 'package:taafee_mobile/features/card/business_logic_layer/card_controller.dart';
import 'package:taafee_mobile/common/widgets/responsive_view.dart';
import 'package:taafee_mobile/features/home/business_logic_layer/home_controller.dart';

import '../../../../common/widgets/header_screen.dart';
import '../../../../common/widgets/text.dart';
import '../../../../core/routing/routing_manager.dart';
import '../../../card/presentation_layer/widgets/my_card.dart';

class MyCardsScreen extends StatelessWidget {
  final CardController cardController = Get.find<CardController>();
  final HomeController homeController = HomeController();
  MyCardsScreen({super.key});
  final ScrollController scrollController = ScrollController();
  void moreDate() {
    scrollController.addListener(() {
      if (scrollController.position.atEdge && scrollController.offset != 0) {
        cardController.getMyCards(homeController.user.value!.id,
            onConnectionError: (e) {
          Toast.showToast('no_internert_connection'.tr);
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    homeController.readUser();
    cardController.getMyCards(homeController.user.value!.id,
        onConnectionError: (e) {
      Toast.showToast('no_internert_connection'.tr);
    });
    return Scaffold(
      backgroundColor: AppColors.backGroundColor,
      body: SingleChildScrollView(
        controller: scrollController,
        child: Column(
          children: [
            HeaderScreen("my_cards".tr)
                .paddingSymmetric(horizontal: 20)
                .paddingOnly(
                  top: 20,
                ),
            Obx(
              () => Row(
                mainAxisAlignment: (homeController.isArabic.value)
                    ? MainAxisAlignment.start
                    : MainAxisAlignment.end,
                children: [
                  Container(
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(30),
                        color: AppColors.primeColor),
                    height: 38,
                    width: 95,
                    child: RegularTextWidget(
                      "new_card".tr,
                      fontSize: 14,
                      color: Colors.white,
                    ),
                  ).onTap(() {
                    RoutingManager.to(RouteName.addCard);
                  })
                ],
              ).paddingSymmetric(
                horizontal: Responsive.isTablet() ? 40 : 20,
              ),
            ),
            RxViewer(
              width: Get.width,
              height: Get.height * 0.8,
              errorHeight: Get.width,
              errorWidth: Get.height * 0.75,
              rxFuture: cardController.myCardState,
              child: () => (cardController.myCardState.result.data.isEmpty)
                  ? SizedBox(
                      width: Get.width,
                      height: Get.height * 0.7,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Image.asset(
                            'assets/images/Add-New-Card.png',
                            width: 128,
                            height: 128,
                          ),
                          RegularTextWidget(
                            'press_(new_card)_to_add_new_card!'.tr,
                            fontSize: 18,
                            textAlign: TextAlign.center,
                          ).center(),
                        ],
                      ),
                    )
                  : Obx(() {
                      return ResponsiveView(
                          mainAxisExtent: 185,
                          itemCount:
                              cardController.myCardState.result.data.length,
                          childBuilder: (index) {
                            return Obx(() {
                              return MyCardWidget(
                                cardController.myCardState.result.data[index],
                                isShowEditIcon: true,
                              );
                            });
                          });
                    }),
            )
          ],
        ),
      ).makeSafeArea(),
    );
  }
}
