import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:mzansi_innovation_hub/main.dart';
import 'package:mzansi_innovation_hub/mih_apis/mih_mzansi_wallet_apis.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_inputs_and_buttons/mih_dropdown_input.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_inputs_and_buttons/mih_number_input.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_inputs_and_buttons/mih_text_input.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_layout/mih_single_child_scroll.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_package_components/mih_button.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_package_components/mih_package_tool_body.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_package_components/mih_floating_menu.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_package_components/mih_package_window.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_package_components/mih_search_bar.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_pop_up_messages/mih_error_message.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_pop_up_messages/mih_loading_circle.dart';
import 'package:mzansi_innovation_hub/mih_objects/app_user.dart';
import 'package:mzansi_innovation_hub/mih_objects/loyalty_card.dart';
import 'package:mzansi_innovation_hub/mih_packages/mzansi_wallet/builder/build_loyalty_card_list.dart';
import 'package:mzansi_innovation_hub/mih_packages/mzansi_wallet/components/mih_card_display.dart';
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';

class MihCards extends StatefulWidget {
  final AppUser signedInUser;
  const MihCards({
    super.key,
    required this.signedInUser,
  });

  @override
  State<MihCards> createState() => _MihCardsState();
}

class _MihCardsState extends State<MihCards> {
  final TextEditingController shopController = TextEditingController();
  final TextEditingController _nicknameController = TextEditingController();
  final TextEditingController cardNumberController = TextEditingController();
  final TextEditingController cardSearchController = TextEditingController();
  final FocusNode searchFocusNode = FocusNode();
  late Future<List<MIHLoyaltyCard>> cardList;
  List<MIHLoyaltyCard> listOfCards = [];
  //bool showSelectedCardType = false;
  final ValueNotifier<String> shopName = ValueNotifier("");
  final ValueNotifier<List<MIHLoyaltyCard>> searchShopName = ValueNotifier([]);

  final MobileScannerController scannerController = MobileScannerController(
    detectionSpeed: DetectionSpeed.unrestricted,
  );
  final boxFit = BoxFit.contain;

  void searchShop() {
    if (cardSearchController.text.isEmpty) {
      searchShopName.value = listOfCards;
    } else {
      List<MIHLoyaltyCard> temp = [];
      for (var item in listOfCards) {
        if (item.shop_name
            .toLowerCase()
            .contains(cardSearchController.text.toLowerCase())) {
          temp.add(item);
        }
      }
      searchShopName.value = temp;
    }
  }

  void openscanner() async {
    Navigator.of(context).pushNamed(
      '/scanner',
      arguments: cardNumberController,
    );
  }

  void shopSelected() {
    if (shopController.text.isNotEmpty) {
      shopName.value = shopController.text;
    } else {
      shopName.value = "";
    }
  }

