import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:local_auth/local_auth.dart';
import 'package:SEFER/constant/colors.dart';
import 'package:SEFER/constant/links.dart';
import 'package:SEFER/controller/functions/crud.dart';

class PassengerAdminController extends GetxController {
  bool isLoading = false;
  Map passengersData = {};
  Map passengers = {};
  double height = 150;
  final formPassKey = GlobalKey<FormState>();
  final formPrizeKey = GlobalKey<FormState>();
  final titleNotify = TextEditingController();
  final bodyNotify = TextEditingController();
  final passengerController = TextEditingController();
  final passengerPrizeController = TextEditingController();

  Future getPassengerCount() async {
    isLoading = true;
    update();
    var res = await CRUD().get(link: AppLink.getPassengerDetails, payload: {});
    var d = jsonDecode(res);
    if (d['status'] == 'success') {
      passengersData = d;
    }

    isLoading = false;
    update();
  }

  Future addPassengerPrizeToWallet() async {
    for (var i = 0; i < passengersData['message'].length; i++) {
      await CRUD().post(link: AppLink.addPassengersWallet, payload: {
        'passenger_id': passengersData['message'][i]['id'],
        'balance': passengerPrizeController.text,
      });
    }

    Get.back();
  }

  void addPassengerPrizeToWalletSecure() async {
    try {
      // Check if local authentication is available
      bool isAvailable = await LocalAuthentication().isDeviceSupported();
      if (isAvailable) {
        // Authenticate the user
        bool didAuthenticate = await LocalAuthentication().authenticate(
          localizedReason: 'Use Touch ID or Face ID to confirm payment',
        );
        if (didAuthenticate) {
          // User authenticated successfully, proceed with payment
          await addPassengerPrizeToWallet();
          Get.snackbar('Prize Added', '', backgroundColor: AppColor.greenColor);
        } else {
          // Authentication failed, handle accordingly
          Get.snackbar('Authentication failed', '',
              backgroundColor: AppColor.redColor);
          // print(
          //     'Authentication failed');
        }
      } else {
        // Local authentication not available, proceed with payment without authentication
        await addPassengerPrizeToWallet();
        Get.snackbar('Prize Added', '', backgroundColor: AppColor.greenColor);
      }
    } catch (e) {
      rethrow;
    }
  }

  Future getPassengers() async {
    var res = await CRUD().get(link: AppLink.getPassengerbyEmail, payload: {
      'passengerEmail': passengerController.text,
      'passengerId': passengerController.text,
      'passengerphone': passengerController.text,
    });
    var d = jsonDecode(res);
    // print(d['message']);
    if (d['status'] == 'success') {
      passengers = d;
    }
    // print(passengers);
    update();
  }

  changeHeight() {
    if (passengers.isEmpty) {
      height = 0;
      update();
    }
    height = 150;
    update();
  }

  void clearPlaces() {
    passengers = {};
    update();
  }

  @override
  void onInit() {
    getPassengerCount();
    super.onInit();
  }
}
