import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../../../app.dart';

class ContactsController extends GetxController {
  RxBool isLoading = false.obs;
  RxBool hasError = false.obs;
  ContactsModel? contacts;

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

  Future<void> getContacts() async {
    if (contacts != null) return;
    isLoading.value = true;
    hasError.value = false;

    final result = await AddressesApi.getContacts();

    if (result == null) hasError.value = true;

    contacts = result;

    isLoading.value = false;
  }

  Future<void> makePhoneCall() async {
    if (contacts != null) {
      final Uri launchUri = Uri(
        scheme: 'tel',
        path: contacts?.phone,
      );
      await launchUrl(launchUri);
    }
  }
}
