import 'package:domain/repository/facility_repository.dart';
import 'package:plugin_platform/engine/loading/loading_engine.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:router/componentRouter/component_service_manager.dart';
import 'package:router/ext/auto_router_extensions.dart';

import 'facility_detail_state.dart';

part 'facility_detail_view_model.g.dart';

@riverpod
class FacilityDetailViewModel extends _$FacilityDetailViewModel with DioCancelableMixin{
  late final FacilityRepository _facilityRepository;
  @override
  FacilityDetailState build() {
    _facilityRepository = ref.read(facilityRepositoryProvider);
    final state =  FacilityDetailState();
    registerCancellation();
    return state;
  }

  /// 获取详情
  Future fetchFacilityDetail(String? bookingId) async {
    final result = await _facilityRepository.fetchFacilityBookingDetail(id: bookingId, cancelToken: cancelToken);

    // 处理数据
    if (result.isSuccess) {
      state = state.copyWith(detail: result.data);
    } else {
      ToastEngine.show(result.errorMsg ?? "UnKnow Error");
    }
  }

  //根据OrderId继续支付订单
  // void doPaymentOrder(String orderId) async {
  //   //支付流程 Loading 开始
  //   LoadingEngine.show();
  //
  //   //调用支付服务
  //   bool success = await ComponentServiceManager().paymentService.executePayment(orderId: orderId);
  //
  //   //支付流程 Loading 结束
  //   LoadingEngine.dismiss();
  //
  //   //如果成功，返回页面
  //   if (success) {
  //     appRouter.maybePop();
  //   }
  // }

}
