import 'package:flutter/material.dart';
import '../data/models/account_models.dart';
import '../data/services/asset_service.dart';
import '../data/services/fund_service.dart';
import '../core/network/dio_client.dart';

/// 资产状态管理
class AssetProvider extends ChangeNotifier {
  final AssetService _assetService;
  final FundService _fundService;

  AssetOverview? _overview;
  AccountFund? _fundAccount;
  List<AccountTrade> _tradeAccounts = [];
  List<AccountFlow> _flows = [];
  bool _isLoading = false;
  bool _isLoadingFlows = false;
  String? _error;

  AssetProvider(this._assetService, this._fundService);

  // Getters
  AssetOverview? get overview => _overview;
  AccountFund? get fundAccount => _fundAccount;
  List<AccountTrade> get tradeAccounts => _tradeAccounts;
  List<AccountTrade> get holdings => _tradeAccounts;
  List<AccountFlow> get flows => _flows;
  bool get isLoading => _isLoading;
  bool get isLoadingFlows => _isLoadingFlows;
  String? get error => _error;

  /// 加载资产总览
  Future<void> loadOverview() async {
    _isLoading = true;
    _error = null;
    notifyListeners();

    try {
      final response = await _assetService.getOverview();
      if (response.success) {
        _overview = response.data;
      } else {
        _error = response.message;
      }
    } catch (e) {
      _error = '加载失败: $e';
    }

    _isLoading = false;
    notifyListeners();
  }

  /// 加载资金账户
  Future<void> loadFundAccount() async {
    try {
      final response = await _assetService.getFundAccount();
      if (response.success) {
        _fundAccount = response.data;
        notifyListeners();
      }
    } catch (_) {
      // 忽略错误
    }
  }

  /// 加载交易账户
  Future<void> loadTradeAccount() async {
    try {
      final response = await _assetService.getTradeAccount();
      if (response.success) {
        _tradeAccounts = response.data ?? [];
        notifyListeners();
      }
    } catch (_) {
      // 忽略错误
    }
  }

  /// 加载资金流水
  Future<void> loadFlows({int? flowType, int pageNum = 1}) async {
    _isLoadingFlows = true;
    notifyListeners();

    try {
      final response = await _assetService.getFlow(
        flowType: flowType,
        pageNum: pageNum,
      );
      if (response.success && response.data != null) {
        final list = response.data!['list'] as List?;
        _flows = list?.map((e) => AccountFlow.fromJson(e as Map<String, dynamic>)).toList() ?? [];
      }
    } catch (_) {
      // 忽略错误
    }

    _isLoadingFlows = false;
    notifyListeners();
  }

  /// 划转资金
  Future<ApiResponse<void>> transfer({
    required int direction,
    required String amount,
  }) async {
    try {
      final response = await _assetService.transfer(
        direction: direction,
        amount: amount,
      );
      if (response.success) {
        // 刷新数据
        await loadOverview();
        await loadFundAccount();
        await loadTradeAccount();
      }
      return response;
    } catch (e) {
      return ApiResponse.fail('划转失败: $e');
    }
  }

  /// 充值
  Future<ApiResponse<void>> deposit({required String amount, String? remark}) async {
    try {
      final response = await _fundService.deposit(amount: amount, remark: remark);
      if (response.success) {
        await loadOverview();
        await loadFundAccount();
      }
      return response;
    } catch (e) {
      return ApiResponse.fail('充值申请失败: $e');
    }
  }

  /// 提现
  Future<ApiResponse<void>> withdraw({required String amount, String? remark}) async {
    try {
      final response = await _fundService.withdraw(amount: amount, remark: remark);
      if (response.success) {
        await loadOverview();
        await loadFundAccount();
      }
      return response;
    } catch (e) {
      return ApiResponse.fail('提现申请失败: $e');
    }
  }

  /// 刷新所有资产数据
  Future<void> refreshAll() async {
    await Future.wait([
      loadOverview(),
      loadFundAccount(),
      loadTradeAccount(),
    ]);
  }
}
