import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:hallo_doctor_doctor_app/app/models/withdraw_method_model.dart';
import 'package:hallo_doctor_doctor_app/app/models/bank_withdraw_method_model.dart';
import 'package:hallo_doctor_doctor_app/app/services/withdraw_service.dart';
import 'package:hallo_doctor_doctor_app/app/utils/exceptions.dart';

class WithdrawMethodController extends GetxController
    with StateMixin<List<WithdrawMethod>> {
  final amount = Get.arguments;
  @override
  void onInit() {
    super.onInit();
    getAllPaymentMethod();
  }

  void addPaypal(String name, String email) async {
    try {
      EasyLoading.show(maskType: EasyLoadingMaskType.black);
      await WithdrawService().addPaypalMethod(name, email);
      Get.back();
      getAllPaymentMethod();
    } catch (e) {
      exceptionToast(e.toString());
    } finally {
      EasyLoading.dismiss();
    }
  }

  void addBankAccount({
    required String name,
    required String accountNumber,
    required String accountHolderName,
    required String bankName,
    String? branchCode,
    String? swiftCode,
  }) async {
    try {
      EasyLoading.show(maskType: EasyLoadingMaskType.black);
      await WithdrawService().addBankMethod(
        name: name,
        accountNumber: accountNumber,
        accountHolderName: accountHolderName,
        bankName: bankName,
        branchCode: branchCode,
        swiftCode: swiftCode,
      );
      Get.back();
      getAllPaymentMethod();
    } catch (e) {
      exceptionToast(e.toString());
    } finally {
      EasyLoading.dismiss();
    }
  }

  getAllPaymentMethod() async {
    change([], status: RxStatus.loading());
    WithdrawService().getWithdrawMethod().then((value) {
      if (value.isEmpty) {
        change([], status: RxStatus.empty());
        return;
      }
      change(value, status: RxStatus.success());
    }).catchError((err) {
      exceptionToast(err.toString());
      change([], status: RxStatus.error(err.toString()));
    });
  }

  toWithdrawDetail(WithdrawMethod withdrawMethod) {
    Get.toNamed('/withdraw-detail', arguments: [
      {'withdrawMethod': withdrawMethod, 'amount': amount}
    ]);
  }
}
