package com.crm.ucard.controller;

import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.base.BaseReportResultDto;
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.*;
import com.crm.rely.backend.core.dto.wasabi.WasabiActiveCardDto;
import com.crm.rely.backend.core.dto.wasabi.WasabiBalanceInfoDto;
import com.crm.rely.backend.core.dto.wasabi.WasabiOccupationsDto;
import com.crm.rely.backend.core.dto.wasabi.WasabiUpdatePinDto;
import com.crm.rely.backend.core.entity.base.BaseEntity;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.custom.kyc.WebsdkLinkAliEntity;
import com.crm.rely.backend.core.entity.ucard.*;
import com.crm.rely.backend.core.entity.wasabi.*;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.GetIpAndMac;
import com.crm.ucard.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 银行卡服务接口
 */
@Slf4j
@RestController
@RequestMapping("/wasabi/custom")
public class CustomWasabiCardController {

    @Autowired
    private UcardCardService ucardCardService;
    @Autowired
    private WasabiDocumentApiService wasabiDocumentApiService;
    @Autowired
    private UcardPermissionService ucardPermissionService;
    @Autowired
    private KycService kycService;
    @Autowired
    private UcardFileService ucardFileService;
    @Autowired
    private UcardTypeConfigService ucardTypeConfigService;
    @Autowired
    private UcardEmailService ucardEmailService;
    @Autowired
    private UcardBlockchainService ucardBlockchainService;

    /**
     * 商户用户注册
     */
    @PostMapping("/merchant/user/register")
    @FeignClientAnnotation
    public BaseResultDto registerMerchantUser(@RequestBody CardMerchantUserEntity request, InfoEntity entity, HttpServletRequest servletRequest) throws ServiceException {
        String ip = GetIpAndMac.getIp(servletRequest);
        request.setIpAddress(ip);
        request.setCustomId(entity.getCustomInfo().getId());
        request.setCId(entity.getCustomInfo().getCId());
        request.setOperateUser(entity.getCustomInfo().getCId());
        return ucardCardService.registerMerchantUser(request);
    }

    /**
     * 更新商户用户信息
     */
    @PostMapping("/merchant/user/update")
    @FeignClientAnnotation
    public BaseResultDto updateMerchantUser(@RequestBody CardMerchantUserEntity request, InfoEntity entity) throws ServiceException {
        request.setCustomId(entity.getCustomInfo().getId());
        request.setCId(entity.getCustomInfo().getCId());
        request.setOperateUser(entity.getCustomInfo().getCId());
        return ucardCardService.updateMerchantUser(request);
    }

    /**
     * 更新用户邮寄地址
     */
    @PostMapping("/merchant/user/address/update")
    @FeignClientAnnotation
    public BaseResultDto updateUserMailingAddress(@RequestBody WasabiSaveUserMailingAddressEntity request) throws ServiceException {
        return ucardCardService.updateUserMailingAddress(request);
    }

    /**
     * 查看用户详情
     */
    @PostMapping("/merchant/user/single")
    @FeignClientAnnotation
    public BaseResultDto merchantUserSingle(InfoEntity infoEntity) throws Exception {
        return ucardCardService.merchantUserSingle(infoEntity.getCustomInfo().getId());
    }

    /**
     * 用户获取卡片类型
     */
    @PostMapping("/card/types/list")
    @FeignClientAnnotation
    public BaseResultDto getCardTypeList(@RequestBody CardTypeEntity entity, InfoEntity infoEntity) throws Exception {
        entity.setCId(infoEntity.getCustomInfo().getCId());
        return BaseResultDto.success(ucardCardService.getCardTypeList(entity));
    }

    /**
     * 申请开卡
     */
    @PostMapping("/card/apply")
    @FeignClientAnnotation
    public BaseResultDto applyCard(@RequestBody CardApplyEntity request, InfoEntity infoEntity) throws Exception {
        request.setCId(infoEntity.getCustomInfo().getCId());
        return ucardCardService.applyCard(request);
    }

    /**
     * 获取卡片申请列表
     */
    @PostMapping("/card/apply/page")
    @FeignClientAnnotation
    public ResultWithPagerDto<CardApplyDto> getCardApplyPageList(@RequestBody CardApplyPageEntity entity) throws ServiceException {
        return ucardCardService.getCardApplyPageList(entity);
    }

    /**
     * 获取卡片申请详情
     */
    @PostMapping("/card/apply/details")
    @FeignClientAnnotation
    public BaseResultDto cardApplyDetails(@RequestBody BaseEntity entity) throws ServiceException {
        return ucardCardService.cardApplyDetails(entity);
    }

