package com.crm.pay.controller.pay;

import com.crm.pay.controller.base.BasePayCloseFunctionController;
import com.crm.pay.entity.callback.retailer.PayRetailerCallbackEntity;
import com.crm.pay.service.PayRetailerService;
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.crm.rely.backend.core.exception.ServiceException;
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: 2021-05-31 14:58
 */
@RestController
@RequestMapping("/retailer")
public class RetailerController extends BasePayCloseFunctionController {

    @Autowired
    private PayRetailerService service;

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


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

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


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


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


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

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

    /**
     * 异步回调
     *
     * @param callbackEntity 用于获取回调数据
     * @return 返回成功给第三方系统
     * @throws Exception
     */
    @RequestMapping("/{payCode}/callback")
    public void callback(@PathVariable("payCode") String payCode,
                         @RequestBody PayRetailerCallbackEntity callbackEntity, HttpServletResponse response) throws Exception {

        service.callback(callbackEntity, getPayType(payCode));
        sendRedirect(response);
    }


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

        sendRedirect(response);
    }

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

        sendRedirect(response);
    }

    private String getPayType(String payCode) throws ServiceException {
        if (Strings.isNullOrEmpty(payCode)) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        switch (payCode.toLowerCase()) {

            case "br":
                return PayConstants.PAY_RETAILER_PAY_KEY_BR;
            case "brw":
                return PayConstants.PAY_RETAILER_PAY_KEY_BRW;
            case "cl":
                return PayConstants.PAY_RETAILER_PAY_KEY_CL;
            case "co":
                return PayConstants.PAY_RETAILER_PAY_KEY_CO;
            case "cr":
                return PayConstants.PAY_RETAILER_PAY_KEY_CR;
            case "ec":
                return PayConstants.PAY_RETAILER_PAY_KEY_EC;
            case "sv":
                return PayConstants.PAY_RETAILER_PAY_KEY_SV;
            case "mx":
                return PayConstants.PAY_RETAILER_PAY_KEY_MX;
            case "pa":
                return PayConstants.PAY_RETAILER_PAY_KEY_PA;
            case "pe":
                return PayConstants.PAY_RETAILER_PAY_KEY_PE;
            case "gt":
                return PayConstants.PAY_RETAILER_PAY_KEY_GT;
            case "ar":
                return PayConstants.PAY_RETAILER_PAY_KEY_AR;
            default:
                return payCode;
        }
    }
}
