package com.bizzan.bitrade.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.bizzan.bitrade.entity.Coin;
import com.bizzan.bitrade.entity.FollowWallet;
import com.bizzan.bitrade.entity.MemberWallet;
import com.bizzan.bitrade.entity.UserPurse;
import com.bizzan.bitrade.model.AccountBalance;
import com.bizzan.bitrade.service.CoinService;
import com.bizzan.bitrade.service.FollowWalletService;
import com.bizzan.bitrade.service.MemberWalletService;
import com.bizzan.bitrade.service.UserPurseService;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/asset")
@Slf4j
public class InnerAssetController {

    @Autowired
    private UserPurseService userPurseService;
    @Autowired
    private FollowWalletService followWalletService;
    @Autowired
    private MemberWalletService walletService;
    @Autowired
    private CoinService coinService;

    @PostMapping("accounts")
    public List<AccountBalance> accounts(String ids) {
        log.info("asset/accounts ids={}", ids);
        List<Long> idList = JSON.parseArray(ids, Long.class);
        List<UserPurse> swapList = userPurseService.findByIds(idList);
        List<FollowWallet> followList = followWalletService.findByIds(idList);

        Coin coin = coinService.findByUnit("USDT");
        List<AccountBalance> accountList = new ArrayList<>();
        for (Long id : idList) {
            AccountBalance accountBalance = new AccountBalance();
            accountBalance.setMemberId(id);
            BigDecimal swapBalance = getSwapBalanceById(swapList, id);
            accountBalance.setSwapBalance(swapBalance);
            BigDecimal followBalance = getFollowBalanceById(followList, id);
            accountBalance.setFollowBalance(followBalance);
            MemberWallet memberWallet = walletService.findByCoinAndMemberId(coin, id);
            BigDecimal spotBalance = memberWallet.getBalance().add(memberWallet.getFrozenBalance());
            accountBalance.setSpotBalance(spotBalance);
            accountBalance.setTotalBalance(swapBalance.add(followBalance).add(spotBalance));
            accountList.add(accountBalance);
        }
        return accountList;
    }

    private BigDecimal getSwapBalanceById(List<UserPurse> swapList, Long id) {
        for (UserPurse wallet : swapList) {
            if (id.longValue() == wallet.getId())
                return wallet.getBalance().add(wallet.getFrozenBalance().add(wallet.getFrozenMargin()));
        }
        return BigDecimal.ZERO;
    }

    private BigDecimal getFollowBalanceById(List<FollowWallet> followList, Long id) {
        for (FollowWallet wallet : followList) {
            if (id.longValue() == wallet.getId())
                return wallet.getBalance().add(wallet.getFrozenBalance().add(wallet.getFrozenMargin()));
        }
        return BigDecimal.ZERO;
    }
}
