import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart';
import 'package:get/get.dart';

import '../../../core/config/server_response.dart';
import '../../../core/data/repositories/app_repositories.dart';
import '../../../core/utils/SessionCache.dart';
import '../../../custom_widgets/custom_toasty.dart';
import '../../../models/DepositAccountResponse.dart';
import '../../../models/ExchangeRate.dart';
import '../../../models/dashboard_response_model.dart';
import '../../u_received/controllers/u_received_controller.dart';
import '../../u_send/controllers/u_send_controller.dart';

RxInt transitionUpdate = RxInt(0);

class DashboardScreenController extends GetxController {
  BuildContext context = Get.context as BuildContext;

  final AppRepositories repository = Get.find<AppRepositories>();

  RxBool isDrawerOpen = false.obs;
  int curentIndex = 0;

  RxList<DepositAccount> depositeAccount = <DepositAccount>[DepositAccount.empty()].obs;
  final ZoomDrawerController zoomController = ZoomDrawerController();

  /// Change Icon method
  void toggleDrawer() {
    isDrawerOpen.value = !isDrawerOpen.value;
    if (isDrawerOpen.value) {
      zoomController.open!();
    } else {
      zoomController.close!();
    }
  }

  @override
  Future<void> onReady() async {
    super.onReady();
    var arguments = Get.arguments;
    fetchDepositAccounts();
    fetchExchangeRates();

    transitionUpdate.listen(
      (p0) {
        fetchDepositAccounts();
        fetchExchangeRates();
      },
    );
  }

  @override
  void onInit() {
    super.onInit();
    // fingerPrintLogin();
  }

  // Future<void> fingerPrintLogin() async {
  //   String info = await AppStorage.getString(AppConstants.IS_FINGER_PRINT_ENABLED, fallback: "");
  //   if (info.isNotEmpty) {
  //     FingerPrintInfo fingerPrintInfo = FingerPrintInfo.fromMap(jsonDecode(info));
  //     if (fingerPrintInfo.cmpUserId != SessionCache.instance.userInfo.cmpUserId || fingerPrintInfo.cmpCustpassword != SessionCache.instance.userInfo.cmpCustpassword) {
  //       // SessionCache.instance.fingerPrintController.clearFromDB();
  //     }
  //   }
  // }

  Future<void> fetchDepositAccounts() async {
    ServerResponse response = await repository.fetchAccountDetails();
    if (response.isError) {
      Toasty.error(response.errorMsg);
      return;
    }
    DashBoardResponseModel dashboardResponse = DashBoardResponseModel.fromMap(response.response);
    SessionCache.instance.depositAccountList.value = dashboardResponse.deposit;
    depositeAccount.assignAll(SessionCache.instance.depositAccountList);
  }

  Future<void> fetchExchangeRates() async {
    ServerResponse response = await repository.fetchExchangeRate();
    if (response.isError) {
      Toasty.error(response.errorMsg);
      return;
    }
    SessionCache.instance.exchangeRateList.value = List<ExchangeRate>.from(((response.response ?? []) as List<dynamic>).map((x) => ExchangeRate.fromMap(x)));

    SessionCache.instance.exchangeRateList.insert(
        0,
        ExchangeRate(
          petExrtdesc: "Official Rate",
          pcrCurrcode: "586",
          perEratrateact: 1.0,
          petExrtcode: "3",
          perEratdate: "2024-06-27",
          pcrCurrshort: "PKR",
          porOrgacode: "0006",
          pcrCurrdesc: "Pakistani Rupees",
        ));
  }

  updatePage(int index) {
    Get.put(UReceivedController());

    curentIndex = index;

    update();
    if (index != 1) {
      Get.find<USendController>().clearTransaction();
    }
  }

  /// Get Flag fucntion
  String getFlagWithCurrencyCode(currencyCodeInt) {
    if (currencyCodeInt == "586") {
      return "assets/uco/icons/ic_pakistan.png";
    } else if (currencyCodeInt == "997") {
      return "assets/uco/icons/usa.png";
    } else {
      return "assets/uco/icons/euro_flag.png";
    }

    //  else {
    //   return "assets/uco/icons/default_flag.png";
    //  }
  }
}
