import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:Intaleq/constant/box_name.dart';
import 'package:Intaleq/constant/links.dart';
import 'package:Intaleq/controller/functions/crud.dart';
import 'package:Intaleq/main.dart';

class CaptainWalletController extends GetxController {
  bool isLoading = false;
  Map walletDate = {};
  Map walletDateVisa = {};
  Map walletDriverPointsDate = {};
  final formKey = GlobalKey<FormState>();
  String totalAmount = '0';
  String totalAmountVisa = '0';
  String totalPoints = '0';
  final amountFromBudgetController = TextEditingController();

  payFromBudget() async {
    if (formKey.currentState!.validate()) {
      var pointFromBudget = box.read(BoxName.countryCode) == 'Jordan'
          ? int.parse((amountFromBudgetController.text)) * 100
          : int.parse((amountFromBudgetController.text));

      await addDriverPayment('fromBudgetToPoints',
          int.parse((amountFromBudgetController.text)) * -1);
      Future.delayed(const Duration(seconds: 2));
      await addDriverWallet('fromBudget', pointFromBudget.toString());
      update();
      Get.back();
      // getCaptainWalletFromRide();
      // getCaptainWalletFromBuyPoints();
      // checkAccountCaptainBank();
    }
  }

  // Future getCaptainWalletFromRide() async {
  //   isLoading = true;
  //   update();
  //   var res = await CRUD().get(
  //     link: AppLink.getAllPaymentFromRide,
  //     payload: {'driverID': box.read(BoxName.driverID)},
  //   );
  //   walletDate = jsonDecode(res);
  //   totalAmount = walletDate['message'][0]['total_amount'].toString() == null
  //       ? '0'
  //       : walletDate['message'][0]['total_amount'];

  //   var res1 = await CRUD().get(
  //       link: AppLink.getAllPaymentVisa,
  //       payload: {'driverID': box.read(BoxName.driverID)});
  //   walletDateVisa = jsonDecode(res1);
  //   totalAmountVisa = walletDateVisa['message'][0]['diff'].toString() == null
  //       ? '0'
  //       : walletDateVisa['message'][0]['diff'];
  //   isLoading = false;
  //   update();
  // }

  // Future getCaptainWalletFromBuyPoints() async {
  //   isLoading = true;
  //   update();
  //   var res = await CRUD().get(
  //     link: AppLink.getDriverPaymentPoints,
  //     payload: {'driverID': box.read(BoxName.driverID)},
  //   );
  //   walletDriverPointsDate = jsonDecode(res);
  //   if (walletDriverPointsDate['message'][0]['driverID'].toString() ==
  //       box.read(BoxName.driverID)) {
  //     double totalPointsDouble = double.parse(
  //         walletDriverPointsDate['message'][0]['total_amount'].toString());
  //     totalPoints = totalPointsDouble.toStringAsFixed(0);
  //   } else {
  //     totalPoints = '0';
  //   }

  //   isLoading = false;
  //   update();
  // }

  late String paymentID;
  Future addDriverPayment(String paymentMethod, amount) async {
    var res =
        await CRUD().postWallet(link: AppLink.addDriverPaymentPoints, payload: {
      'driverID': box.read(BoxName.driverID).toString(),
      'amount': amount.toString(),
      'payment_method': paymentMethod.toString(),
    });
    var d = jsonDecode(res);
    paymentID = d['message'].toString();
  }

  Future addDriverWallet(String paymentMethod, point) async {
    await CRUD().postWallet(link: AppLink.addDriversWalletPoints, payload: {
      'driverID': box.read(BoxName.driverID).toString(),
      'paymentID': paymentID.toString(),
      'amount': point,
      'paymentMethod': paymentMethod.toString(),
    });
  }

  //check if account bank is created or not
  Future checkAccountCaptainBank() async {
    isLoading = false;
    update();
    if (box.read(BoxName.accountIdStripeConnect).toString().isEmpty) {
      var res = await CRUD().getWallet(link: AppLink.getAccount, payload: {
        'id': box.read(BoxName.driverID).toString(),
      });
      var d = jsonDecode(res);
      if (d['status'] != 'failure') {
        box.write(BoxName.accountIdStripeConnect,
            d['message'][0]['accountBank'].toString());
      }
    }
    isLoading = true;
    update();
  }

  @override
  void onInit() {
    // getCaptainWalletFromRide();
    // getCaptainWalletFromBuyPoints();
    // checkAccountCaptainBank();
    super.onInit();
  }
}