    /**
     * 查询开卡进度
     */
    @PostMapping("/card/apply/progress")
    @FeignClientAnnotation
    public BaseResultDto queryApplyProgress(@RequestBody CardApplyProgressEntity request) throws Exception {
        return ucardCardService.queryApplyProgress(request);
    }

    /**
     * 获取我的卡片列表
     */
    @PostMapping("/card/list")
    @FeignClientAnnotation
    public BaseResultDto getCardList(@RequestBody CardPageEntity entity, InfoEntity infoEntity) throws ServiceException {
        entity.setCId(infoEntity.getCustomInfo().getCId());
        return BaseResultDto.success(ucardCardService.getCardList(entity));
    }

    /**
     * 获取卡片详情
     */
    @PostMapping("/card/single")
    @FeignClientAnnotation
    public BaseResultDto getCardSingle(@RequestBody SingleLongEntity entity) throws ServiceException {
        return BaseResultDto.success(ucardCardService.getCardSingle(entity));
    }

    /**
     * 银行卡激活
     */
    @PostMapping("/card/activate")
    @FeignClientAnnotation
    public BaseResultDto<WasabiActiveCardDto> activateCard(@RequestBody WasabiActiveCardEntity request,
                                                           InfoEntity infoEntity) throws Exception {
        request.setCId(infoEntity.getCustomInfo().getCId());
        return ucardCardService.activeCard(request);
    }

    /**
     * 银行卡充值
     */
    @PostMapping("/card/recharge")
    @FeignClientAnnotation
    public BaseResultDto rechargeCard(@RequestBody WasabiBalanceEntity request, InfoEntity infoEntity) throws Exception {
        request.setCId(infoEntity.getCustomInfo().getCId());
        return ucardCardService.cardDeposit(request);
    }

    /**
     * 查询用户钱包余额
     */
    @PostMapping("/card/wallet/balance")
    @FeignClientAnnotation
    public BaseResultDto<WalletBalanceDto> walletBalance(@RequestBody WalletBalanceEntity request) throws ServiceException {
        return ucardCardService.walletBalance(request);
    }

    /**
     * 获取用户钱包记录列表
     */
    @PostMapping("/card/wallet/record/page")
    @FeignClientAnnotation
    public ResultWithPagerDto<CardWalletRecordDto> getCardWalletRecordPageList(@RequestBody CardWalletRecordPageEntity entity) throws ServiceException {
        return ucardCardService.getCardWalletRecordPageList(entity);
    }

    /**
     * 充值记录分页查询
     */
    @PostMapping("/card/recharge/page")
    @FeignClientAnnotation
    public BaseReportResultDto<List<CardRechargeOrderDto>, CardRechargeOrderSumDto> queryRechargePageList(@RequestBody CardRechargePageEntity entity) throws ServiceException {
        return ucardCardService.queryRechargePageList(entity);
    }

    /**
     * 查询卡片余额
     */
    @PostMapping("/card/balance")
    @FeignClientAnnotation
    public BaseResultDto<WasabiBalanceInfoDto> queryCardBalance(@RequestBody WasabiBalanceInfoEntity request) throws ServiceException {
        return ucardCardService.getBalanceInfo(request);
    }

    /**
     * 查询充值订单
     */
    @PostMapping("/card/recharge/order")
    @FeignClientAnnotation
    public BaseResultDto queryRechargeOrder(@RequestBody CardRechargeOrderQueryEntity request) throws Exception {
        return ucardCardService.queryDepositOrder(request);
    }


    /**
     * 冻结卡片
     */
    @PostMapping("/card/freeze")
    @FeignClientAnnotation
    public BaseResultDto freezeCard(@RequestBody WasabiFreezeAndUnfreezeEntity request, InfoEntity infoEntity) throws Exception {
        request.setCId(infoEntity.getCustomInfo().getCId());
        return ucardCardService.cardFreeze(request);
    }

    /**
     * 解冻卡片
     */
    @PostMapping("/card/unfreeze")
    @FeignClientAnnotation
    public BaseResultDto unfreezeCard(@RequestBody WasabiFreezeAndUnfreezeEntity request, InfoEntity infoEntity) throws Exception {
        request.setCId(infoEntity.getCustomInfo().getCId());
        return ucardCardService.cardUnfreeze(request);
    }

