import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../data/datasources/remote/trading_remote_datasource.dart';
import '../../data/models/p2p_transfer_model.dart';
import '../../data/models/p2p_transfer_fee_config_model.dart';
import '../../core/di/injection.dart';

/// P2P转账状态
class TransferState {
  final bool isLoading;
  final String? error;
  final P2pTransferModel? lastTransfer;
  final AccountLookupModel? recipientAccount;

  TransferState({
    this.isLoading = false,
    this.error,
    this.lastTransfer,
    this.recipientAccount,
  });

  TransferState copyWith({
    bool? isLoading,
    String? error,
    P2pTransferModel? lastTransfer,
    AccountLookupModel? recipientAccount,
    bool clearError = false,
    bool clearRecipient = false,
  }) {
    return TransferState(
      isLoading: isLoading ?? this.isLoading,
      error: clearError ? null : (error ?? this.error),
      lastTransfer: lastTransfer ?? this.lastTransfer,
      recipientAccount: clearRecipient ? null : (recipientAccount ?? this.recipientAccount),
    );
  }
}

class TransferNotifier extends StateNotifier<TransferState> {
  final TradingRemoteDataSource _dataSource;

  TransferNotifier(this._dataSource) : super(TransferState());

  /// 查询收款方账户
  Future<AccountLookupModel?> lookupRecipient(String phone) async {
    state = state.copyWith(isLoading: true, clearError: true, clearRecipient: true);
    try {
      final account = await _dataSource.lookupAccount(phone);
      state = state.copyWith(
        isLoading: false,
        recipientAccount: account,
      );
      return account;
    } catch (e) {
      state = state.copyWith(
        isLoading: false,
        error: e.toString(),
      );
      return null;
    }
  }

  /// 执行P2P转账
  Future<bool> transfer({
    required String toPhone,
    required String amount,
    String? memo,
  }) async {
    state = state.copyWith(isLoading: true, clearError: true);
    try {
      final result = await _dataSource.p2pTransfer(
        toPhone: toPhone,
        amount: amount,
        memo: memo,
      );
      state = state.copyWith(
        isLoading: false,
        lastTransfer: result,
      );
      return true;
    } catch (e) {
      state = state.copyWith(
        isLoading: false,
        error: e.toString(),
      );
      return false;
    }
  }

  /// 清除状态
  void clearState() {
    state = TransferState();
  }

  /// 清除错误
  void clearError() {
    state = state.copyWith(clearError: true);
  }
}

final transferNotifierProvider =
    StateNotifierProvider<TransferNotifier, TransferState>(
  (ref) => TransferNotifier(getIt<TradingRemoteDataSource>()),
);

/// P2P转账手续费配置
final p2pTransferFeeConfigProvider = FutureProvider<P2pTransferFeeConfigModel>(
  (ref) async {
    final dataSource = getIt<TradingRemoteDataSource>();
    return dataSource.getP2pTransferFeeConfig();
  },
);

/// P2P转账历史记录
final p2pTransferHistoryProvider =
    FutureProvider.family<List<P2pTransferModel>, String>(
  (ref, accountSequence) async {
    final dataSource = getIt<TradingRemoteDataSource>();
    return dataSource.getP2pTransferHistory(accountSequence);
  },
);
