package com.izouma.nineth.web.mobile.v1;


import com.izouma.nineth.domain.User;
import com.izouma.nineth.domain.UserFundAccount;
import com.izouma.nineth.dto.TradeTypeDTO;
import com.izouma.nineth.dto.pay.TradeType;
import com.izouma.nineth.dto.userfund.BalanceInfoDTO;
import com.izouma.nineth.enums.AccountStatus;
import com.izouma.nineth.service.UserFundAccountForLianlianpayService;
import com.izouma.nineth.service.pay.UniformPayConstants;
import com.izouma.nineth.service.pay.UniformPayUtil;
import com.izouma.nineth.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/payOrder/v1")
@Slf4j
@AllArgsConstructor
public class OrderPayControllerV1 {
    private final UniformPayUtil uniformPayUtil;
    private UserFundAccountForLianlianpayService userFundAccountForLianlianpayService;

    /**
     * 获取支持的交易渠道
     * @return
     */
    @RequestMapping("/getSupportTradeType")
    @ResponseBody
    public List<TradeTypeDTO> getSupportTradeType(){
        List<TradeTypeDTO> tradeTypeList = new ArrayList<>();

        String defaultPlatform = uniformPayUtil.getDefaultPayPlatform();
        if(UniformPayConstants.NOTPAY_PLATFORM.equals(defaultPlatform)){
            //启用钱包功能
            final User user = SecurityUtils.getAuthenticatedUser();
            if(user != null){

                UserFundAccount lianlianAccount = userFundAccountForLianlianpayService.getAccount(user.getId());
                if(lianlianAccount != null && lianlianAccount.getAccountStatus() == AccountStatus.NORMAL){
                    TradeTypeDTO walletpay = new TradeTypeDTO();
                    walletpay.setName(TradeType.WALLET_ACCOUNT.name());
                    walletpay.setDescription("钱包支付");
                    //查询余额
                    try {
                        BalanceInfoDTO balanceInfoDTO = userFundAccountForLianlianpayService.getBalance(lianlianAccount.getUserId());
                        walletpay.setTotalBalance(balanceInfoDTO.getTotalBalance());
                    }catch(Exception e){
                        log.error("查询余额失败：" + e.getMessage());
                    }

                    tradeTypeList.add(walletpay);
                }


                boolean isBindCard = uniformPayUtil.isBindBankCard(user.getId());
                if(isBindCard) {
                    TradeTypeDTO quickpay = new TradeTypeDTO();
                    quickpay.setName(TradeType.QUICK_PAY.name());
                    quickpay.setDescription("快捷支付");
                    tradeTypeList.add(quickpay);
                }


            }

        }else{

            TradeTypeDTO defaultPay = new TradeTypeDTO();
            defaultPay.setName(TradeType.ALIPAY_WAP.name());
            defaultPay.setDescription("默认支付");
            tradeTypeList.add(defaultPay);

        }

        return tradeTypeList;
    }

}
