package com.crm.pay.controller.pay;

import com.crm.pay.controller.base.BasePayCloseFunctionController;
import com.crm.pay.dto.callback.vertu.VertuResultDto;
import com.crm.pay.entity.callback.VertuCallbackEntity;
import com.crm.pay.service.VertuService;
import com.crm.rely.backend.model.constant.PayConstants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.pay.dto.PayDto;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.finance.deposit.FinanceDepositAddEntity;
import com.crm.pay.entity.FinanceDepositParameterEntity;
import com.crm.rely.backend.core.exception.PayValidatedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @description:
 * @author: houn
 * @create: 2023-06-18 14:58
 */
@RestController
@RequestMapping("/vertu")
public class VertuController extends BasePayCloseFunctionController {


    @Autowired
    private VertuService service;

    /**
     * 支付
     *
     * @param login  入金账号
     * @param amount 入金金额
     * @return 错误信息
     * @throws Exception
     */
    @RequestMapping("/idr/ib/p2p/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payIdrIbP2p(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_IDR_IB_P2P, 0, entity, infoEntity, request);
    }

    @RequestMapping("/idr/ib/p2p/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payIdrIbP2p(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("activity") int activity,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_IDR_IB_P2P, activity, entity, infoEntity,
                request);
    }

    @RequestMapping("/idr/va/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payIdrVa(@PathVariable("login") Long login,
                                  @PathVariable("amount") BigDecimal amount,
                                  @PathVariable("bankCode") String bankCode,
                                  @RequestBody FinanceDepositParameterEntity entity,
                             CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_IDR_VA, 0, entity, infoEntity, request);
    }

    @RequestMapping("/idr/va/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payIdrVa(@PathVariable("login") Long login,
                                  @PathVariable("amount") BigDecimal amount,
                                  @PathVariable("activity") int activity,
                                  @PathVariable("bankCode") String bankCode,
                                  @RequestBody FinanceDepositParameterEntity entity,
                             CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_IDR_VA, activity, entity, infoEntity,
                request);
    }

    @RequestMapping("/myr/ib/fpx/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payMyrIbFpx(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_MYR_IB_FPX, 0, entity, infoEntity, request);
    }

    @RequestMapping("/myr/ib/fpx/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payMyrIbFpx(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("activity") int activity,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_MYR_IB_FPX, activity, entity, infoEntity,
                request);
    }

    @RequestMapping("/myr/ib/p2c/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payMyrIbP2c(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_MYR_IB_P2C, 0, entity, infoEntity, request);
    }

    @RequestMapping("/myr/ib/p2c/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payMyrIbP2c(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("activity") int activity,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_MYR_IB_P2C, activity, entity, infoEntity,
                request);
    }

    @RequestMapping("/sgd/ib/p2p/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto paySgdIbP2p(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_SGD_IB_P2P, 0, entity, infoEntity, request);
    }

    @RequestMapping("/sgd/ib/p2p/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto paySgdIbP2p(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("activity") int activity,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_SGD_IB_P2P, activity, entity, infoEntity,
                request);
    }

    @RequestMapping("/thb/ib/p2p/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payThbIbP2p(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_THB_IB_P2P, 0, entity, infoEntity, request);
    }

    @RequestMapping("/thb/ib/p2p/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payThbIbP2p(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("activity") int activity,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_THB_IB_P2P, activity, entity, infoEntity,
                request);
    }

    @RequestMapping("/thb/qr/qrc/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payThbQrQrc(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_THB_QR_QRC, 0, entity, infoEntity, request);
    }

    @RequestMapping("/thb/qr/qrc/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payThbQrQrc(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("activity") int activity,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_THB_QR_QRC, activity, entity, infoEntity,
                request);
    }

    @RequestMapping("/thb/qr/qrp/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payThbQrQrp(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_THB_QR_QRP, 0, entity, infoEntity, request);
    }

    @RequestMapping("/thb/qr/qrp/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payThbQrQrp(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("activity") int activity,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_THB_QR_QRP, activity, entity, infoEntity,
                request);
    }

    @RequestMapping("/vnd/ib/p2p/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payVndIbP2p(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_VND_IB_P2P, 0, entity, infoEntity, request);
    }

    @RequestMapping("/vnd/ib/p2p/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payVndIbP2p(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("activity") int activity,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_VND_IB_P2P, activity, entity, infoEntity,
                request);
    }

    @RequestMapping("/vnd/qr/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payVndQr(@PathVariable("login") Long login,
                                  @PathVariable("amount") BigDecimal amount,
                                  @PathVariable("bankCode") String bankCode,
                                  @RequestBody FinanceDepositParameterEntity entity,
                             CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_VND_QR, 0, entity, infoEntity, request);
    }

    @RequestMapping("/vnd/qr/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payVndQr(@PathVariable("login") Long login,
                                  @PathVariable("amount") BigDecimal amount,
                                  @PathVariable("activity") int activity,
                                  @PathVariable("bankCode") String bankCode,
                                  @RequestBody FinanceDepositParameterEntity entity,
                             CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_VND_QR, activity, entity, infoEntity,
                request);
    }

    @RequestMapping("/jpy/ft/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payJpyFt(@PathVariable("login") Long login,
                                  @PathVariable("amount") BigDecimal amount,
                                  @PathVariable("bankCode") String bankCode,
                                  @RequestBody FinanceDepositParameterEntity entity,
                             CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_JPY_FT, 0, entity, infoEntity, request);
    }

    @RequestMapping("/jpy/ft/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payJpyFt(@PathVariable("login") Long login,
                                  @PathVariable("amount") BigDecimal amount,
                                  @PathVariable("activity") int activity,
                                  @PathVariable("bankCode") String bankCode,
                                  @RequestBody FinanceDepositParameterEntity entity,
                             CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_JPY_FT, activity, entity, infoEntity,
                request);
    }

    @RequestMapping("/jpy/fo/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payJpyFo(@PathVariable("login") Long login,
                                  @PathVariable("amount") BigDecimal amount,
                                  @PathVariable("bankCode") String bankCode,
                                  @RequestBody FinanceDepositParameterEntity entity,
                             CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_JPY_IB_FO, 0, entity, infoEntity, request);
    }

    @RequestMapping("/jpy/fo/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payJpyFo(@PathVariable("login") Long login,
                                  @PathVariable("amount") BigDecimal amount,
                                  @PathVariable("activity") int activity,
                                  @PathVariable("bankCode") String bankCode,
                                  @RequestBody FinanceDepositParameterEntity entity,
                             CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.VERTU_PAY_KEY_JPY_IB_FO, activity, entity, infoEntity,
                request);
    }


    private BaseResultDto payCall(Long login,
                                  BigDecimal amount,
                                  String bankCode,
                                  String payType,
                                  int activity,
                                  FinanceDepositParameterEntity entity,
                             CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {
        validated(login, amount);

        FinanceDepositAddEntity financeDepositSaveEntity = getFinanceDepositSaveEntity(login, amount, bankCode,
                payType, activity, entity, request);

        String serial = service.pay(financeDepositSaveEntity, infoEntity);
        PayDto url = new PayDto(1, serial);
        return BaseResultDto.success(url);
    }

    /**
     * 异步回调
     *
     * @param callbackEntity 用于获取回调数据
     * @return 返回成功给第三方系统
     * @throws Exception
     */
    @RequestMapping(value = "/idribp2p/callback")
    @ResponseBody
    public VertuResultDto callbackIdrIbP2p(@RequestBody VertuCallbackEntity callbackEntity) throws Exception {
        callbackEntity.setChannelCode(PayConstants.VERTU_PAY_KEY_IDR_IB_P2P);
        VertuResultDto resultDto = service.callback(callbackEntity);
        return resultDto;
    }

    @RequestMapping(value = "/idrva/callback")
    @ResponseBody
    public VertuResultDto callbackIdrVa(@RequestBody VertuCallbackEntity callbackEntity) throws Exception {
        callbackEntity.setChannelCode(PayConstants.VERTU_PAY_KEY_IDR_VA);
        VertuResultDto resultDto = service.callback(callbackEntity);
        return resultDto;
    }

    @RequestMapping(value = "/myribfpx/callback")
    @ResponseBody
    public VertuResultDto callbackMyrIbFpx(@RequestBody VertuCallbackEntity callbackEntity) throws Exception {
        callbackEntity.setChannelCode(PayConstants.VERTU_PAY_KEY_MYR_IB_FPX);
        VertuResultDto resultDto = service.callback(callbackEntity);
        return resultDto;
    }

    @RequestMapping(value = "/myribp2c/callback")
    @ResponseBody
    public VertuResultDto callbackMyrIbP2c(@RequestBody VertuCallbackEntity callbackEntity) throws Exception {
        callbackEntity.setChannelCode(PayConstants.VERTU_PAY_KEY_MYR_IB_P2C);
        VertuResultDto resultDto = service.callback(callbackEntity);
        return resultDto;
    }

    @RequestMapping(value = "/sgbibp2p/callback")
    @ResponseBody
    public VertuResultDto callbackSgbIbP2p(@RequestBody VertuCallbackEntity callbackEntity) throws Exception {
        callbackEntity.setChannelCode(PayConstants.VERTU_PAY_KEY_SGD_IB_P2P);
        VertuResultDto resultDto = service.callback(callbackEntity);
        return resultDto;
    }

    @RequestMapping(value = "/thbibp2p/callback")
    @ResponseBody
    public VertuResultDto callbackThbIbP2p(@RequestBody VertuCallbackEntity callbackEntity) throws Exception {
        callbackEntity.setChannelCode(PayConstants.VERTU_PAY_KEY_THB_IB_P2P);
        VertuResultDto resultDto = service.callback(callbackEntity);
        return resultDto;
    }

    @RequestMapping(value = "/thbqrqrc/callback")
    @ResponseBody
    public VertuResultDto callbackThbQrQrc(@RequestBody VertuCallbackEntity callbackEntity) throws Exception {
        callbackEntity.setChannelCode(PayConstants.VERTU_PAY_KEY_THB_QR_QRC);
        VertuResultDto resultDto = service.callback(callbackEntity);
        return resultDto;
    }

    @RequestMapping(value = "/thbqrqrp/callback")
    @ResponseBody
    public VertuResultDto callbackThbQrQrp(@RequestBody VertuCallbackEntity callbackEntity) throws Exception {
        callbackEntity.setChannelCode(PayConstants.VERTU_PAY_KEY_THB_QR_QRP);
        VertuResultDto resultDto = service.callback(callbackEntity);
        return resultDto;
    }

    @RequestMapping(value = "/vbdibp2p/callback")
    @ResponseBody
    public VertuResultDto callbackVndIbP2p(@RequestBody VertuCallbackEntity callbackEntity) throws Exception {
        callbackEntity.setChannelCode(PayConstants.VERTU_PAY_KEY_VND_IB_P2P);
        VertuResultDto resultDto = service.callback(callbackEntity);
        return resultDto;
    }

    @RequestMapping(value = "/vndqr/callback")
    @ResponseBody
    public VertuResultDto callbackVndRq(@RequestBody VertuCallbackEntity callbackEntity) throws Exception {
        callbackEntity.setChannelCode(PayConstants.VERTU_PAY_KEY_VND_QR);
        VertuResultDto resultDto = service.callback(callbackEntity);
        return resultDto;
    }

    @RequestMapping(value = "/jpyft/callback")
    @ResponseBody
    public VertuResultDto callbackJpyFt(@RequestBody VertuCallbackEntity callbackEntity) throws Exception {
        callbackEntity.setChannelCode(PayConstants.VERTU_PAY_KEY_JPY_FT);
        VertuResultDto resultDto = service.callback(callbackEntity);
        return resultDto;
    }

    @RequestMapping(value = "/jpyibfo/callback")
    @ResponseBody
    public VertuResultDto callbackJpyIbFo(@RequestBody VertuCallbackEntity callbackEntity) throws Exception {
        callbackEntity.setChannelCode(PayConstants.VERTU_PAY_KEY_JPY_IB_FO);
        VertuResultDto resultDto = service.callback(callbackEntity);
        return resultDto;
    }


    /**
     * 同步页面 成功
     *
     * @param response 用于重定向页面
     * @throws IOException
     */
    @RequestMapping(value = "/pickup")
    public void pickup(HttpServletResponse response) throws Exception, PayValidatedException {

        sendRedirect(response);
    }

    @RequestMapping(value = "/error")
    public void error(HttpServletResponse response) throws Exception {

        sendRedirect(response);
    }
}
