package com.crm.pay.controller.pay;

import com.alibaba.fastjson.JSON;
import com.crm.pay.controller.base.BasePayCloseFunctionController;
import com.crm.pay.entity.callback.PayOkCallbackEntity;
import com.crm.pay.service.PayOkService;
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: 2024-04-29 14:58
 */
@RestController
@RequestMapping("/pay/ok")
public class PayOkController extends BasePayCloseFunctionController {

    @Autowired
    private PayOkService service;

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


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

    @RequestMapping("/thb/pay/{login}/{amount}/{activity}")
    public BaseResultDto thbPay(@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, PayConstants.PAYOK_PAY_TYPE_THB, activity, entity, infoEntity, request);
    }

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


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

    @RequestMapping("/idr/pay/{login}/{amount}/{activity}")
    public BaseResultDto idrPay(@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, PayConstants.PAYOK_PAY_TYPE_IDR, 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,
                payType, activity, entity, request);

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

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

        PayOkCallbackEntity callbackEntity = JSON.parseObject(data, PayOkCallbackEntity.class);
        callbackEntity.setSign(request.getHeader("sign"));

        service.callback(callbackEntity, data, PayConstants.PAYOK_PAY_TYPE_THB);
        return PayConstants.PAYOK_PAY_RESULT_STATUS;
    }

    @RequestMapping(value = "/idr/callback", produces = "text/html")
    public String idrCallback(@RequestBody String data,
                              HttpServletRequest request) throws Exception {

        PayOkCallbackEntity callbackEntity = JSON.parseObject(data, PayOkCallbackEntity.class);
        callbackEntity.setSign(request.getHeader("sign"));

        service.callback(callbackEntity, data, PayConstants.PAYOK_PAY_TYPE_IDR);
        return PayConstants.PAYOK_PAY_RESULT_STATUS;
    }


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