import 'dart:typed_data';

import 'package:domain/entity/comm_payment_detail_entity.dart';
import 'package:domain/entity/payment_intent_entity.dart';
import 'package:domain/entity/payment_page_entity.dart';
import 'package:plugin_platform/platform_export.dart';
import 'package:plugin_platform/http/dio_engine.dart';
import 'package:plugin_platform/http/http_result.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../constants/api_constants.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:plugin_basic/provider/http_provider/http_provider.dart';

part 'payment_repository.g.dart';

@Riverpod(keepAlive: true)
PaymentRepository paymentRepository(Ref ref) {
  final dioEngine = ref.watch(dioEngineProvider);
  return PaymentRepository(dioEngine: dioEngine);
}

/*
 * 支付模块的数据仓库
 */
class PaymentRepository {
  DioEngine dioEngine;

  PaymentRepository({required this.dioEngine});


  /// 获取支付列表
  Future<HttpResult<PaymentPageEntity>> fetchPaymentList({
    required String type,
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params['type'] = type;
    params['page'] = curPage.toString();
    params['limit'] = "10";

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiPaymentList,
      params: params,
      method: HttpMethod.GET,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = PaymentPageEntity.fromJson(json!);
      return result.convert<PaymentPageEntity>(data: data);
    }
    return result.convert();
  }

  /// 获取支付列表
  Future<HttpResult<CommPaymentDetailEntity>> fetchPaymentDetail({
    required String orderId,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params['order_id'] = orderId;

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiPaymentDetail,
      params: params,
      method: HttpMethod.GET,
      networkDebounce: true,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = CommPaymentDetailEntity.fromJson(json!);
      return result.convert<CommPaymentDetailEntity>(data: data);
    }
    return result.convert();
  }



  /// 根据 OrderId 生成对应的 PaymentIntent
  Future<HttpResult<PaymentIntentEntity>> obtainPaymentIntent({
    required String orderId,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params['order_id'] = orderId;

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiPaymentIntent,
      params: params,
      method: HttpMethod.POST,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = PaymentIntentEntity.fromJson(json!);
      return result.convert<PaymentIntentEntity>(data: data);
    }
    return result.convert();
  }
}
