package com.crm.pay.controller.pay;

import com.crm.pay.controller.base.BasePayCloseFunctionController;
import com.crm.pay.entity.callback.SafeCallbackEntity;
import com.crm.pay.service.SafeService;
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 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;

@RestController
@RequestMapping("/safe")
public class SafeController extends BasePayCloseFunctionController {

    @Autowired
    private SafeService service;

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

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

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


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

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

        FinanceDepositAddEntity balanceSaveEntity = getFinanceDepositSaveEntity(login, amount,
                PayConstants.PAY_TYPE_SAFE, activity, entity, request);

        String url = service.pay(balanceSaveEntity);

        PayDto payDto = new PayDto(1, url);
        return BaseResultDto.success(payDto);
    }

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

        SafeCallbackEntity safeCallbackEntity = getCallback(request);

        service.callback(safeCallbackEntity);

        return PayConstants.SAFE_SUCCESS;
    }

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

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

        sendRedirect(response);
    }

    /**
     * 在form-data中获取回调数据
     *
     * @param request 用于获取form-data中的数据
     * @return 返回回调数据构建的实体
     */
    private SafeCallbackEntity getCallback(HttpServletRequest request) {


        String orderNo = request.getParameter("orderNo");
        String orderAmount = request.getParameter("orderAmount");
        String orderCurrency = request.getParameter("orderCurrency");
        String orderPayment = request.getParameter("orderPayment");
        String transactionId = request.getParameter("transactionId");
        String status = request.getParameter("status");
        String signType = request.getParameter("signType");
        String sign = request.getParameter("sign");

        SafeCallbackEntity safeCallbackEntity = new SafeCallbackEntity();
        safeCallbackEntity.setOrderNo(orderNo);
        safeCallbackEntity.setOrderAmount(orderAmount);
        safeCallbackEntity.setOrderCurrency(orderCurrency);
        safeCallbackEntity.setOrderPayment(orderPayment);
        safeCallbackEntity.setTransactionId(transactionId);
        safeCallbackEntity.setStatus(status);
        safeCallbackEntity.setSignType(signType);
        safeCallbackEntity.setSign(sign);

        return safeCallbackEntity;
    }
}