  void addCardWindow(BuildContext ctxt) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) => MihPackageWindow(
        fullscreen: false,
        windowTitle: "Add New Loyalty Card",
        onWindowTapClose: () {
          shopController.clear();
          cardNumberController.clear();
          _nicknameController.clear();
          shopName.value = "";
          Navigator.pop(context);
        },
        windowBody: Column(
          children: [
            MIHDropdownField(
              controller: shopController,
              hintText: "Shop Name",
              dropdownOptions: const [
                "+More",
                "Apple Tree",
                "Auchan",
                "Best Before",
                "Big Save",
                "Boxer",
                "BP",
                "Builders Warehouse",
                "Checkers",
                "Choppies",
                "Clicks",
                "Continente",
                "Cotton:On",
                "Carrefour",
                "Dis-Chem",
                "Edgars",
                "Eskom",
                "Exclusive Books",
                "Fresh Stop",
                "Fresmart",
                "Infinity",
                "Jet",
                "Justrite",
                "Kero",
                "Leroy Merlin",
                "Makro",
                "Naivas",
                "OK Foods",
                "Panarottis",
                "Pick n Pay",
                "PnA",
                "PQ Clothing",
                "Rage",
                "Sefalana",
                "Sasol",
                "Shell",
                "Shoprite",
                "Signature Cosmetics & Fragrances",
                "Spar",
                "Spur",
                "TFG Group",
                "Toys R Us",
                "Woermann Brock",
                "Woolworths"
              ],
              required: true,
              editable: true,
              enableSearch: false,
            ),
            ValueListenableBuilder(
              valueListenable: shopName,
              builder: (BuildContext context, String value, Widget? child) {
                return Visibility(
                  visible: value != "",
                  child: Column(
                    children: [
                      const SizedBox(height: 10),
                      MihCardDisplay(
                          shopName: shopName.value, nickname: "", height: 200),
                    ],
                  ),
                );
              },
            ),
            const SizedBox(height: 10),
            MIHTextField(
              controller: _nicknameController,
              hintText: "Card Title",
              editable: true,
              required: false,
            ),
            const SizedBox(height: 10),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: [
                Flexible(
                  child: MIHNumberField(
                    controller: cardNumberController,
                    hintText: "Card Number",
                    editable: true,
                    required: true,
                    enableDecimal: false,
                  ),
                ),
                const SizedBox(width: 10),
                MihButton(
                  onPressed: () {},
                  buttonColor:
                      MzanziInnovationHub.of(context)!.theme.secondaryColor(),
                  width: 100,
                  child: Text(
                    "Scan",
                    style: TextStyle(
                      color:
                          MzanziInnovationHub.of(context)!.theme.primaryColor(),
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ],
            ),
            const SizedBox(height: 15),
            MihButton(
              onPressed: () {
                if (shopController.text == "" ||
                    cardNumberController.text == "") {
                  showDialog(
                    context: context,
                    builder: (context) {
                      return const MIHErrorMessage(errorType: "Input Error");
                    },
                  );
                } else {
                  MIHMzansiWalletApis.addLoyaltyCardAPICall(
                    widget.signedInUser,
                    widget.signedInUser.app_id,
                    shopController.text,
                    cardNumberController.text,
                    "",
                    0,
                    _nicknameController.text,
                    0,
                    context,
                  );
                }
              },
              buttonColor:
                  MzanziInnovationHub.of(context)!.theme.secondaryColor(),
              width: 300,
              child: Text(
                "Add",
                style: TextStyle(
                  color: MzanziInnovationHub.of(context)!.theme.primaryColor(),
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    cardNumberController.dispose();
    shopController.removeListener(shopSelected);
    shopController.dispose();
    cardSearchController.removeListener(searchShop);
    cardSearchController.dispose();
    searchShopName.dispose();
    _nicknameController.dispose();
    searchFocusNode.dispose();
    shopName.dispose();
    super.dispose();
  }

  @override
  void initState() {
    cardList = MIHMzansiWalletApis.getLoyaltyCards(widget.signedInUser.app_id);
    shopController.addListener(shopSelected);
    cardSearchController.addListener(searchShop);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final Size size = MediaQuery.sizeOf(context);
    final double width = size.width;
    return MihPackageToolBody(
      borderOn: false,
      bodyItem: getBody(width),
    );
  }

  Widget getBody(double width) {
    return Stack(
      children: [
        MihSingleChildScroll(
          child: Column(
            children: [
              Padding(
                padding: EdgeInsets.symmetric(horizontal: width / 20),
                child: MihSearchBar(
                  controller: cardSearchController,
                  hintText: "Search Cards",
                  // prefixIcon: Icons.search,
                  prefixIcon: Icons.search,
                  fillColor:
                      MzanziInnovationHub.of(context)!.theme.secondaryColor(),
                  hintColor:
                      MzanziInnovationHub.of(context)!.theme.primaryColor(),
                  onPrefixIconTap: () {
                    print("Search Icon Pressed: ${cardSearchController.text}");
                  },
                  searchFocusNode: searchFocusNode,
                ),
              ),
              const SizedBox(height: 10),
              FutureBuilder(
                future: cardList,
                builder: (context, snapshot) {
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return const Center(
                      child: Mihloadingcircle(),
                    );
                  } else if (snapshot.connectionState == ConnectionState.done &&
                      snapshot.hasData) {
                    listOfCards = snapshot.data!;
                    searchShop();
                    return ValueListenableBuilder(
                      valueListenable: searchShopName,
                      builder: (BuildContext context,
                          List<MIHLoyaltyCard> value, Widget? child) {
                        return BuildLoyaltyCardList(
                          cardList: searchShopName.value,
                          signedInUser: widget.signedInUser,
                          navIndex: 0,
                        );
                      },
                    );
                  } else {
                    return const Center(
                      child: Text("Error Loading Loyalty Cards"),
                    );
                  }
                },
              ),
            ],
          ),
        ),
        Positioned(
          right: 10,
          bottom: 10,
          child: MihFloatingMenu(
              animatedIcon: AnimatedIcons.menu_close,
              children: [
                SpeedDialChild(
                  child: Icon(
                    Icons.add,
                    color:
                        MzanziInnovationHub.of(context)!.theme.primaryColor(),
                  ),
                  label: "Add Loyalty Card",
                  labelBackgroundColor:
                      MzanziInnovationHub.of(context)!.theme.successColor(),
                  labelStyle: TextStyle(
                    color:
                        MzanziInnovationHub.of(context)!.theme.primaryColor(),
                    fontWeight: FontWeight.bold,
                  ),
                  backgroundColor:
                      MzanziInnovationHub.of(context)!.theme.successColor(),
                  onTap: () {
                    addCardWindow(context);
                  },
                )
              ]),
        )
      ],
    );
  }
}
