package com.bizzan.bitrade.controller;


import com.bizzan.bitrade.service.BiPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.spark.bipay.constant.CoinType;
import com.spark.bipay.entity.Address;
import com.spark.bipay.entity.SupportCoin;
import com.spark.bipay.entity.Transaction;
import com.spark.bipay.http.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
public class RequestController {
    private static final Logger log = LoggerFactory.getLogger(RequestController.class);
    @Autowired
    private BiPayService biPayService;

    /**
     * 创建新地址
     *
     * @param coinType
     * @return
     */
    @RequestMapping("/bipay/create-address")
    public Address createCoinAddress(int coinType) {
        log.info("[Udun-接口] 创建地址 入参 coinType={}", coinType);
        Address result = biPayService.createCoinAddress(CoinType.codeOf(coinType),"","");
        log.info("[Udun-接口] 创建地址 返回 address={}", result != null ? result.getAddress() : null);
        return result;
    }

    /**
     * 发起转账请求
     *
     * @param coinType
     * @param amount
     * @param address
     * @return
     */
    @RequestMapping("/bipay/transfer")
    public ResponseMessage<String> transfer(int coinType, BigDecimal amount, String address, String memo) {
        String orderId = String.valueOf(Calendar.getInstance().getTimeInMillis());
        CoinType coin = CoinType.codeOf(coinType);
        log.info("[Udun-接口] 提币 入参 coinType={}, amount={}, address={}, memo={}, orderId={}", coinType, amount, address, memo, orderId);
        ResponseMessage<String> resp = biPayService.transfer(orderId, amount, coin, coin.getCode(), address, memo);
        log.info("[Udun-接口] 提币 返回 code={}, message={}, data={}", resp.getCode(), resp.getMessage(), resp.getData());
        return resp;
    }

    /**
     * 代付
     *
     * @param coinType
     * @param amount
     * @param address
     * @return
     */
    @RequestMapping("/bipay/autotransfer")
    public ResponseMessage<String> autoTransfer(int coinType, BigDecimal amount, String address, String memo) {
        String orderId = String.valueOf(Calendar.getInstance().getTimeInMillis());
        CoinType coin = CoinType.codeOf(coinType);
        log.info("[Udun-接口] 代付 入参 coinType={}, amount={}, address={}, memo={}, orderId={}", coinType, amount, address, memo, orderId);
        ResponseMessage<String> resp = biPayService.autoTransfer(orderId, amount, coin, coin.getCode(), address, memo);
        log.info("[Udun-接口] 代付 返回 code={}, message={}, data={}", resp.getCode(), resp.getMessage(), resp.getData());
        return resp;
    }

    @RequestMapping("/test")
    public String test() {
        return "Success";
    }

    @RequestMapping("/bipay/transaction")
    public List<Transaction> queryTransaction() throws Exception {
        log.info("[Udun-接口] 查询交易 入参(空)");
        List<Transaction> list = biPayService.queryTransaction();
        log.info("[Udun-接口] 查询交易 返回 count={}", list != null ? list.size() : 0);
        return list;
    }

    /**
     * 校验地址合法性
     * @param mainCoinType
     * @param address
     */
    @RequestMapping("/bipay/checkAddress")
    public boolean checkAddress(String mainCoinType, String address) throws Exception {
        log.info("[Udun-接口] 校验地址 入参 mainCoinType={}, address={}", mainCoinType, address);
        boolean result = biPayService.checkAddress(mainCoinType, address);
        log.info("[Udun-接口] 校验地址 返回 isInternal={}", result);
        return result;
    }

    /**
     *获取支持的币种
     * @param showBalance
     * @return
     * @throws Exception
     */
    @RequestMapping("/bipay/getSupportCoin")
    public List<SupportCoin> getSupportCoin(Boolean showBalance) throws Exception {
        log.info("[Udun-接口] 获取支持币种 入参 showBalance={}", showBalance);
        List<SupportCoin> list = biPayService.getSupportCoin(showBalance);
        log.info("[Udun-接口] 获取支持币种 返回 count={}", list != null ? list.size() : 0);
        return list;
    }
}
