import 'package:plugin_basic/basic_export.dart';
import 'package:plugin_platform/engine/notify/notify_engine.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared/utils/log_utils.dart';

part 'estate_upload_success_view_model.g.dart';

@riverpod
class EstateUploadSuccessViewModel extends _$EstateUploadSuccessViewModel {
  @override
  void build() {}

  //拨打电话
  void callPhone(String? phone) async {
    if (phone == null || phone.trim().isEmpty) {
      // 处理手机号为空的情况（如提示用户）
      ToastEngine.show("The contact number for this community has not been set up yet");
      return;
    }

    // 构造电话URL（tel:开头）
    final Uri phoneUri = Uri.parse('tel:${phone.trim()}');

    try {
      // 启动系统电话应用（externalApplication：打开外部应用）
      await launchUrl(phoneUri, mode: LaunchMode.externalApplication);
    } catch (e) {
      Log.e('无法拨打电话：$e');
      NotifyEngine.showFailure('Unable to make a phone call');
    }
  }

  //发送邮件

  void callEmail(String? email) async {
    if (email == null || email.trim().isEmpty) {
      // 处理邮箱为空的情况
      ToastEngine.show("The email for this community has not been set up yet");
      return;
    }

    // 构造邮件URL（mailto:开头）
    final Uri emailUri = Uri.parse('mailto:${email.trim()}');

    try {
      // 启动系统邮件应用
      await launchUrl(emailUri, mode: LaunchMode.externalApplication);
    } catch (e) {
      // 处理异常（如未安装邮件应用）
      Log.e('无法发送邮件：$e');
      NotifyEngine.showFailure('Unable to send email');
    }
  }
}
