import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:taafee_mobile/common/extensions/widget_extension.dart';
import 'package:taafee_mobile/common/widgets/toast.dart';
import 'package:taafee_mobile/features/auth/business_logic_layer/auth_controller.dart';
import 'package:taafee_mobile/features/card/business_logic_layer/card_controller.dart';
import 'package:taafee_mobile/features/card/data_layer/model/card_model.dart';
import 'package:taafee_mobile/features/favorite/business_logic_layer/favorite_controller.dart';

import '../../../../common/widgets/button.dart';
import '../../../../common/widgets/text.dart';
import '../../../../core/routing/routing_manager.dart';

// ignore: must_be_immutable
class FavoriteWidget extends StatelessWidget {
  final CardController cardController = Get.find<CardController>();
  final FavoriteController favoriteController = Get.find<FavoriteController>();
  final CardModel cardModel;
  final bool isFavoriteCardWidget;
  final bool isFromDetailsScreen;
  AuthController authController = Get.find<AuthController>();
  FavoriteWidget(
      {super.key,
      this.isFavoriteCardWidget = false,
      this.isFromDetailsScreen = false,
      required this.cardModel});

  @override
  Widget build(BuildContext context) {
    if (authController.isGuest.value) {
      return SvgPicture.asset(
        'assets/icons/heart.svg',
        colorFilter: const ColorFilter.mode(Colors.white, BlendMode.srcIn),
      ).onTap(() {
        Get.defaultDialog(
            title: '',
            content: Column(
              children: [
                BoldTextWidget('you_have_to_sign_in'.tr),
                const SizedBox(
                  height: 20,
                ),
                ButtonWidget(
                    onTap: () {
                      RoutingManager.off(RouteName.login);
                    },
                    title: 'sign_in'.tr)
              ],
            ));
      });
    } else {
      // if (isFromDetailsScreen || isFavoriteCardWidget) {
      //   return ValueBuilder<bool>(
      //     initialValue: favoriteController.isCardFav(cardModel.id),
      //     builder: (bool? value, toggle) {
      //       if (value!) {
      //         return SvgPicture.asset(
      //           'assets/icons/heart_fill.svg',
      //         ).onTap(() async {
      //           toggle(!value);

      //           favoriteController.toggleFavoriteCard(cardModel.id);

      //           cardController.toggleFavorite(cardModel.id, true,
      //               onSuccess: (val) {});
      //         });
      //       }
      //       return SvgPicture.asset(
      //         'assets/icons/heart.svg',
      //         colorFilter:
      //             const ColorFilter.mode(Colors.white, BlendMode.srcIn),
      //       ).onTap(() async {
      //         toggle(!value);

      //         favoriteController.toggleFavoriteCard(
      //           cardModel.id,
      //           cardModel: cardModel,
      //           userId: cardModel.user.id,
      //         );

      //         cardController.toggleFavorite(cardModel.id, false,
      //             onSuccess: (val) {
      //           // if (isFromDetailsScreen) {
      //           //   favoriteController.getFavorites();
      //           // }
      //         });

      //         // cardModel.isFav = !cardModel.isFav;
      //         // if (isFavoriteCardWidget || isFromDetailsScreen) {
      //         //   cardController.cardState.result.clear();
      //         //   cardController.getCards();
      //         // }
      //       });
      //     },
      //   );
      // } else {
      return Obx(() {
        if (favoriteController.isCardFav(cardModel.id)) {
          return SvgPicture.asset(
            'assets/icons/heart_fill.svg',
          ).onTap(() async {
            favoriteController.toggleFavoriteCard(cardModel.id);
            cardController.toggleFavorite(cardModel.id, true, onSuccess: (val) {
              // favoriteController.getFavorites();
            }, onError: (err) {
              if (err.toString() == 'You Have no Internet Connection') {
                Toast.showToast('no_internert_connection'.tr);
              } else {
                Toast.showToast('unknown_error'.tr);
              }
            });
          });
        } else {
          return SvgPicture.asset(
            'assets/icons/heart.svg',
            colorFilter: const ColorFilter.mode(Colors.white, BlendMode.srcIn),
          ).onTap(() async {
            favoriteController.toggleFavoriteCard(
              cardModel.id,
              cardModel: cardModel,
              userId: cardModel.user.id,
            );
            cardController.toggleFavorite(cardModel.id, false,
                onSuccess: (val) {
              // favoriteController.getFavorites();
            }, onError: (err) {
              if (err.toString() == 'You Have no Internet Connection') {
                Toast.showToast('no_internert_connection'.tr);
              } else {
                Toast.showToast('unknown_error'.tr);
              }
            });
          });
        }
      });

      //   //   // cardModel.isFav = !cardModel.isFav;
      //   //   // if (isFavoriteCardWidget || isFromDetailsScreen) {
      //   //   //   cardController.cardState.result.clear();
      //   //   //   cardController.getCards();
      //   //   // }
      // }
    }
  }
}
