package com.crm.pay.controller.pay;

import com.crm.pay.controller.base.BasePayCloseFunctionController;
import com.crm.pay.entity.callback.MtCallbackEntity;
import com.crm.pay.service.MtService;
import com.crm.rely.backend.core.constant.Constants;
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 com.google.common.base.Strings;
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: 2020-05-07 14:58
 */
@RestController
@RequestMapping("/mt")
public class MtController extends BasePayCloseFunctionController {

    @Autowired
    private MtService service;

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

        if (Strings.isNullOrEmpty(payType)) {
            throw new PayValidatedException(Constants.SYSTEM_ERROR);
        }

        String payTypeCode = getPayTypeCode(payType);

        return payCall(login, amount, payTypeCode, 0, entity, infoEntity, request);
    }

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

        if (Strings.isNullOrEmpty(payType)) {
            throw new PayValidatedException(Constants.SYSTEM_ERROR);
        }

        String payTypeCode = getPayTypeCode(payType);

        return payCall(login, amount, payTypeCode, activity, entity, infoEntity, request);
    }


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

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

        String serial = service.pay(financeDepositSaveEntity);
        PayDto payDto = new PayDto(2, serial);
        return BaseResultDto.success(payDto);
    }

    /**
     * 异步回调
     *
     * @param callback 用于获取回调数据
     * @return 返回成功给第三方系统
     * @throws Exception
     */
    @PostMapping(value = "/{payType}/callback", produces = "text/html")
    public String cnyCallback(@PathVariable("payType") String payType, MtCallbackEntity callback) throws Exception {
        String payTypeCode = getPayTypeCode(payType);

        if (callback != null) {
            callback.setPayType(payTypeCode);
        } else {
            return PayConstants.MTPAY_FAIL_RESULT;
        }
        service.callback(callback);
        return PayConstants.MTPAY_SUCCESS_RESULT;
    }


    /**
     * 同步页面 成功
     *
     * @param response 用于重定向页面
     * @throws IOException
     */
    @GetMapping(value = "/{payType}/pickup")
    public void cnyPickup(@PathVariable("payType") String payType, HttpServletResponse response) throws IOException,
            PayValidatedException {
        String payTypeCode = getPayTypeCode(payType);

        response.sendRedirect("success.html");
    }


    /**
     * 根据payType取得真实的通道code 用于后续处理
     *
     * @param payType
     * @return
     * @throws PayValidatedException
     */
    private String getPayTypeCode(String payType) throws PayValidatedException {
        String payTypeCode;
        if (payType.equals(PayConstants.MTPAY_CNY)) {
            payTypeCode = PayConstants.PAY_MTPAY_CNY;
        } else if (payType.equals(PayConstants.MTPAY_MYR)) {
            payTypeCode = PayConstants.PAY_MTPAY_MYR;
        } else if (payType.equals(PayConstants.MTPAY_IDR)) {
            payTypeCode = PayConstants.PAY_MTPAY_IDR;
        } else if (payType.equals(PayConstants.MTPAY_VND)) {
            payTypeCode = PayConstants.PAY_MTPAY_VND;
        } else {
            throw new PayValidatedException(Constants.SYSTEM_ERROR);
        }
        return payTypeCode;
    }
}
