package com.crm.pay.controller.pay;

import com.crm.pay.controller.base.BasePayCloseFunctionController;
import com.crm.pay.service.Va5PayService;
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;
import java.util.TreeMap;

/**
 * @description:
 * @author: houn
 * @create: 2022-02-17 14:58
 */
@RestController
@RequestMapping("/5pay")
public class Va5PayController extends BasePayCloseFunctionController {

    @Autowired
    private Va5PayService service;

    /**
     * 支付
     *
     * @param login  入金账号
     * @param amount 入金金额
     * @return 错误信息
     * @throws Exception
     */
    @RequestMapping("/va/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payVa(@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.VA5_PAY_KEY_VA, 0, entity, infoEntity, request);
    }

    @RequestMapping("/va/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payVa(@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.VA5_PAY_KEY_VA, activity, entity, infoEntity, request);
    }

    @RequestMapping("/c2c/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payC2c(@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.VA5_PAY_KEY_C2C, 0, entity, infoEntity, request);
    }

    @RequestMapping("/c2c/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payC2c(@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.VA5_PAY_KEY_C2C, activity, entity, infoEntity, request);
    }

    @RequestMapping("/f2f/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payF2f(@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.VA5_PAY_KEY_F2F, 0, entity, infoEntity, request);
    }

    @RequestMapping("/f2f/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payF2f(@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.VA5_PAY_KEY_F2F, 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);
        PayDto payDto = new PayDto(2, serial);
        return BaseResultDto.success(payDto);
    }

    /**
     * 异步回调
     *
     * @param callbackEntity 用于获取回调数据
     * @return 返回成功给第三方系统
     * @throws Exception
     */
    @RequestMapping(value = "/va/callback", produces = "text/html")
    public String callbackVa(@RequestBody TreeMap<String, String> callbackEntity) throws Exception {
        service.callback(callbackEntity, PayConstants.VA5_PAY_KEY_VA);
        return PayConstants.VA5PAY_RESULT_SUCCESS_STATUS;
    }

    @PostMapping(value = "/c2c/callback", produces = "text/html")
    public String callbackC2c(@RequestBody TreeMap<String, String> callbackEntity) throws Exception {
        service.callback(callbackEntity, PayConstants.VA5_PAY_KEY_C2C);
        return PayConstants.VA5PAY_RESULT_SUCCESS_STATUS;
    }

    @RequestMapping(value = "/f2f/callback", produces = "text/html")
    public String callbackF2f(@RequestBody TreeMap<String, String> callbackEntity) throws Exception {
        service.callback(callbackEntity, PayConstants.VA5_PAY_KEY_F2F);
        return PayConstants.VA5PAY_RESULT_SUCCESS_STATUS;
    }


    /**
     * 同步页面 成功
     *
     * @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);
    }
}
