package com.bizzan.bitrade.service;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bizzan.bitrade.constant.TransactionType;
import com.bizzan.bitrade.entity.FollowWallet;
import com.bizzan.bitrade.entity.MemberHistory;
import com.bizzan.bitrade.entity.MemberWallet;
import com.bizzan.bitrade.entity.UserPurse;
import com.bizzan.bitrade.vo.MemberWalletVo;

@Service
public class BalanceService {

	@Autowired
    private UserPurseService userPurseService;
    @Autowired
    private FollowWalletService followWalletService;
    @Autowired
    private MemberWalletService memberWalletService;
    @Autowired
    private MemberHistoryService memberHistoryService;
    @Autowired
    private MemberTransactionService transactionService;
    
    public BigDecimal sumBalanceByMemberIds(List<Long> ids) {
    	BigDecimal swapBalance = userPurseService.sumBalanceByMemberIds(ids);
        BigDecimal followBalance = followWalletService.sumBalanceByMemberIds(ids);
        BigDecimal spotBalance = memberWalletService.sumBalanceByMemberIds(ids);
        BigDecimal totalBalance = spotBalance.add(swapBalance).add(followBalance);
        return totalBalance;
    }
    
    public BigDecimal sumBalanceByMemberId(Long id) {
    	UserPurse userPurse = userPurseService.findOne(id);
    	BigDecimal contractBalance = userPurse.getBalance().add(userPurse.getFrozenBalance()).add(userPurse.getFrozenMargin());
    	FollowWallet followWallet = followWalletService.findOne(id);
    	BigDecimal followBalance = followWallet.getBalance().add(followWallet.getFrozenBalance());
    	BigDecimal spotBalance = memberWalletService.sumBalanceByMemberId(id);
		BigDecimal totalBalance = spotBalance.add(contractBalance).add(followBalance);
		return totalBalance;
    }
    
    public MemberWalletVo getMemberWalletVo(Long id) {
    	MemberWallet wallet = memberWalletService.findByCoinUnitAndMemberId("USDT", id);
    	if (wallet == null) {
    		MemberWalletVo vo = new MemberWalletVo();
    		vo.setMemberId(id);
    		vo.setBalance(BigDecimal.ZERO);
    		vo.setFrozenBalance(BigDecimal.ZERO);
    		vo.setWithdrawableBalance(BigDecimal.ZERO);
    		vo.setTransferableBalance(BigDecimal.ZERO);
    		return vo;
    	}

        BigDecimal totalBalance = this.sumBalanceByMemberId(id).subtract(wallet.getFrozenBalance());
        MemberHistory memberHistory = memberHistoryService.sumMemberHistoryByMemberId(id);
        BigDecimal adminRechargeBalance = BigDecimal.ZERO;
        if (memberHistory.getAdminRecharge()!=null)
        	adminRechargeBalance = memberHistory.getAdminRecharge();
        adminRechargeBalance = adminRechargeBalance.add(transactionService.sumTodayAmountByMemberIdAndType(id, TransactionType.ADMIN_RECHARGE));
        BigDecimal withdrawableBalance = wallet.getBalance();
        BigDecimal remainingBalance = totalBalance.subtract(adminRechargeBalance);
        if (remainingBalance.compareTo(withdrawableBalance) < 0) {
        	if (remainingBalance.compareTo(BigDecimal.ZERO) < 0)
        		withdrawableBalance = BigDecimal.ZERO;
        	else
        		withdrawableBalance = remainingBalance;
        }

        MemberWalletVo vo = new MemberWalletVo();
        vo.setMemberId(id);
        vo.setBalance(wallet.getBalance());
        vo.setFrozenBalance(wallet.getFrozenBalance());
        vo.setWithdrawableBalance(withdrawableBalance);
        if (adminRechargeBalance.compareTo(BigDecimal.ZERO) > 0) {
        	vo.setTransferableBalance(BigDecimal.ZERO);
        } else {
        	vo.setTransferableBalance(withdrawableBalance);
        }
        return vo;
    }
}
