package com.crm.ucard.controller;

import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.ucard.UcardWalletDepositTransactionDto;
import com.crm.rely.backend.core.dto.ucard.UcardWalletWithdrawDto;
import com.crm.rely.backend.core.entity.ucard.CardSendEmailCodeEntity;
import com.crm.rely.backend.core.entity.ucard.UcardWalletDepositTransactionPageEntity;
import com.crm.rely.backend.core.entity.ucard.UcardWalletWithdrawPageEntity;
import com.crm.rely.backend.core.entity.ucard.WalletWithdrawApplyEntity;
import com.crm.rely.backend.core.entity.ucard.vaultody.VaultodyGenerateDepositAddressEntity;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.ucard.service.UcardEmailService;
import com.crm.ucard.service.VaultodyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ucard钱包
 */
@Slf4j
@RestController
@RequestMapping("/ucard/wallet/custom")
public class CustomUcardWalletController {

    @Autowired
    private VaultodyService vaultodyService;
    @Autowired
    private UcardEmailService emailService;

    /**
     * 生成钱包地址
     * @param entity:
     * @return void
     */
    @PostMapping("/generate/deposit/address")
    @FeignClientAnnotation
    public BaseResultDto generateDepositAddress(@RequestBody VaultodyGenerateDepositAddressEntity entity) throws Exception {
        String address = vaultodyService.generateDepositAddress(entity);
        try {
            vaultodyService.linkAddress(entity.getBlockchain(), address);
        } catch (Exception e){
            log.error("链接钱包地址失败", e);
        }

        return BaseResultDto.success(Constants.SUCCESS,address);
    }

    /**
     * 加密货币交易分页列表
     */
    @PostMapping("/deposit/transaction/page")
    @FeignClientAnnotation
    ResultWithPagerDto<UcardWalletDepositTransactionDto> getWalletDepositTransactionPage(@RequestBody UcardWalletDepositTransactionPageEntity entity) throws ServiceException{
        return vaultodyService.getWalletDepositTransactionPage(entity);
    }

    /**
     * 钱包提款分页列表
     */
    @PostMapping("/page")
    @FeignClientAnnotation
    ResultWithPagerDto<UcardWalletWithdrawDto> getWalletWithdrawPage(@RequestBody UcardWalletWithdrawPageEntity entity) throws ServiceException {
        return vaultodyService.getWalletWithdrawPage(entity);
    }

    /**
     * 钱包提款申请
     */
    @PostMapping("/apply")
    @FeignClientAnnotation
    BaseResultDto walletWithdrawApply(@RequestBody WalletWithdrawApplyEntity entity) throws Exception{
        return vaultodyService.walletWithdrawApply(entity);
    }

    @PostMapping("/send/email/code")
    @FeignClientAnnotation
    public BaseResultDto sendEmailCode(@RequestBody CardSendEmailCodeEntity entity) throws Exception {
        emailService.sendEmailCode(entity);
        return BaseResultDto.success(Constants.SEND_SUCCESS);
    }

}
