package com.crm.pay.controller.pay;

import com.crm.pay.controller.base.BasePayCloseFunctionController;
import com.crm.pay.entity.callback.UGatePayCallbackEntity;
import com.crm.pay.service.UGatePayService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
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.pay.entity.finance.withdraw.automatic.FinanceWithdrawInfoEntity;
import com.crm.pay.entity.finance.withdraw.automatic.FinanceWithdrawUGateEntity;
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("/ugate")
public class UGatePayController extends BasePayCloseFunctionController {

    @Autowired
    private UGatePayService service;

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


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

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


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

    private BaseResultDto payCall(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,
                PayConstants.UGATE_PAY_KEY, activity, entity, request);

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

    @PostMapping("/withdraw/info")
    @FeignClientAnnotation
    public BaseResultDto withdrawInfo(@RequestBody FinanceWithdrawInfoEntity entity) throws Exception {
        return BaseResultDto.success(service.withdrawInfo(entity));
    }

    @PostMapping("/withdraw")
    @FeignClientAnnotation
    public BaseResultDto withdraw(@RequestBody FinanceWithdrawUGateEntity entity) throws Exception {
        service.withdraw(entity);
        return BaseResultDto.success();
    }

    /**
     * 异步回调
     *
     * @param callbackEntity 用于获取回调数据
     * @return 返回成功给第三方系统
     * @throws Exception
     */
    @RequestMapping(value = "/callback", produces = "text/html")
    public String callback(@RequestBody UGatePayCallbackEntity callbackEntity, HttpServletResponse response) throws Exception {
        try {
            return service.callback(callbackEntity, PayConstants.UGATE_PAY_KEY);
        } catch (Exception e) {
            return Constants.FAIL;
        }
    }

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

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


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

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


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

    private BaseResultDto payHcCall(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,
                PayConstants.UGATE_PAY_KEY, activity, entity, request);

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

    @PostMapping("/hc/withdraw/info")
    @FeignClientAnnotation
    public BaseResultDto withdrawHcInfo(@RequestBody FinanceWithdrawInfoEntity entity) throws Exception {
        return BaseResultDto.success(service.withdrawInfo(entity));
    }

    @PostMapping("/hc/withdraw")
    @FeignClientAnnotation
    public BaseResultDto withdrawHc(@RequestBody FinanceWithdrawUGateEntity entity) throws Exception {
        service.withdraw(entity);
        return BaseResultDto.success();
    }

    /**
     * 异步回调
     *
     * @param callbackEntity 用于获取回调数据
     * @return 返回成功给第三方系统
     * @throws Exception
     */
    @RequestMapping(value = "/hc/callback", produces = "text/html")
    public String callbackHc(@RequestBody UGatePayCallbackEntity callbackEntity, HttpServletResponse response) throws Exception {
        try {
            return service.callback(callbackEntity, PayConstants.UGATE_PAY_KEY_HC);
        } catch (Exception e) {
            return Constants.FAIL;
        }
    }

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

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