import 'dart:convert';

import 'package:get/get.dart';
import 'package:sefer_admin1/constant/colors.dart';

import '../../constant/links.dart';
import '../firebase/firbase_messge.dart';
import 'crud.dart';

class WalletController extends GetxController {
  String paymentToken = '';
  Future<String> generateTokenDriver(String amount, driverID) async {
    var res = await CRUD().post(link: AppLink.addPaymentTokenDriver, payload: {
      'driverID': driverID.toString(),
      'amount': amount.toString(),
    });
    var d = jsonDecode(res);
    return d['message'];
  }

  addPaymentToDriver(String amount, driverID, token) async {
    paymentToken = await generateTokenDriver(amount.toString(), driverID);
    var res = await CRUD().post(link: AppLink.addDrivePayment, payload: {
      'rideId': 'gift_$driverID _${DateTime.now().toIso8601String()}',
      'amount': amount,
      'payment_method': 'visaRide',
      'passengerID': 'gift',
      'token': paymentToken,
      'driverID': driverID.toString(),
    });
    if (res != 'failure') {
      FirebaseMessagesController().sendNotificationToAnyWithoutData(
        "لديك هدية من سفَر".tr,
        'لقد حصلت على هدية من سفر بقيمة $amount ',
        token, // Access token correctly
        'ding.wav',
      );
      Get.snackbar('success', 'addPaymentToDriver',
          backgroundColor: AppColor.greenColor);
    } else {
      Get.snackbar('error', 'addPaymentToDriver',
          backgroundColor: AppColor.redColor);
    }
  }

  Future addDriverWallet(String paymentMethod, driverID, point, phone) async {
    // paymentToken = await generateToken(count);
    // var paymentID = await getPaymentId(paymentMethod, point.toString());
    await CRUD().postWallet(link: AppLink.addFromAdmin, payload: {
      'driverID': driverID.toString(),
      'paymentID': 'gift_connect_$driverID${DateTime.timestamp()}'.toString(),
      'amount': point,
      'token': 'gift_connect',
      'paymentMethod': paymentMethod,
      'phone': phone,
    });
  }

  Future addDrivergift300(String paymentMethod, driverID, point, phone) async {
    // paymentToken = await generateToken(count);
    // var paymentID = await getPaymentId(paymentMethod, point.toString());
    var res = await CRUD().postWallet(link: AppLink.add300ToDriver, payload: {
      'driverID': driverID.toString(),
      'paymentID': paymentMethod,
      'amount': point,
      'token': 'gift_connect_300',
      'paymentMethod': paymentMethod,
      'phone': phone,
    });
    if (res != 'failure') {
      Get.snackbar('success', 'addDrivergift300',
          backgroundColor: AppColor.greenColor);
    } else {
      Get.snackbar('error', res, backgroundColor: AppColor.redColor);
    }
  }

  Future addSeferWallet(String point, driverID) async {
    var amount = (int.parse(point) * -1).toStringAsFixed(0);
    var seferToken = await generateTokenDriver(amount, driverID);
    var res = await CRUD().post(link: AppLink.addSeferWallet, payload: {
      'amount': amount.toString(),
      'paymentMethod': 'visaRide',
      'passengerId': 'gift$driverID',
      'token': seferToken,
      'driverId': driverID.toString(),
    });
    if (res != 'failure') {
      Get.snackbar('success', 'addSeferWallet',
          backgroundColor: AppColor.greenColor);
    } else {
      Get.snackbar('error', 'addSeferWallet',
          backgroundColor: AppColor.redColor);
    }
  }
}