    /**
     * 找回密码
     */
    @PostMapping("/card/password/reset")
    @FeignClientAnnotation
    public BaseResultDto<WasabiUpdatePinDto> resetPassword(@RequestBody WasabiUpdatePinEntity entity,
                                                           InfoEntity infoEntity) throws Exception {
        entity.setCId(infoEntity.getCustomInfo().getCId());
        return ucardCardService.updatePin(entity);
    }

    /**
     * 校验文件
     */
    @PostMapping("/validated/file")
    @FeignClientAnnotation
    public void validatedFile(@RequestParam(value = "file") MultipartFile file) throws ServiceException {
        wasabiDocumentApiService.validatedFile(file);
    }

    /**
     * 查询国家和城市
     */
    @PostMapping("/card/country")
    @FeignClientAnnotation
    public BaseResultDto getCardCountry(@RequestBody CardCountryEntity request) throws ServiceException {
        request.setCardCategory(2);
        return BaseResultDto.success(ucardCardService.getCardCountryList(request));
    }

    /**
     * 查询职业信息
     */
    @PostMapping("/card/occupation/list")
    @FeignClientAnnotation
    public BaseResultDto<List<WasabiOccupationsDto>> getOccupationList() throws ServiceException {
        return ucardCardService.getOccupationList();
    }

    /**
     * 查询交易记录分页列表
     */
    @PostMapping("/card/transac/page")
    @FeignClientAnnotation
    public ResultWithPagerDto<CardTransacOrderDto> getTransactionPageList(@RequestBody CardTransacOrderPageEntity request) throws ServiceException {
        return ucardCardService.getTransacOrderPageList(request);
    }

    /**
     * 权限验证
     */
    @PostMapping("/card/permission/verify")
    @FeignClientAnnotation
    public BaseResultDto permissionVerify(InfoEntity entity) throws ServiceException {
        return ucardPermissionService.permissionVerify(entity.getCustomInfo());
    }

    /**
     * 身份认证获取web链接
     * @param entity
     * @return
     */
    @PostMapping("/getWebsdkLink")
    public BaseResultDto getWebsdkLink(@RequestBody WebsdkLinkAliEntity entity, InfoEntity infoEntity) throws Exception {
        return kycService.getWebsdkLink(entity, infoEntity.getCustomInfo().getId());
    }

    /**
     * 扫码上传文件
     */
    @PostMapping("/scan/code")
    @FeignClientAnnotation
    public BaseResultDto scanCode(@RequestBody CardWebSocketEntity entity) throws ServiceException {
        return ucardFileService.scanCode(entity);
    }

    /**
     * 证件类型配置列表
     *
     * @param entity
     * @return
     */
    @PostMapping("/card/id/type/config/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<CardIdTypeConfigDto> getCardIdTypeConfigList(@RequestBody CardIdTypeConfigPageEntity entity) throws ServiceException {
        return ucardTypeConfigService.getCardIdTypeConfigList(entity);
    }

    /**
     * 查询CVV 发送短信验证码
     */
    @PostMapping("/query/cvv/send/code")
    @FeignClientAnnotation
    public void queryCvvSendCode(@RequestBody @Validated CardSendEmailCodeEntity entity) throws Exception {
        ucardEmailService.queryCvvSendCode(entity);
    }

    /**
     * 验证邮箱验证码查询cvv
     */
    @PostMapping("/query/cvv/verify/code")
    @FeignClientAnnotation
    public BaseResultDto queryCvvVerifyCode(@RequestBody @Validated CardQueryCvvVerifyEmailCodeEntity entity) throws Exception {
        return ucardEmailService.queryCvvVerifyCode(entity);
    }

    /**
     * 查询账户信息
     */
    @PostMapping("/card/account/dropdown")
    @FeignClientAnnotation
    public BaseResultDto cardAccountDropdown(@RequestBody CardAccountDropdownEntity entity) throws Exception {
        return ucardCardService.cardAccountDropdown(entity);
    }

    /**
     * 区块链配置下拉列表
     */
    @PostMapping("/card/blockchain/config/dropdown")
    @FeignClientAnnotation
    BaseResultDto getCardBlockchainConfigDropdown(@RequestBody CardBlockchainConfigDropdownEntity entity) throws ServiceException {
        return ucardBlockchainService.getCardBlockchainConfigDropdown(entity);
    }

    /**
     * 卡扣款分页列表
     */
    @PostMapping("/card/withdraw/page")
    @FeignClientAnnotation
    ResultWithPagerDto<CardWithdrawDto> queryCardWithdrawPage(@RequestBody CardWithdrawPageEntity entity) throws ServiceException{
        return ucardCardService.queryCardWithdrawPage(entity);
    }


}
